簡體   English   中英

如何使用ASP.NET MVC 4.0 DonutOutputCache VaryByCustom使緩存無效

[英]How to invalidate cache using ASP.NET MVC 4.0 DonutOutputCache VaryByCustom

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

任何幫助都感激不盡。

謝謝。

對我有用的解決方案是使用OutputCacheManager的RemoveItems方法而不是RemoveItem 這有點棘手,因為需要使用RouteValueDictionary。 下面為我​​工作的示例:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM