Cache
createCache from @bext-stack/framework/kv is Laravel's Cache::remember
in TypeScript: memoize any JSON-serializable value behind a key, over a
pluggable store. (This is distinct from the <ISR> fragment cache in
@bext-stack/framework/cache, which caches rendered HTML subtrees — see
Caching.)
When To Use It#
- Skip re-computing something expensive within a freshness window (a report, an aggregate, a third-party API response).
- Small shared flags/values across requests (via the SDK KV store).
remember & friends#
import { createCache, kvStore } from "@bext-stack/framework/kv";
const cache = createCache({ store: kvStore({ appId: "my-site" }) });
// compute once, serve from cache within the TTL:
const report = await cache.remember("report:monthly", 300, () => buildReport());
await cache.put("flag", true, 60);
await cache.get<boolean>("flag"); // true | null
await cache.has("flag");
await cache.forget("flag");
const token = await cache.pull("one-time"); // get + forget
const won = await cache.add("lock", 1, 10); // set only if absent → true/false
createCache JSON-serializes, so you cache typed objects, not just strings.
remember correctly distinguishes absent from a falsy value — false,
0, and "" are cached, not treated as a miss and recomputed.
| Member | Does |
|---|---|
remember(key, ttl, fn) |
get, or compute + store + return |
get(key) / put(key, value, ttl?) |
typed read / write |
has / forget |
presence / removal |
pull(key) |
get then forget |
add(key, value, ttl?) |
write only if absent (a cheap lock) |
Stores#
The cache is a thin layer over a CacheStore:
| Store | Scope | Use |
|---|---|---|
kvStore({ appId }) |
cross-request / process (SDK KV over loopback) | shared, durable |
memoryStore() |
per-V8-isolate Map with TTL |
request-scoped, short-lived, tests |
Bring your own by implementing get/set/delete. A prefix option
namespaces keys so two caches over one store don't collide.
Try It#
The live cache demo calls remember
twice — the factory runs once and the second call is served from cache (same
timestamp). Source in sites/demo/src/app/examples/cache/page.tsx.
See Also#
- Caching — page/ISR caching and the
<ISR>fragment cache. - Jobs & Queue — cache expensive job results.
- Application Toolkit — the rest of the TypeScript app layer.