如何使用ASP.NET MVC 4.0 DonutOutputCache VaryByCustom使缓存无效

时间:2013-10-05 09:48:29

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

我正在为我的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>

非常感谢任何帮助。

感谢。

1 个答案:

答案 0 :(得分:4)

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

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