Volt v0.1 · TypeScript APIs on Bun · Get started
Docs/Guides/Response Caching

Response Caching

Cache public GET responses and verify cache hits.

Complete cache example

index.ts
import { Volt, cache, MemoryCacheStore } from 'voltts';

const app = new Volt();
const store = new MemoryCacheStore();
let reads = 0;
app.get('/catalog', () => ({ reads: ++reads, products: ['Coffee'] }), undefined, [
  cache({ store, ttl: 30, etag: true }),
]);
if (globalThis._importMeta_.main) app.listen(3000);
export default app;

Verify cache behavior

bash
bun run index.ts
curl -i http://localhost:3000/catalog
curl -i http://localhost:3000/catalog

The first response has X-Cache: MISS; the second has X-Cache: HIT and the same reads value. ttl is in seconds. The default key includes the HTTP method, path, and query string. The middleware caches successful 200 responses for GET and HEAD and can generate ETag headers.

Cache boundaries

Only attach shared response caching to public endpoints. The default key does not include user identity or authorization headers. In-memory entries disappear on restart and are not shared across processes. MemoryCacheStore supports deletion and clearing; a RedisCacheStore adapter requires a compatible client. Do not apply this response cache to streaming, binary, or personalized endpoints without a suitable storage and key policy.