为每个用户mvc配置输出缓存
我有一个用户特定的仪表板。 仪表板每天只会更改,我想使用MVC's
OutputCache
。 有没有办法配置每个用户的缓存,并在请求是新的一天到期?
我研究了这个,发现你可以扩展OutputCache
属性来动态设置你的持续时间,但是如何为每个用户配置?
提前致谢
在您的Web.config
:
在你的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
。
在web.config中试试这个,
........... ...........