Output caching

Output caching is a technique that enables caching of HTTP responses in ASP.NET Core apps. Output caching differs from response caching in the following ways:

  • The caching behavior is configurable on the server. Response caching behavior is defined by HTTP headers.
  • Output caching can be used for UI apps such as Razor Pages, while response caching is typically not beneficial for them because browsers generally set request headers that prevent caching.

Output caching is available in ASP.NET Core 7.0 and later. To use output caching, you need to add the Microsoft.AspNetCore.OutputCache NuGet package to your project and configure the middleware in your Startup class. For example:

public void ConfigureServices(IServiceCollection services)
{
    services.AddOutputCache();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseOutputCache();
    // ...
}

You can also configure some options for the middleware, such as the default cache policy, the cache key, the cache revalidation, the cache tags, and the cache storage.


You can apply output caching to a specific endpoint or page by using the CacheOutput extension method or the [OutputCache] attribute. For example:

app.MapGet("/cached", () => DateTime.Now).CacheOutput();
app.MapGet("/attribute", [OutputCache] (context) => DateTime.Now);

You can also create policies to specify caching configuration that applies to multiple endpoints or pages. For example:

services.AddOutputCache(options =>
{
    options.AddBasePolicy(builder => builder.Expire(TimeSpan.FromSeconds(10)));
    options.AddPolicy("Expire20", builder => builder.Expire(TimeSpan.FromSeconds(20)));
    options.AddPolicy("Expire30", builder => builder.Expire(TimeSpan.FromSeconds(30)));
});

You can then apply a policy to an endpoint or page by using the CacheOutput extension method or the [OutputCache] attribute with the policy name. For example:

app.MapGet("/policy20", () => DateTime.Now).CacheOutput("Expire20");
app.MapGet("/policy30", [OutputCache("Expire30")] (context) => DateTime.Now);

Response caching vs Output caching

the difference between response caching and output caching is that:

  • Response caching is mainly for browser caching and uses the HTTP cache headers to control how the clients or proxies should cache the response. It can reduce the number of requests to the server, but it does not store the response on the server side.
  • Output caching is just for server caching and stores the response on the server side based on some configuration. It can increase the throughput of the server when responses are cached, but it does not affect the browser or proxy caching behavior.

So, if you want to optimize the client-side caching, you should use response caching. If you want to optimize the server-side caching, you should use output caching. You can also use them both for different endpoints or scenarios.



Vikash Chauhan

C# & .NET experienced Software Engineer with a demonstrated history of working in the computer software industry.

Post a Comment

Previous Post Next Post

Contact Form