Response caching in ASP.NET Core web API is a feature that allows you to cache the responses of your API endpoints based on HTTP cache headers. This can improve the performance and scalability of your API by reducing the load on the server and network.
To use response caching in ASP.NET Core web API, you need to do the following steps:
Add the Response Caching Middleware to your app by calling services.AddResponseCaching() in the ConfigureServices method and app.UseResponseCaching() in the Configure method of your Startup class.
Apply the ResponseCache attribute to your API controllers or actions and specify the parameters such as Duration, Location, VaryByHeader, etc. to control how the responses are cached.
Optionally, you can also use the ResponseCachingOptions class to configure some global settings for response caching, such as the maximum size of the cache, the default cache profile, etc.
For example, you can apply the ResponseCache
attribute to an API action like this:
[HttpGet]
[ResponseCache(Duration = 60, Location = ResponseCacheLocation.Any)]
public IEnumerable<WeatherForecast> Get()
{
// ...
}
This will cache the response of this action for 60 seconds and allow any cache (client, server, or proxy) to store it. It will also add the Cache-Control
header to the response with the value public,max-age=60
.
ResponseCacheLocation is an enum that specifies where the response may be cached. It has the following possible values1:
- Any: The response may be cached by any cache, including the client, a proxy, or the server.
- Client: The response may be cached only by the client.
- None: The response may not be cached by any cache.
- NoStore: The response may not be stored by any cache. This is equivalent to setting the no-store directive in the Cache-Control header.
Common Cache-Control
directives are shown in the following table.
Directive | Action |
---|---|
public | A cache may store the response. |
private | The response must not be stored by a shared cache. A private cache may store and reuse the response. |
max-age | The client doesn't accept a response whose age is greater than the specified number of seconds. Examples: max-age=60 (60 seconds), max-age=2592000 (1 month) |
no-cache | On requests: A cache must not use a stored response to satisfy the request. The origin server regenerates the response for the client, and the middleware updates the stored response in its cache. On responses: The response must not be used for a subsequent request without validation on the origin server. |
no-store | On requests: A cache must not store the request. On responses: A cache must not store any part of the response. |
Other cache headers that play a role in caching are shown in the following table.
Header | Function |
---|---|
Age | An estimate of the amount of time in seconds since the response was generated or successfully validated at the origin server. |
Expires | The time after which the response is considered stale. |
Pragma | Exists for backwards compatibility with HTTP/1.0 caches for setting no-cache behavior. If the Cache-Control header is present, the Pragma header is ignored. |
Vary | Specifies that a cached response must not be sent unless all of the Vary header fields match in both the cached response's original request and the new request.what is the difference between no-store and no-cache? the difference between no-store and no-cache is that:
So, if you want to prevent any caching of your response, you should use no-store. If you want to allow caching, but only with server validation, you should use no-cache. |