什么是缓存配置文件?

时间:2016-09-23 19:26:27

标签: asp.net-mvc caching cacheprofile

我在从MSDN文档中阅读this article时遇到过它。我是关于缓存的新手,实际上,我读过有关缓存的第一篇文章。你能简单解释一下吗?

1 个答案:

答案 0 :(得分:9)

简而言之,您可以在web.config中设置一个缓存配置文件,而不必将设置应用于您希望使用缓存设置的每个操作或控制器:

在web.config中,您可以指定缓存配置文件的选项:

  <system.web>
    <compilation debug="true" targetFramework="4.5.1" />
    <httpRuntime targetFramework="4.5.1" />
    <caching>
      <outputCacheSettings>
        <outputCacheProfiles>
          <add name="cp1" duration="30" location="Any" varyByHeader="user-agent" />
        </outputCacheProfiles>
      </outputCacheSettings>
    </caching>
  </system.web>

然后,您可以在任何地方使用配置文件,例如。

[OutputCache(CacheProfile="cp1")]
public ActionResult Index() {
     //...
}

以上示例取自Apress Pro ASP.NET MVC 5 Platform by Adam Freeman。 我推荐它作为一个很好的阅读。