C# 按用户配置输出缓存mvc
我有一个特定于用户的仪表板。仪表板只会每天更改,我想使用C# 按用户配置输出缓存mvc,c#,asp.net-mvc,asp.net-mvc-3,C#,Asp.net Mvc,Asp.net Mvc 3,我有一个特定于用户的仪表板。仪表板只会每天更改,我想使用MVC的OutputCache。是否有任何方法可以配置每个用户的缓存,并在请求是新的一天时过期 我对此进行了研究,发现您可以扩展OutputCache属性来动态设置持续时间,但是如何配置每个用户的持续时间 提前感谢在web.config中尝试此功能 <system.web> ........... ........... <caching> <outputCacheSettings> &
MVC的OutputCache
。是否有任何方法可以配置每个用户的缓存,并在请求是新的一天时过期
我对此进行了研究,发现您可以扩展OutputCache
属性来动态设置持续时间,但是如何配置每个用户的持续时间
提前感谢在web.config中尝试此功能
<system.web>
...........
...........
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<add name="UserCache" duration="1440" varyByParam="UserID" enabled="true" location="Client"/>
</outputCacheProfiles>
</outputCacheSettings>
</caching>
...........
...........
在Web.config
中:
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<add name="Dashboard" duration="86400" varyByParam="*" varyByCustom="User" location="Server" />
</outputCacheProfiles>
</outputCacheSettings>
</caching>
然后在您的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
允许您编写一个自定义方法来确定是否会有缓存hit/miss
相同的概念是否适用于DevTrends DonutCaching?没有尝试过,但因为它们也提供了VaryByCustom
机制。我想答案是肯定的。
//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);
}