【问题标题】:MVC 5 Homepage Output cache per User and Anonymous Guests每个用户和匿名访客的 MVC 5 主页输出缓存
【发布时间】:2016-05-07 14:50:05
【问题描述】:

作为 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();
 }

参考解决方案:Output cache per User

【问题讨论】:

    标签: c# asp.net outputcache asp.net-mvc-5


    【解决方案1】:

    您的方法依赖于用户的名称(通常映射到他们的给定名称)对所有用户都是唯一的,并且没有实际用户具有名称“Guest”。

    更好的方法是将其映射到该用户的应用程序内部 ID。

    if (arg == "User")
    {
        if (context.User.Identity.IsAuthenticated)
        { 
            return $"User={context.User.Identity.GetUserId()}";
        }
        else
        {
            return $"User={int.MinValue}";
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-15
      • 2017-10-03
      • 2011-01-06
      • 2019-01-24
      • 1970-01-01
      • 1970-01-01
      • 2010-10-27
      • 2014-02-08
      相关资源
      最近更新 更多