Invalidation
Invalidation removes cached entries after data changes. Layeron Cache supports single-entry deletes and grouped purges.
Delete One Entry
Section titled “Delete One Entry”Use delete(request) when you know the exact request to remove:
await apiCache.delete("https://api.example.com/api/products")If the original entry used custom vary headers, pass the same vary options:
await apiCache.delete(request, { vary: ["accept-language"],})delete returns true when Cloudflare Cache reports that an entry was removed.
Purge by Request
Section titled “Purge by Request”Use purge({ request }) when a workflow already works with purge objects:
const result = await apiCache.purge({ request: "https://api.example.com/api/products",})The result includes the count of removed entries:
{ "purged": 1 }Purge by Tag
Section titled “Purge by Tag”Tag purges are the most common application workflow:
await apiCache.put(request, response.clone(), { tags: ["products", "product:123"],})
await apiCache.purge({ tags: ["product:123"],})Use broad and precise tags together:
| Tag | Purpose |
|---|---|
products | Purge product list pages after bulk imports. |
product:123 | Purge one product detail page after editing it. |
tenant:acme | Purge public tenant-scoped content after tenant changes. |
cms:home | Purge a specific CMS page or slot. |
Purge by Prefix
Section titled “Purge by Prefix”Prefix purges target keys that begin with a prefix:
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/.
Invalidate After Writes
Section titled “Invalidate After Writes”Write routes should purge the entries affected by the change:
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.
Choosing an Invalidation Strategy
Section titled “Choosing an Invalidation Strategy”| Strategy | Use When |
|---|---|
| Delete by request | You can reconstruct one exact request key. |
| Purge by request | You want a consistent purge(...) call shape. |
| Purge by tag | Multiple URLs share the same data dependency. |
| Purge by prefix | URLs share a predictable path prefix. |
Prefer tags for application data. Tags express the relationship between cached responses and the data that can invalidate them.