Advertisement
Advanced ASP.NET Core, Caching

What is response caching middleware, and how does it differ from application-level data caching?

Short answer

Response caching (via UseResponseCaching() and [ResponseCache] attributes) caches entire HTTP responses at the HTTP layer based on cache-control headers, serving cached output directly without re-invoking the controller action at all -- distinct from application-level data…

Save ASP.NET Core mock interview
Advertisement
AModel answer
Response caching (via UseResponseCaching() and [ResponseCache] attributes) caches entire HTTP responses at the HTTP layer based on cache-control headers, serving cached output directly without re-invoking the controller action at all -- distinct from application-level data caching (IMemoryCache/IDistributedCache), which caches specific pieces of data used within business logic, potentially still requiring the full request pipeline to execute.
builder.Services.AddResponseCaching();
app.UseResponseCaching();

[ResponseCache(Duration = 60, VaryByQueryKeys = new[] { "category" })]
[HttpGet]
public IActionResult GetProducts(string category) => Ok(_products.Where(p => p.Category == category));
Real-world example
A public product catalog API caches entire GET /products?category=electronics responses for 60 seconds using response caching middleware, avoiding the full controller execution (including any data-layer caching logic) entirely for repeated identical requests within that window.
Likely follow-ups
  • How does VaryByQueryKeys affect what counts as a 'unique' cached response?
  • How does response caching interact with a CDN sitting in front of the API?
Related topics