OutputCache/ResponseCache VaryByParam

Nic*_*eer 6 c# asp.net-caching asp.net-web-api asp.net-core-1.0

ResponseCache有点替代OutputCache; 但是,我想做服务器端缓存以及每个参数输入.

根据这里这里的一些答案,我应该使用IMemoryCache或者IDistributedCache这样做.我特别感兴趣的是在参数不同的控制器上进行缓存,之前在asp.net 4中使用过OutputCache,VaryByParam如下所示:

[OutputCache(CacheProfile = "Medium", VaryByParam = "id", Location = OutputCacheLocation.Server)]
public ActionResult Index(long id) 
{ 
    ///...
}
Run Code Online (Sandbox Code Playgroud)

我将如何在asp.net核心中复制这个?

小智 8

首先确保您使用的是 ASP.NET Core 1.1 或更高版本。

然后在您的控制器方法上使用类似于此的代码:

[ResponseCache(Duration = 300, VaryByQueryKeys = new string[] { "date_ref" } )]
public IActionResult Quality(DateTime date_ref)
Run Code Online (Sandbox Code Playgroud)

来源:https : //docs.microsoft.com/en-us/aspnet/core/performance/caching/middleware


小智 7

如果要通过控制器中所有请求中的所有请求查询参数更改缓存:

[ResponseCache(Duration = 20, VaryByQueryKeys = new[] { "*" })]
public class ActiveSectionController : ControllerBase
{
   //...
}
Run Code Online (Sandbox Code Playgroud)