MVC3定制的OutputCache [英] MVC3 custom outputcache
问题描述
我想在我的应用程序中使用的缓存,但我返回的数据是特定于用户登录。当我需要用户改变,我不能使用任何现成的缓存规则。
I'd like to use caching in my application but the data I'm returning is specific to the logged in user. I can't use any of the out of the box caching rules when I need to vary by user.
有人能指出我在正确的方向上创建自定义缓存属性。 或者说,我在控制器构造函数中初始化的私人控制器成员 _user $;从控制器我可以从
Thread.CurrentPrincipal.Identity访问用户C $ C>
Can someone point me in the right direction on creating a custom caching attribute. From the controller I can access the user from Thread.CurrentPrincipal.Identity;
or a private controller member that I initialize in the controller constructor _user
感谢您。
推荐答案
您可以使用VaryByCustom是。在Global.asax中重写 GetVaryByCustomString
方法:
You could use the VaryByCustom. In Global.asax override the GetVaryByCustomString
method:
public override string GetVaryByCustomString(HttpContext context, string arg)
{
if (arg == "IsLoggedIn")
{
if (context.Request.Cookies["anon"] != null)
{
if (context.Request.Cookies["anon"].Value == "false")
{
return "auth";
}
else
{
return "anon";
}
}
else
{
return "anon";
}
}
else
{
return base.GetVaryByCustomString(context, arg);
}
}
和再使用的OutputCache属性:
and then use the OutputCache attribute:
[OutputCache(CacheProfile = "MyProfile")]
public ActionResult Index()
{
return View();
}
和在web.config中:
and in web.config:
<caching>
<outputcachesettings>
<outputcacheprofiles>
<clear />
<add varybycustom="IsLoggedIn" varybyparam="*" duration="86400" name="MyProfile" />
</outputcacheprofiles>
</outputcachesettings>
</caching>
这篇关于MVC3定制的OutputCache的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!