在 asp.net-mvc 中缓存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/385986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Caching in asp.net-mvc
提问by Boris Callens
I would like to cache my most database heavy actions in my asp.net-mvc site. In my research I have found
我想在我的 asp.net-mvc 站点中缓存我最重要的数据库操作。在我的研究中我发现
- donut cachingon Phil's blog
- Caching/compressingfilters on Kazi's blog
- Scott Hansleman's podcast about how they cached things in SO.
But I don't feel I get it yet.
I want to be able to cache my POST request depending on several pars. These pars are in an object. So I would like to cache the result of the following request:
但我觉得我还没有得到它。
我希望能够根据几个解析缓存我的 POST 请求。这些 pars 在一个对象中。所以我想缓存以下请求的结果:
public ActionResult AdvancedSearch(SearchBag searchBag)
Where searchBag is an object that holds (a bunch) of optional search parameters. My views themselves are light (as they should be), but the data access can be rather time consuming, depending on what fields are filled in in the search bag.
其中 searchBag 是一个包含(一堆)可选搜索参数的对象。我的视图本身很轻(应该如此),但数据访问可能相当耗时,具体取决于搜索包中填写的字段。
I have the feeling I should be caching on my datalayer, rather then on my actions.
How am I supposed to use the VaryByParam in the OutputCache attribute?
我觉得我应该缓存在我的数据层上,而不是我的行为上。
我应该如何在 OutputCache 属性中使用 VaryByParam?
采纳答案by Matthew
I like to cache in the model or data layer as well. This isolates everything to do with retrieving data from the controller/presentation. You can access the ASP.NET cache from System.Web.HttpContext.Current.Cacheor use the Caching Application Block from the Enterprise Library. Create your key for the cached data from the parameters for the query. Be sure to invalidate the cache when you update the data.
我也喜欢缓存在模型或数据层。这隔离了与从控制器/演示文稿检索数据有关的一切。您可以System.Web.HttpContext.Current.Cache从企业库中访问或使用缓存应用程序块访问 ASP.NET 缓存。根据查询参数为缓存数据创建密钥。更新数据时,请务必使缓存无效。
回答by Andrei R?nea
Or you can be independent of the HttpContext.Current and access Cache from HttpRuntime.Cache :)
或者你可以独立于 HttpContext.Current 并从 HttpRuntime.Cache 访问 Cache :)
回答by Haacked
Often, OutputCaching can be the most fast and efficient, but only when it meets your requirements. No point in having fast efficient if it's wrong! ;)
通常,OutputCaching 可能是最快速和最有效的,但前提是它满足您的要求。如果它是错误的,那么快速高效毫无意义!;)
In this case, it sounds like caching at the data layer is correct because you have complex caching needs. Sometimes, you can combine the two if the set of parameters which control what output is cached is simple.
在这种情况下,听起来在数据层缓存是正确的,因为您有复杂的缓存需求。有时,如果控制缓存输出的参数集很简单,则可以将两者结合起来。
回答by Nikki
you can use output caching something like this
你可以使用这样的输出缓存
[OutputCache(Duration = 10, VaryByParam = "empID")]
public ActionResult GetEmployeeDetail(int empID)
{
Employee e = new Employee();
return Content(e.getEmployeeDetails(empID));
}
or you can use cache profiles set it in web config
或者您可以使用缓存配置文件在 web 配置中设置它
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<add name="Admin"
duration="86420" varyByParam="none"/>
</outputCacheProfiles>
</outputCacheSettings>
</caching>
and use this tag
[OutputCache(CacheProfile="Admin")]

