Skip to content

createCache

import { createCache } from '@requence/cache'
function createCache(options?: CacheOptions): Cache
interface CacheOptions {
memoryTTL?: number // Max time (ms) to hold results in memory. Default: 0 (infinite)
memorySize?: number // Max results per function in memory. Default: 0 (infinite)
backend?: CacheBackend | CacheBackend[] // External backend(s)
defaultScope?: string | null | (() => string | null | void) // Default scope
}

The Cache object is a Proxy that combines the management API with direct function access:

Register a cached function. Returns the cache for chaining.

const cache = createCache()
.define('getUser', async (id: string) => db.users.findById(id))
.define('getProject', async (id: string) => db.projects.findById(id))

Invalidate a specific cached result by function key and arguments.

Invalidate all cached results for the given function key(s).

Invalidate all cached results tagged with the given tag(s).

Return a scoped view of the cache. All reads, writes, and invalidations are isolated to the given scope.

Clear all entries in the current scope.

Clear all data from the cache backend(s).

Reset all cache instances created in the current process.

Execute a function with caching disabled. The function executes normally but results are not stored.

Returns an imperative { disable(), enable() } handle for disabling caching across async boundaries.