- ASP.NET Core MVC
- ASP.NET Core logging with Serilog
- ASP.NET Core managed config
- ASP.NET Core MVC with /api/-area
- ASP.NET Core API versioning
- ASP.NET Core API with Swagger UI
- ASP.NET Core EF in separate project
- ASP.NET Core AutoMapper
- ASP.NET Core response cache
- ASP.NET Core memory cache
- ASP.NET Core TypeScript
ASP.NET Core comes with built-in support for response caching. Unlike cache-control header that tells client/proxies how long to cache data, the response cache will cache output of an action.
The advantage of caching is that you can decrease the amount of expensive operations on the server by caching the result.
Install
1 |
Install-Package Microsoft.AspNetCore.ResponseCaching |
Use
The simplest form of this caching is to decorate an action with the ResponseCache attribute. Duration says how many seconds to cache the output.
1 2 3 4 5 6 |
[HttpGet] [ResponseCache(Duration = 10)] public string CurrentTime() { return DateTime.Now.ToString(); } |
Note that the output may vary depending on HTTP headers, query variables, or post data. If this is the case then you need to specify so. For example:
1 2 3 4 5 6 |
[HttpGet] [ResponseCache(Duration = 10, VaryByQueryKeys = new string[] { "SessionId" }, VaryByHeader = "User-Agent")] public string CurrentDate(string sessionId) { return DateTime.Now.ToString(); } |
Response caching supports much more complex scenarios.