cache
cache(
config?):Policy
Defined in: src/policies/traffic/cache.ts:175
Cache upstream responses to reduce latency and load.
Sets a cache status header on every response:
HIT— served from cacheMISS— fetched from upstream, now cachedBYPASS— upstream Cache-Control directive prevented cachingSKIP— not eligible for caching (wrong method or server error status)
Server error responses (5xx) are never cached. Store failures degrade gracefully via safeCall — a broken cache store never crashes the request.
For methods with a request body (POST, PUT, PATCH), the default cache key includes a SHA-256 hash of the body to prevent key collisions across different payloads.
Parameters
Section titled “Parameters”config?
Section titled “config?”Cache TTL, storage backend, and key strategy. All fields optional.
Returns
Section titled “Returns”A Policy at priority 40.
Example
Section titled “Example”// Simple 5-minute in-memory cache for GET requestscache({ ttlSeconds: 300 });
// Cache with Vary on Accept-Language and custom storecache({ ttlSeconds: 600, varyHeaders: ["accept-language"], store: new CacheApiCacheStore(caches.default),});