Skip to content

Examples

These examples show complete Cache patterns you can adapt inside Layeron route handlers.

Cache a public list endpoint and purge it after writes.

Terminal window
const apiCache = cache({
name: "public-api",
ttlSeconds: 60,
tags: ["products"],
})
app.use(apiCache)
app.get("/api/products", async (request) => {
const cached = await apiCache.match(request)
if (cached) {
return cached
}
const products = await listProducts()
const response = Response.json({ products })
await apiCache.put(request, response.clone(), {
tags: ["products"],
})
return response
})
app.post("/api/products", async (request) => {
const input = await request.json()
const product = await createProduct(input)
await apiCache.purge({
tags: ["products"],
})
return Response.json({ product }, { status: 201 })
})

Attach list and item tags to detail responses.

Terminal window
app.get("/api/products/:id", async (request) => {
const pathSegments = new URL(request.url).pathname.split("/")
const id = pathSegments[3]
const cached = await apiCache.match(request)
if (cached) {
return cached
}
const product = await findProduct(id)
if (!product) {
return Response.json({ error: "Missing product" }, { status: 404 })
}
const response = Response.json({ product })
await apiCache.put(request, response.clone(), {
tags: ["products", `product:${id}`],
ttlSeconds: 120,
})
return response
})
app.patch("/api/products/:id", async (request) => {
const pathSegments = new URL(request.url).pathname.split("/")
const id = pathSegments[3]
const input = await request.json()
const product = await updateProduct(id, input)
await apiCache.purge({
tags: ["products", `product:${id}`],
})
return Response.json({ product })
})

Use vary when the same path can produce different languages.

Terminal window
const contentCache = cache({
name: "content",
ttlSeconds: 300,
vary: ["accept-language"],
})
app.use(contentCache)
app.get("/api/homepage", async (request) => {
const cached = await contentCache.match(request)
if (cached) {
return cached
}
const language = request.headers.get("accept-language") ?? "en"
const page = await loadHomepage(language)
const response = Response.json({ page })
await contentCache.put(request, response.clone(), {
tags: ["homepage"],
})
return response
})

Purge cached content after a CMS or commerce webhook.

Terminal window
app.post("/webhooks/catalog", async (request) => {
const event = await verifyCatalogWebhook(request)
if (event.type === "product.updated") {
await apiCache.purge({
tags: ["products", `product:${event.productId}`],
})
}
if (event.type === "catalog.rebuilt") {
await apiCache.purge({
tags: ["products"],
})
}
return Response.json({ ok: true })
})