配置每个用户的MVC输出缓存 [英] Configure output cache per user mvc
问题描述
我有一个用户特定的仪表板。仪表板只会每天都在变化,我想用 MVC的
的OutputCache
。有什么办法来配置每用户缓存,当请求是新的一天到期?
I have a user specific dashboard. The dashboard will only change daily, I want to use MVC's
OutputCache
. Is there any way to configure the caching per user and to expire when the request is a new day?
我已经研究这个发现可以延长的OutputCache
属性但是动态地设置你的时间我该怎么配置这个每个用户?
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?
在此先感谢
推荐答案
在你的的Web.config
:
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<add name="Dashboard" duration="86400" varyByParam="*" varyByCustom="User" location="Server" />
</outputCacheProfiles>
</outputCacheSettings>
</caching>
在你的控制器
/ 动作
:
[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
让你写一个自定义的方法来确定是否会有一个缓存命中/缺失
In essence, GetVaryByCustomString
lets you write a custom method to determine whether there will be a Cache hit/miss
.
这篇关于配置每个用户的MVC输出缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!