Composable caching primitives with TTL, stale-while-revalidate, and HTTP response caching. Zero framework dependencies — works with any runtime that has standard Request/Response.
Reads try each layer in order and stop at the first hit, promoting it into every earlier
layer. Writes and deletes reach every layer. A layer that throws is skipped rather than
failing the operation, so a shared remote layer can be down while a local one still serves.
This is a backend, not a cache option: the cache above it sees one store with one
declaration, so nothing in the key, the entry, or the purge path changes.
function createBlobStorage(backend: BlobBackend): StorageInterface;
Adapts a byte-only backend into a StorageInterface, storing each entry as one frame.
The entry's metadata travels as JSON and its payload travels as itself, appended after it.
That keeps a response body — text or binary — out of the JSON document: text pays no
escaping in either direction, and bytes pay no base64 and no 4/3 expansion in the backend.
The payload is the one named by {@link CacheEntry.payload}, which the producer of the entry
declares — http/entry.ts for a response body, cache.ts for a byte value. Nothing here
infers a payload from a value's shape.
This declares binary, because the frame carries the declared payload as bytes. Every
other member of the entry is JSON, exactly as it would be on a serializing backend: a byte
view hidden somewhere ocache did not put one does not survive, on this backend or on any
other JSON-shaped one.
A frame written by a different version is read as a miss, so a format change costs one
revalidation rather than a mangled entry.
Wraps an HTTP handler with response caching and conditional response support.
Only GET and HEAD requests without Range are cacheable.
Only 200, 203, 301, and 308 responses are stored.
Response Cache-Control and Vary headers can prevent storage.
Parameters:
handler — Handler to cache.
opts — Cache and HTTP options.
Returns: — A cached handler with resource-level cache management methods.
EventHandler
type EventHandler<E extends HTTPEvent = HTTPEvent> = (
Handler that receives an HTTP event.
expireCache
async function expireCache<ArgsT extends unknown[] = any[]>( input:
Marks matching entries as stale without removing them.
SWR may serve the stale value within its original stale window.
Without SWR, the next call revalidates before returning.
Pass the original lifetime options to preserve the remaining storage TTL.
This function throws when options.storage is unset.
async function invalidateCache<ArgsT extends unknown[] = any[]>( input:
Removes matching entries from all base prefixes.
Pass the original options object or the same explicit storage backend.
This function throws when options.storage is unset because no global store exists.
Prefer the cached function's .invalidate() method when available.
function resolveSignal(event: HTTPEvent): AbortSignal | undefined;
The deadline signal of the resolution this event leads, if it leads one.
resolveStatus
function resolveStatus(event: HTTPEvent): CacheStatus | undefined;
The status of the foreground resolution this event leads, if it leads one.
scheduleTimer
function scheduleTimer(fn: () => void, ms: number): () => void;
Schedules fn after ms, past the 32-bit timer limit, without keeping the process alive.
A delay longer than MAX_TIMER_MS is chained: each hop re-arms for what remains,
so a 30-day maxAge fires in 30 days rather than in 1 ms. Returns the cancel function.
Shared with withDeadline in cache.ts so no timer in the cache can overflow.
StorageOption
type StorageOption = StorageInterface | (() => StorageInterface);
A storage instance or a late-bound storage factory.
The cache calls a factory once, on the first cache operation.