【问题标题】:Configure output cache per user mvc为每个用户 mvc 配置输出缓存
【发布时间】:2014-08-07 01:03:32
【问题描述】:

我有一个用户特定的仪表板。仪表板只会每天更改,我想使用MVC's OutputCache。有没有办法配置每个用户的缓存并在请求是新的一天时过期?

我对此进行了研究,发现您可以扩展 OutputCache 属性以动态设置您的持续时间,但是如何为每个用户配置它?

提前致谢

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-3


    【解决方案1】:

    在你的Web.config:

    <caching>
      <outputCacheSettings>
        <outputCacheProfiles>
          <add name="Dashboard" duration="86400" varyByParam="*" varyByCustom="User" location="Server" />
        </outputCacheProfiles>
      </outputCacheSettings>
    </caching>
    

    在你的Controller/Action:

    [OutputCache(CacheProfile="Dashboard")]
    public class DashboardController { ...}
    

    然后在你的Global.asax:

    //string arg filled with the value of "varyByCustom" in your web.config
    public override string GetVaryByCustomString(HttpContext context, string arg)
    {
        if (arg == "User")
            {
            // depends on your authentication mechanism
            return "User=" + context.User.Identity.Name;
            //return "User=" + context.Session.SessionID;
            }
    
        return base.GetVaryByCustomString(context, arg);
    }
    

    本质上,GetVaryByCustomString 让您编写自定义方法来确定是否会有 Cache hit/miss

    【讨论】:

    • 这个概念是否适用于 DevTrends DonutCaching?
    • 没试过,但因为他们也提供了VaryByCustom 机制。我认为答案是肯定的。
    【解决方案2】:

    在 web.config 中试试这个,

    <system.web>
     ...........
     ...........
     <caching>
      <outputCacheSettings>
        <outputCacheProfiles>
          <add name="UserCache" duration="1440" varyByParam="UserID" enabled="true" location="Client"/>
        </outputCacheProfiles>
      </outputCacheSettings>
    </caching>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-09
      • 1970-01-01
      • 2011-01-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多