howdy_cache

An application cache for howdy apps: keep the result of expensive work for a while, and throw it away when the data it came from changes. Entries live in this node’s memory or in the app’s database, PostgreSQL or SQLite, or anywhere a store can reach.

Add it to your project:

gleam add howdy_cache@1

Fetch or compute

import howdy/cache
import howdy/cache/memory
import howdy/codec

// In main, once:
let app_cache = cache.new(memory.new(max_entries: 50_000))

// Anywhere:
pub fn find(id: Int) -> service.Result(Note) {
  let entry =
    cache.entry("note:" <> int.to_string(id), ttl: 300)
    |> cache.tags(["note:" <> int.to_string(id)])
  use <- cache.fetch(app_cache, entry, note_codec())
  notes.find(db, id)
}

// When the note changes, inside its transaction or not:
cache.invalidate(app_cache, ["note:" <> int.to_string(id)])

Stores

See the caching docs for the details, and howdy/conditional in the core package for answering 304 Not Modified.

Development

The tests run against the memory store and a SQLite database. Set HOWDY_CACHE_TEST_POSTGRES_URL to run the database store against PostgreSQL too; the tests drop and recreate the package’s tables there.

HOWDY_CACHE_TEST_POSTGRES_URL=postgres://howdy_cache_test@localhost/postgres gleam test
✨ Search Document