我在用着VaryByCustom
在每个浏览器和每个用户的基础上创建输出缓存:
[OutputCache(Duration = 6000, VaryByParam = "*", VaryByCustom="browser;userName")]
(我已经重写了GetVaryByCustomString()
使这项工作成功。)
如果可能的话,我需要能够删除单个用户的输出缓存,而不使不同用户的输出缓存无效。我读过关于HttpResponse.RemoveOutputCacheItem()
,但这可以通过根据路径删除输出缓存来实现。有没有办法根据 VaryByCustom 字符串来做到这一点?
您可以利用VaryByCustom
财产在[OutputCache]
通过覆盖HttpApplication.GetVaryByCustomString
并检查HttpContext.Current.User.IsAuthenticated.
这是我将在 Global.asax.cs 文件中创建的内容:
public override string GetVaryByCustomString(HttpContext context, string custom)
{
if (custom == "UserName")
{
if (context.Request.IsAuthenticated)
{
return context.User.Identity.Name;
}
return null;
}
return base.GetVaryByCustomString(context, custom);
}
然后在 OutputCache 属性中使用它:
[OutputCache(Duration = 10, VaryByParam = "none", VaryByCustom = "UserName")]
public ActionResult Profiles()
{
//...
}
但请注意,在这种情况下用户名应该是不可变的!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)