In-process key-value cache with TTL, LRU eviction, and per-namespace counters. Proven namespaces feed runtime sandboxing.
import { cacheGet, cacheSet, cacheIncr } from "zigttp:cache";
function handler(req) {
const cached = cacheGet("pages", req.url);
if (cached !== undefined) return Response.json(JSON.parse(cached));
cacheIncr("metrics", "requests", 1, 0);
const page = { path: req.url, ts: Date.now() };
cacheSet("pages", req.url, JSON.stringify(page), 60);
return Response.json(page);
}| Export | Signature | Effect | Purpose |
|---|---|---|---|
cacheGet |
cacheGet(namespace, key): string | undefined |
read | Read a cached value. |
cacheSet |
cacheSet(namespace, key, value, ttlSeconds): boolean |
write | Store a value with a TTL. |
cacheDelete |
cacheDelete(namespace, key): boolean |
write | Remove a key. |
cacheIncr |
cacheIncr(namespace, key, delta, ttlSeconds): number |
write | Atomic increment; creates the key if absent. |
cacheStats |
cacheStats(namespace): { hits, misses, size } |
read | Per-namespace counters. |
TTLs are in seconds. Pass 0 to skip TTL (entries still subject to
LRU eviction when the namespace capacity is reached).
- Every literal namespace feeds the
cache.namespacescontract section.cache.dynamicisfalsewhen all calls use string literals. - With a proven namespace list, the runtime sandbox restricts cache
access to exactly those names. A handler calling
cacheGet(userInput, ...)reportscache.dynamic: trueand the namespace set stays open.
cacheSet()returnsfalsewhen the namespace policy blocks the write.cacheGet()returnsundefinedfor missing or policy- denied reads.cacheIncr()returns the new value. It throws on non-numeric deltas.
- No external store; data lives in the server process. Cache does not survive restarts.
- Multi-process deployments do not share cache state. Use
zigttp:sqlfor cross-process shared state.
zigttp:ratelimit- sliding-window rate limiting built on top ofcacheIncr.zigttp:sql- durable relational storage when cache eviction is unacceptable.