Skip to content

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 cache
  • MISS — fetched from upstream, now cached
  • BYPASS — upstream Cache-Control directive prevented caching
  • SKIP — 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.

CacheConfig

Cache TTL, storage backend, and key strategy. All fields optional.

Policy

A Policy at priority 40.

// Simple 5-minute in-memory cache for GET requests
cache({ ttlSeconds: 300 });
// Cache with Vary on Accept-Language and custom store
cache({
ttlSeconds: 600,
varyHeaders: ["accept-language"],
store: new CacheApiCacheStore(caches.default),
});