BastionCache
Bastion.Cache — ETag, response caching, fragment caching, and Cache-Control helpers.
Provides caching middleware that composes into the HttpServer Conn pipeline.
Typical usage:
conn
|> MyApp.Router.route()
|> Bastion.Cache.etag()
-- Full-response cache:
Bastion.Cache.cached(conn, "posts:index", 60, fn c ->
HttpServer.send_resp(c, 200, posts_html())
end)
-- Fragment cache:
Bastion.Cache.fragment("stats:global", 300, fn ->
IOList.from_string("<div>" ++ compute_stats() ++ "</div>")
end)Functions
Set an arbitrary Cache-Control header value on the response.
Full-response cache middleware. On a cache hit the stored status and body are restored onto conn and the generator is skipped. On a miss the generator runs, its result is stored in Vault with the given TTL, and then returned.
`key` — string cache key, e.g. `"posts:index"`
`ttl_secs` — time-to-live in seconds; `0` caches forever
`generator` — `fn(Conn) -> Conn` that builds the response
BastionCache.cached(conn, "posts:index", 60, fn c ->
HttpServer.send_resp(c, 200, posts_html())
end)ETag middleware. Computes a weak ETag from the response body hash, sets the ETag response header, and short-circuits to 304 Not Modified when the client's If-None-Match header matches.
Call after the response body has been set but before sending.
conn
|> MyApp.Router.route()
|> BastionCache.etag()Fragment cache. On a hit returns the cached IOList directly. On a miss calls the generator, stores with ttl_secs, and returns it.
`key` — string cache key, e.g. `"stats:global"`
`ttl_secs` — TTL in seconds; `0` caches forever
`generator` — `fn() -> IOList`
BastionCache.fragment("stats:global", 300, fn () ->
IOList.from_string("<div>" ++ compute_stats() ++ "</div>")
end)Delete a single entry from the named Vault table by key. No-op if the key does not exist.
BastionCache.invalidate("response_cache", "posts:index")Delete all entries whose string key starts with prefix from the named Vault table. O(n) scan — avoid on very large tables.
BastionCache.invalidate_prefix("fragment_cache", "user:42:")Set Cache-Control: no-store, no-cache, must-revalidate — no caching.
Set Cache-Control: public, max-age=<seconds>.