⚙️ The Cache Tag That Was Never Actually Attached
Output Caching’s tag-based eviction lets you clear exactly the cached responses you need via IOutputCacheStore.EvictByTagAsync, instead of flushing the entire cache – but that only works for responses that were actually tagged when they were cached, through policy.Tag(“…”) on the endpoint’s caching policy. Call EvictByTagAsync with a tag that was never applied to anything – a typo, a tag wired up on a different endpoint, or just forgotten entirely – and the call returns successfully having evicted precisely nothing, while the stale response keeps being served from cache indefinitely.
🔎 The Problem
app.MapGet("/api/products", GetProducts)
.CacheOutput(policy => policy.Expire(TimeSpan.FromMinutes(10)));
// No .Tag("products") was ever added to this policy.
app.MapPost("/api/products", async (Product p, IOutputCacheStore store) =>
{
await SaveProduct(p);
// This call succeeds with no exception - but since nothing
// was ever tagged "products", it evicts zero cache entries.
// The GET above keeps serving its old cached response for
// the full 10 minutes regardless of this call ever running.
await store.EvictByTagAsync("products", default);
});
✅ Fix: Actually Tag the Policy You Mean to Evict
- Add .Tag(“products”) to every caching policy you intend to invalidate by that tag, and keep the tag name as a shared constant instead of a magic string repeated in two unrelated places.
- Write an integration test that caches a response, calls EvictByTagAsync, and then asserts the next request is actually a cache miss (not just that the eviction call didn’t throw) – that’s the only way to catch a tag that silently never matched anything.
- When adding a new cached endpoint, decide and write down which write operations are supposed to invalidate it, then verify the tag wiring in both places at once rather than adding caching and invalidation in separate, disconnected changes.
🚨 Why This Is Dangerous
- EvictByTagAsync has no way to report ‘tag not found’ – there is no exception, no return value indicating zero entries matched, and no log line pointing at the mismatch.
- Users keep seeing stale data for the full cache duration after every single write, which is easy to misdiagnose as a caching duration problem rather than an invalidation wiring problem.
An eviction call that always succeeds and a tag that was never attached look identical from the outside – the only difference is whether anything was actually there to evict.

