Introduction
The process by which a browser or other client caches a server's response is known as response caching. This will facilitate the prompt processing of subsequent requests for the same resources. Furthermore, the server won't have to process and produce the same response repeatedly thanks to this.
The response caching headers are set by ASP.NET Core using the ResponseCache attribute. Additionally, we can manage the caching behavior from the server side by utilizing the Response Caching Middleware. Clients and other proxies can read the response caching headers once we've set them up to figure out how to cache the server response. Caching headers are required for browsers, clients, and proxies in accordance with the HTTP 1.1 Response Cache Specification.
Cache Profiles
We can set up cache profiles and use them throughout the application rather than repeating the response cache settings on different controller action methods. The application can use the cache profile's values as defaults for the ResponseCache attribute once it has been set up. Naturally, by defining the attributes on the attribute, we can override the defaults.
Within the Program class, we can define a cache profile.
builder.Services.AddControllers(options =>
{
options.CacheProfiles.Add("Default", new CacheProfile
{
Duration = 60,
Location = ResponseCacheLocation.Any
});
});
Here, we define a new cache profile named Default, whose location is set to public and whose duration is set to two minutes.
This cache profile can now be applied to any controller or endpoint.
[HttpGet("default")]
[ResponseCache(CacheProfileName = "Default")]
public IActionResult Default()
{
return Ok($"Default response was generated, {DateTime.Now}");
}
This will cause the response's defined cache-control settings to be applied.
cache-control: public,max-age=60
We can define multiple cache profiles in the app settings file and make the response cache settings configurable, avoiding hard-coding the cache settings in the Program class.
"CacheProfiles": {
"Cache5Mins": {
"Duration": 600,
"Location": "Any"
},
"CacheVaryByHeader": {
"Duration": 60,
"Location": "Any",
"VaryByHeader": "User-Agent"
}
}
The ConfigurationManager class can then be used to read the cache profiles in the Program class.
builder.Services.AddControllers(options =>
{
var cacheProfiles = builder.Configuration
.GetSection("CacheProfiles")
.GetChildren();
foreach (var cacheProfile in cacheProfiles)
{
options.CacheProfiles
.Add(cacheProfile.Key, cacheProfile.Get<CacheProfile>());
}
});
This approach to defining cache profiles is far superior, particularly if our application requires the definition of multiple cache profiles.
Let’s run the application and validate the response cache.
The above screenshot is for the default response cache profile; the default value is 60 (1 min) until that response is the same; after that duration, the response is changed.
The above screenshot is for the cache vary by header response cache profile; the default value is 60 (1 min), and the vary by header property value is User-Agent until that response is the same; after that duration, the response is changed.
Recall that although Response Caching can greatly enhance the performance of your application, it might not be appropriate in every situation. When content is changing quickly, or user-specific data is being served that shouldn't be cached, caching can result in incorrect or undesirable behavior.
We learned the new technique and evolved together.
Happy coding!