ResponseCache
is somewhat a replacement for OutputCache
; however, I would like to do server side caching as well as per parameter input.
According to some answers here and here, I should be using IMemoryCache
or IDistributedCache
to do this. I'm particularly interested in caching on controllers where the parameter is different, previously done in asp.net 4 with OutputCache
and VaryByParam
like so:
[OutputCache(CacheProfile = "Medium", VaryByParam = "id", Location = OutputCacheLocation.Server)]
public ActionResult Index(long id)
{
///...
}
How would I go about replicating this in asp.net core?
If you want to change the cache by all request query parameters in all requests in the controller:
[ResponseCache(Duration = 20, VaryByQueryKeys = new[] { "*" })]
public class ActiveSectionController : ControllerBase
{
//...
}
First be sure you are using ASP.NET Core 1.1 or higher.
Then use a code similar to this on your controller method:
[ResponseCache(Duration = 300, VaryByQueryKeys = new string[] { "date_ref" } )]
public IActionResult Quality(DateTime date_ref)
Source: https://docs.microsoft.com/en-us/aspnet/core/performance/caching/middleware
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With