Projects /

Project overview

response-cache-kit

A cache for completed HTTP response snapshots that sits beside a framework handler.

Use cases and boundaries

Reuse the status, headers and body of a stored response when several requests would compute the same result. Keep cache lifetime, variants and user partitioning explicit. The host provides the underlying loader and writes the response through its own framework.

Getting started and compatibility

Install the released package

npm install response-cache-kit@1.2.2

Node.js 18+. This handler fragment is for a public catalog. Configure vary: ['accept-language'] and pass the raw request.headers to cache.handle() so the library can include Accept-Language in its key. The response header Vary: Accept-Language tells downstream HTTP caches how to match variants. The library uses its own vary option to build the application cache key.

import { createResponseCache } from 'response-cache-kit';
const cache = createResponseCache({ ttl: 2_000, vary: ['accept-language'] });

// Inside the host's async GET handler, load the catalog using the request's language.
const result = await cache.handle({ method: request.method, url: request.url, headers: request.headers },
  async () => ({
    status: 200,
    headers: { 'content-type': 'application/json', vary: 'Accept-Language' },
    body: await loadCatalog(request)
  }));

Create cache once and send the returned response through the host framework. For the same URL, requesting en, zh-CN, en, zh-CN within the TTL gives a miss for each new language and then a hit for each repeat in a local 1.2.2 check. For user- or tenant-specific responses, pass a partitionKey and set an explicit cache policy. 1.2.2 package archive

Project relationships