API Reference
Generated reference for every public export of ocache. For task-oriented guides, start with Caching Functions and Caching HTTP Handlers.
#BlobValue
type BlobValue = ArrayBufferView | ArrayBufferLike;What a byte backend may return: a view, or the buffer behind one.
#CachedEventHandler
type CachedEventHandler<E extends HTTPEvent = HTTPEvent> = EventHandler<E> &Cached handler with resource-level cache management methods.
Each method covers GET and HEAD variants in every base prefix.
#cachedFunction
const cachedFunction = defineCachedFunction;Alias for defineCachedFunction.
#CacheStatus
type CacheStatus = "hit" | "stale" | "revalidated" | "miss";Result of one cache call.
"hit": returned a fresh stored value."stale": returned stale data and started background revalidation."revalidated": replaced an old value before returning."miss": resolved a value when none existed.
#composeStorage
function composeStorage(
layers: ReadonlyArray<StorageInterface | StorageLayer>,
opts: ComposeStorageOptions =Combines several backends into one tiered StorageInterface.
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.
Example:
const storage = composeStorage([
{ storage: createMemoryStorage({ maxBytes: 64 * 1024 * 1024 }), ttl: 60 },
createBlobStorage(redis),
]);#createBlobStorage
function createBlobStorage(backend: BlobBackend): StorageInterfaceAdapts 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.
Example:
const storage = createBlobStorage({
get: (key) => unstorage.getItemRaw(key),
set: (key, value, opts) =>
value === null ? unstorage.removeItem(key) : unstorage.setItemRaw(key, value, opts),
});#createMemoryStorage
function createMemoryStorage(opts: MemoryStorageOptions =Creates Map-based memory storage with TTLs in seconds and LRU eviction.
#defineCachedFunction
function defineCachedFunction<T, ArgsT extends unknown[] = any[]>(
fn: (...args: ArgsT) => T | Promise<T>,
opts: CacheOptions<T, ArgsT> =Wraps a function with caching, SWR, integrity checks, and request deduplication.
Parameters:
fn— Function to cache.opts— Cache options.
Returns: — The cached function and its cache management methods.
#defineCachedHandler
function defineCachedHandler<E extends HTTPEvent = HTTPEvent>(
handler: EventHandler<E>,
opts: CachedEventHandlerOptions<E> =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.
Parameters:
input— Cache options and function arguments.
Example:
await expireCache({
options: { name: "fetchUser", getKey: (id: string) => id, maxAge: 60, swr: true, storage },
args: ["user-123"],
});#invalidateCache
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.
Parameters:
input— Cache options and function arguments.
Example:
await invalidateCache({
options: { name: "fetchUser", getKey: (id: string) => id, storage },
args: ["user-123"],
});#resolveCacheKeys
async function resolveCacheKeys<ArgsT extends unknown[] = any[]>(
input:Returns one storage key per base prefix.
Pass the same getKey, name, group, and base options as the cached function.
This helper computes keys without accessing storage.
Parameters:
input— Cache options and function arguments.
Returns: — Storage keys in base-prefix order.
Example:
const keys = await resolveCacheKeys({
options: { name: "fetchUser", getKey: (id: string) => id },
args: ["user-123"],
});#resolveSignal
function resolveSignal(event: HTTPEvent): AbortSignal | undefinedThe deadline signal of the resolution this event leads, if it leads one.
#resolveStatus
function resolveStatus(event: HTTPEvent): CacheStatus | undefinedThe status of the foreground resolution this event leads, if it leads one.
#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.