作为DonutCache的替代方案,没有人看到以下输出缓存方法有任何问题。它似乎可以在不暴露任何用户数据的情况下正常工作,并且根据时间戳测试为每个人正确地缓存了页面,这是我最大的担忧。我只想介绍一下自己的基础,然后再在实时网站上实施它。
在Glogal.asax.cs中
public override string GetVaryByCustomString(HttpContext context, string arg)
{
if (arg == "User")
{
if (context.User.Identity.Name != "")
{
return "User=" + context.User.Identity.Name;
}
else
{
return "User=Guest";
}
}
return base.GetVaryByCustomString(context, arg);
}
在Web.config中
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<add name="HomePage" duration="86400" varyByParam="*" varyByCustom="User" location="Server" />
</outputCacheProfiles>
</outputCacheSettings>
</caching>
* n控制器
[OutputCache(CacheProfile = "HomePage")]
public ActionResult Index()
{
return View();
}
参考解决方案:每个用户的输出缓存
您的方法依赖于用户名(通常映射到其给定名称)对于所有用户而言都是唯一的,并且没有实际用户使用“来宾”这个名称。
更好的方法是将其映射到该用户的应用程序内部ID。
if (arg == "User")
{
if (context.User.Identity.IsAuthenticated)
{
return $"User={context.User.Identity.GetUserId()}";
}
else
{
return $"User={int.MinValue}";
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句