【问题标题】:MVC4 View cache duration in config file?MVC4在配置文件中查看缓存持续时间?
【发布时间】:2013-07-02 18:45:26
【问题描述】:

是否可以在 web.config 中为 MVC4 .net 页面设置缓存的持续时间?我有:

[OutputCache(Duration = Convert.ToInt32(ConfigurationManager.AppSettings["cache.eventPage"]), VaryByParam = "Id")]
public ActionResult....

不会编译,因为

属性参数必须是属性参数类型的常量表达式、typeof表达式或数组创建表达式

我们的流量非常高,希望能够在不推出新版本的情况下快速更改此值。这可能吗?

【问题讨论】:

    标签: asp.net asp.net-mvc-4 caching output-caching


    【解决方案1】:

    您可以使用OutputCache profiles;在 web.config 中定义一个部分

    <system.web>
     <caching>
      <outputCacheSettings>
        <outputCacheProfiles>
           <add name="CacheProfile1" duration="10" />  <!--10 seconds -->
           <add name="CacheProfile2" duration="3600" /> <!--one hour-->
           <add name="CacheProfileNone" duration="0" /> <!--disabled-->
        </outputCacheProfiles>
      </outputCacheSettings>
     </caching>
    </system.web>
    

    正如您已经完成的那样,通过属性在您的控制器操作方法上使用它。只需使用 CacheProfile 属性即可。

    [OutputCache(CacheProfile = "CacheProfile1",VaryByParam = "Id")]
    

    您可以为您拥有的每个缓存方案创建不同的配置文件。

    More info on caching at MSDN

    【讨论】:

    • 这就是我最终的做法。我想知道是否有更程序化的方式来访问这些值。不过,这可能是唯一的答案。
    • 明天将为此颁发赏金,为我节省了很多时间。
    猜你喜欢
    • 2011-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-11
    • 1970-01-01
    • 2011-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多