Skip to content

Invalidation

Invalidation removes cached entries after data changes. Layeron Cache supports single-entry deletes and grouped purges.

Use delete(request) when you know the exact request to remove:

Terminal window
await apiCache.delete("https://api.example.com/api/products")

If the original entry used custom vary headers, pass the same vary options:

Terminal window
await apiCache.delete(request, {
vary: ["accept-language"],
})

delete returns true when Cloudflare Cache reports that an entry was removed.

Use purge({ request }) when a workflow already works with purge objects:

Terminal window
const result = await apiCache.purge({
request: "https://api.example.com/api/products",
})

The result includes the count of removed entries:

Terminal window
{ "purged": 1 }

Tag purges are the most common application workflow:

Terminal window
await apiCache.put(request, response.clone(), {
tags: ["products", "product:123"],
})
await apiCache.purge({
tags: ["product:123"],
})

Use broad and precise tags together:

TagPurpose
productsPurge product list pages after bulk imports.
product:123Purge one product detail page after editing it.
tenant:acmePurge public tenant-scoped content after tenant changes.
cms:homePurge a specific CMS page or slot.

Prefix purges target keys that begin with a prefix:

Terminal window
await apiCache.purge({
prefix: "https://api.example.com/api/products",
})

Prefix purges use Cache metadata stored in the internal Database product. They are useful for grouped URL structures such as /api/docs/ or /api/catalog/.

Write routes should purge the entries affected by the change:

Terminal window
app.post("/api/products/:id", async (request) => {
const pathSegments = new URL(request.url).pathname.split("/")
const id = pathSegments[3]
const input = await request.json()
await updateProduct(id, input)
await apiCache.purge({
tags: ["products", `product:${id}`],
})
return Response.json({ ok: true })
})

For webhook-driven content updates, purge inside the webhook handler after the event has been validated and persisted.

StrategyUse When
Delete by requestYou can reconstruct one exact request key.
Purge by requestYou want a consistent purge(...) call shape.
Purge by tagMultiple URLs share the same data dependency.
Purge by prefixURLs share a predictable path prefix.

Prefer tags for application data. Tags express the relationship between cached responses and the data that can invalidate them.