Cambridge Core

skeleton

Layered Cache Demo

The fresh panel generates a new value on every request. The cached panel serves the same value until its 30-second TTL expires — watch the age counter to see the difference.

Fresh no cache
GET /api/demo/fresh
generatedAt2026-05-11T15:15:21.520Z
random0.19809587
age0s
Cached SWR · 30s TTL
GET /api/demo/cached → L1 → L2
generatedAt2026-05-11T15:15:21.724Z
random0.50662943
age0s

L1 — Cache API

Per-datacenter. Sub-millisecond on a hit. Stored in the same physical location as the Worker handling the request.

L2 — Workers KV

Globally replicated key-value store. Backfills L1 on a miss. Survives edge restarts and spans every Cloudflare datacenter.

SWR strategy

Stale-While-Revalidate: when the TTL expires the next request still gets the cached response instantly, while the handler re-runs in the background.

Local dev (npm run dev): Nitro in-memory cache — caching works but not the real CF layers.

Local preview (npm run preview): Miniflare emulates KV + Cache API — full layered cache behaviour.

Deployed (npm run deploy): Real Cloudflare edge — L1 per-datacenter Cache API, L2 globally-replicated KV.