Skip to content

ResponseCacheOptions

Defined in: src/http/cache/ResponseCache.ts:31

HTTP response-cache directive. Wraps a handler with a read-through cache: on hit, the cached response is returned without invoking the inner handler; on miss, the handler runs and its response (if its status code is in cacheStatuses) is stored under the user’s key.

Stampede protection: when many requests hit the same cache-miss concurrently, only one runs the handler; the rest await its result via an in-process Map<key, Promise<HttpResponse>>. This is per-process — cluster-wide single-flight is explicitly out of scope (the complexity-to-value ratio is poor for a cache layer).

Invalidation is the caller’s responsibility: write handlers should cache.delete(key) on mutate. We deliberately avoid auto-magic (tags, patterns) because those are the source of most stale-cache production bugs.

Usage:

const userCache = cached({ cache: ext.cache(), ttlMs: 30_000, key: (req) => users:${req.params.id}, }); route(get(‘/users/:id’, userCache(req => askUserActor(req.params.id))));

readonly cache: Cache

Defined in: src/http/cache/ResponseCache.ts:33

Backing cache.


readonly optional cacheStatuses?: readonly number[]

Defined in: src/http/cache/ResponseCache.ts:48

Status codes whose responses are cacheable. Default [200] — only 2xx are cached. Pass [200, 404] if you want to cache “not found” responses (saves repeat lookups when callers query unknown ids).


readonly key: (req) => string | Promise<string>

Defined in: src/http/cache/ResponseCache.ts:37

Identity function — derives the cache key from the request.

HttpRequest

string | Promise<string>


readonly optional keyPrefix?: string

Defined in: src/http/cache/ResponseCache.ts:42

Cache-key namespace prepended to the user key. Default 'rsp:' so multiple response-caches in one Redis don’t collide.


readonly ttlMs: number

Defined in: src/http/cache/ResponseCache.ts:35

TTL on stored responses (milliseconds). Required — no TTL invites unbounded growth.