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

Ant*_*onM 3 c# asp.net caching asp.net-mvc-4

我正在为我的 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);
}
Run Code Online (Sandbox Code Playgroud)

我的动作就是这样装饰的:

[Authorize]
[DonutOutputCache(Duration = 3600, VaryByCustom = "userlogin")]
public ActionResult UserProfile()
{ ... }
Run Code Online (Sandbox Code Playgroud)

这就是我试图清理该缓存的方式(我也尝试过没有任何参数和“用户登录”,但这些都不起作用:

OutputCacheManager om = new OutputCacheManager();
om.RemoveItem("Customer", "UserProfile", new { UserLogin = User.Identity.Name });
Run Code Online (Sandbox Code Playgroud)

那是剃刀视图部分:

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

任何帮助都感激不尽。

谢谢。

Ant*_*onM 5

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

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