.NET 7 new Output Cache Middleware and It’s Customization
New .Net 7 Output Cache middleware , create custom Output Cache policy for application and clear/flush/expire it when needed
This article covers
- Introduce new .Net7 output cache middleware
- Enable output cache
- Introduce caching properties
- clear/flush/expire cached values before expire time
- create custom cache policy
At the end of this article you can add and use OutputCache middle ware and create our own Custom OutputCache policy based on your need and clear/flush/expire it before expire time reached.

Introduce Output caching
Output caching is a new middleware that helps you store results from your web app and web API and serve them from a cache rather than computing them every time, which improves performance and frees up resources for other activities.
based on GitHub documentation motivation and goals for creating OutputCache and its difference from ResponseCache is as follow:
The existing response cache was designed to implement standard HTTP caching semantics. E.g. it cached based on HTTP cache headers like a proxy would (and may be useful in YARP). It also honors the client’s request cache headers like using no-cache to bypass the cache.
An output cache is a different beast. Rather than checking HTTP cache headers to decide what should and should not be cached, the decision is instead made directly by the application, likely with a new request Feature, attributes, HttpResponse extension methods, etc. This cache also does not honor any request cache headers from the client. The client isn’t supposed to know it’s receiving a cached response.
We should create a new caching middleware that’s explicitly designed for output caching. Leave the old one alone, it’s fine for what it was designed for, but it doesn’t cover the output cache scenarios. Trying to combine the two would just get messy, better to have a clean distinction. Note we can probably copy some of the infrastructure from the existing middleware around intercepting responses and storing them, but the public surface area will be fairly different.
There was one output caching style feature that was added to the response caching middleware later, that should be copied/moved(?). It’s the VaryByQueryKeys feature on
ResponseCachingFeature
.
for more detail take a look at https://github.com/dotnet/aspnetcore/issues/27387
in this article I’m going to use controller . if you use minimal API take a look at this article : https://www.honlsoft.com/blog/2022-09-24-output-caching-in-asp-net-core
Enable Output caching
for enable Output caching, you need to add its middleware to service collection. add following code to program.cs
now we must create and introduce our cache policy to middleware
for this we can use AddBasePolicy() and AddPolicy() methods:
- AddBasePolicy: this method set base behaviors for all polices. for example we can set cache expire time to 10 min and will be applied to all polices. also we can override this behavior in each policy.
- AddPolicy: this method set a policy. all policies inherits behavior from AddBasePolicy method . we can override any behavior inherited from base policy by set it again in AddPolicy, for example we can set expire time to 5 min . for each policy we must set name and use this name in place to have policy for intended action.
these methods can be set as bellow:
now we can use this policy like this:
Caching properties
We can cache by route values, query string, request headers or set not to store response or even change cache duration. for each one we can set like what is set for PolicyName
- VaryByRouteValueNames: read and set from route.
- VaryByHeaderNames : read and set from header.
- VaryByQueryKeys:read and set from query
- NoStore : set not to store response (is boolean)
- Duration :change or set cache duration (in seconds)
Clear/Flush/Expire cached values
for clear/flush/expire cached values before expire time we can use EvictByTagAsync() method of IOutputCacheStore. this method take “Tag” and cancellation token as parameter and clear cache values.
so for clearing a cached value (values) we have to set Tag to policy. we can set it as follow:
to clear cache :
Custom cache policy
this API remove a person from collection and clear/flush cache by tag. now if we call Get API again we will get updated data. where is a lot of use cases that we cache data based on some parameter which are subset of collection. for example imagine we have author and article entities and each author can have multiple article as bellow:
Now we want cache each author and his/her articles by Author Id. in this situation Tag that we use before doesn’t work properly because it clear all cached values. at time of writing this article there is no any method or function introduced by OutputCache that can do it straight forward.so we have to create custom policy. to achieve this aim we must implement IOutputCachePolicy:
register it as follow:
and now usage is :
source code :