الانتقال إلى المحتوى
.NETASP.NET Core· 6 دقائق للقراءة

Response Caching Is Not Output Caching: Who Really Caches Your API Responses

The response caching middleware is an HTTP cache that obeys headers, including the client's. Output caching is the server-side cache you configure, evict and share between requests on your terms.

ReadIn

مقال كل يوم يفرق معاك

22 نوفمبر 2026
  • كاتب موثّق
  • .NET 10
محتويات المقال

The /products/featured endpoint runs the same database query thousands of times a minute, so a developer adds AddResponseCaching(), UseResponseCaching() and a Cache-Control: public, max-age=60 header. A quick test with curl looks perfect: the handler runs once, then every call for a minute comes from memory. After the deploy, the database dashboard doesn't move. The mobile app's HTTP client sends Cache-Control: no-cache on every request, a setting someone added years ago to avoid stale screens, and the middleware obeys it. And when marketing edits the featured list, there's no way to clear what browsers and the CDN already keep.

The common misconception

The belief: adding the response caching middleware gives the server its own response cache. You register it, mark an endpoint, and the server stores the response and reuses it on your terms.

It's a reasonable belief. The name says "response caching", and the middleware really does store responses in server memory. Tests with curl or Postman show it working, because those tools send no caching headers unless you ask. The [ResponseCache] attribute looks like server configuration. And before .NET 7 this was the only built-in way to cache whole responses, so years of tutorials present it as the server cache.

What's missing is who makes the rules. The middleware follows the HTTP caching standard, RFC 9111, and in that standard the client has a say.

What actually happens

Response caching is an HTTP cache inside your app

The middleware behaves like a proxy that happens to run in your process. It's a , so it stores a response only when HTTP allows it: a 200 answer to a GET or HEAD, marked public with a freshness lifetime such as max-age=60, with no Set-Cookie header in the response and no Authorization header in the request. Your app's only lever is the headers it sends.

The client gets a vote

RFC 9111 requires a cache to honor a valid Cache-Control header from the client, and the docs say plainly that there's "no developer control" over this. Browsers often send no-cache or max-age=0 when a page is refreshed. Tested on .NET 10: plain requests were served from memory, while three requests with Cache-Control: no-cache ran the handler three times. Each forced response also replaced the stored copy.

Your headers reach everyone else too

Cache-Control: public, max-age=60 isn't a private note to the middleware. It travels with the response and tells the browser and every proxy or CDN on the way to keep it for a minute. Those copies are out of your reach. And [ResponseCache] is an MVC filter: on a minimal API endpoint it compiled, added no header, and nothing was cached.

Output caching: the server decides

Output caching, added in .NET 7, works the other way around. A policy on the server decides which endpoints are cached, for how long (60 seconds by default), how the key varies, and which tags an entry carries. In the same test, no-cache requests were still served from the cache, and the cached responses carried no Cache-Control header, so browsers and proxies weren't told to keep anything. Even an endpoint that set Cache-Control: no-store was cached, because the policy wins.

It also handles a . By default, concurrent misses for the same entry wait for the first request to fill it. With 50 concurrent requests to a cold 300 ms endpoint, the handler ran 50 times behind response caching and once behind output caching.

The fix: let the server own its cache

Both versions use the same Catalog service, registered as scoped:

C# Program.cs
var builder = WebApplication.CreateBuilder(args);builder.Services.AddScoped<Catalog>();builder.Services.AddResponseCaching();var app = builder.Build();app.UseResponseCaching();app.MapGet("/products/featured", async (HttpContext ctx, Catalog catalog) =>{    // Also tells browsers and every proxy on the way to keep it    ctx.Response.Headers.CacheControl = "public, max-age=60";    return await catalog.GetFeaturedAsync();});// A request with "Cache-Control: no-cache" skips the cache entirelyapp.Run();

The PUT handler is the part response caching can't give you. After a write, EvictByTagAsync drops every entry tagged catalog, so the next read rebuilds it instead of serving a stale list for up to a minute.

Response cachingOutput caching
Who decidesResponse headers, plus the client's request headersPolicies you configure on the server
Client sends no-cacheSkips the cacheIgnored
Browsers and proxiesTold to cache as wellTold nothing
Clearing entriesWait for expiryEvictByTagAsync by tag
Many misses for one entryEvery request runs the handlerOne runs, the rest wait
StorageServer memory onlyMemory, Redis, or a custom IOutputCacheStore

How to apply it in your project

  1. Find every response cacheSearch for UseResponseCaching, [ResponseCache] and code that sets Cache-Control: public. For each one, write down whether the goal is client and CDN caching or less work on the server.
  2. Move server-side caching to output cachingRegister AddOutputCache with named policies, call UseOutputCache after UseCors, UseAuthentication and UseAuthorization, and attach policies with CacheOutput or [OutputCache].
  3. Evict on writesTag cached endpoints by the data they show, and call EvictByTagAsync in the handlers that change that data.
  4. Set client headers on purposeKeep Cache-Control: public, max-age only on responses that are safe for any browser or CDN to keep. Send private or no-store on everything that depends on the user.

Key takeaways

  • The response caching middleware is an RFC 9111 HTTP cache: it follows headers, and the client's no-cache makes it fetch a fresh response.
  • Cache-Control: public tells browsers and proxies to cache too, and you can't evict their copies.
  • Output caching is controlled by server policies: clients can't bypass it, and tags let you evict entries after writes.
  • Output caching locks concurrent misses for the same entry; response caching runs the handler for every one of them.
  • Keep personalized responses out of both: never mark them public, and put UseOutputCache after authentication.

📚 المصادر

  1. Overview of caching in ASP.NET Core — Microsoft Learn
  2. Output caching middleware in ASP.NET Core — Microsoft Learn
  3. Response caching middleware in ASP.NET Core — Microsoft Learn
  4. RFC 9111: HTTP Caching — IETF

التعليقات (0)