简体   繁体   English

为每个用户mvc配置输出缓存

[英]Configure output cache per user mvc

I have a user specific dashboard. 我有一个用户特定的仪表板。 The dashboard will only change daily, I want to use MVC's OutputCache . 仪表板每天只会更改,我想使用MVC's OutputCache Is there any way to configure the caching per user and to expire when the request is a new day? 有没有办法配置每个用户的缓存,并在请求是新的一天时到期?

I have researched this and found you can extend the OutputCache attribute to dynamically set your duration however how can I configure this per user? 我已经研究过这个,发现你可以扩展OutputCache属性来动态设置你的持续时间,但是如何为每个用户配置呢?

Thanks in advance 提前致谢

In your Web.config : 在您的Web.config

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

In your Controller / Action : 在你的Controller / Action

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

Then in your Global.asax : 然后在你的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);
}

In essence, GetVaryByCustomString lets you write a custom method to determine whether there will be a Cache hit/miss . 实质上, GetVaryByCustomString允许您编写自定义方法以确定是否存在Cache hit/miss

Try this in web.config, 在web.config中试试这个,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM