Cache-Control Header Is Not Output Caching: A Handler Counter Test
The author compares a Cache-Control header with ASP.NET Core output caching using a handler counter. The header only instructs clients and proxies, so the handler ran on all ten calls, while output caching stored the response on the server and ran the handler once. It is a concrete warning for anyone who thinks a cache header caches on the server.
Key points
- Cache-Control only instructs browsers, CDNs and proxies; the server stores nothing from it
- In the ten-call test the header endpoint ran its handler ten times, the output-cached one once
- Output caching keeps the finished response in server memory and skips the handler on hits
- EvictByTagAsync("reports") lets you drop tagged entries on demand when data changes
- On per-user endpoints, configure vary rules before enabling output caching to avoid serving one user's data to another
The author noticed that a read-heavy endpoint showed no drop in database load after adding a Cache-Control header. The reason is that this header only tells browsers, CDNs and proxies how to keep a response; the server itself stores nothing. To make this visible, they built two endpoints that increment a counter inside the handler, one using the header and one using output caching, and called each ten times from HttpClient on .NET 10.
The header endpoint ran its handler all ten times. The endpoint marked with CacheOutput ran it once, and the remaining calls received the stored response. After evicting the tag with EvictByTagAsync, the handler ran once more, which shows the cache can be invalidated on demand. The post also notes that the ResponseCaching middleware honors such headers only under narrow conditions, and warns about vary rules on per-user endpoints, suggesting HybridCache for data-level reuse.
Real-world use
- A public product listing endpoint that should not re-run its database query on every request
- An anonymous status or report page shared identically by many callers, refreshed by tag when data changes
- Several endpoints reshaping the same query, where caching the query result with HybridCache is the better fit
Why it matters
A Cache-Control header alone does not reduce server or database load. To cut that load, use ASP.NET Core output caching or a data-layer cache, and verify it with a counter in the handler.