【问题标题】:How to invalidate cache using ASP.NET MVC 4.0 DonutOutputCache VaryByCustom如何使用 ASP.NET MVC 4.0 DonutOutputCache VaryByCustom 使缓存无效
【发布时间】:2013-10-12 08:13:04
【问题描述】:

我正在为我的 ASP.NET 应用程序使用 DevTrends.MvcDonutCaching 包,它运行良好。我目前遇到的一个问题是我为子操作设置的 VaryByCustom 缓存无效。

这是我用于 VaryByCustom 设置的一些代码:

public override string GetVaryByCustomString(HttpContext context, string arg)
{
  if (arg == "userlogin" && context.User.Identity.IsAuthenticated)
  {
     return "UserLogin=" + context.User.Identity.Name;
  }

  return base.GetVaryByCustomString(context, arg);
}

这就是我的动作的装饰方式:

[Authorize]
[DonutOutputCache(Duration = 3600, VaryByCustom = "userlogin")]
public ActionResult UserProfile()
{ ... }

这就是我尝试清理缓存的方式(我也尝试过不带任何参数并使用 'userlogin' 但这些都不起作用:

OutputCacheManager om = new OutputCacheManager();
om.RemoveItem("Customer", "UserProfile", new { UserLogin = User.Identity.Name });

那是剃刀视图部分:

<div id="cabinetMain">
 @{Html.RenderAction("UserProfile", true);}
</div>

任何帮助将不胜感激。

谢谢。

【问题讨论】:

    标签: c# asp.net asp.net-mvc-4 caching


    【解决方案1】:

    对我有用的解决方案是使用 OutputCacheManager 的 RemoveItems 方法而不是 RemoveItem。这有点棘手,因为需要使用 RouteValueDictionary。下面对我有用的示例:

    OutputCacheManager om = new OutputCacheManager();
    RouteValueDictionary rv = new RouteValueDictionary();                    
    rv.Add("userlogin", User.Identity.Name);
    om.RemoveItems("Customer", "UserProfile", rv);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-15
      • 1970-01-01
      • 2012-10-14
      • 1970-01-01
      • 2015-09-01
      • 2014-02-13
      相关资源
      最近更新 更多