如何使用ASP.NET MVC 4.0 DonutOutputCache VaryByCustom是无效缓存 [英] How to invalidate cache using ASP.NET MVC 4.0 DonutOutputCache VaryByCustom

查看:186
本文介绍了如何使用ASP.NET MVC 4.0 DonutOutputCache VaryByCustom是无效缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用DevTrends.MvcDonutCaching包我的ASP.NET应用程序,它的伟大工程。有一个问题,我目前所面对的是VaryByCustom是无效缓存我设立了一个孩子的行动。

I'm using DevTrends.MvcDonutCaching package for my ASP.NET application and it works great. One problem that I have at the moment is with invalidating VaryByCustom cache I set up for a child action.

这是一些code,我有VaryByCustom是设置:

That's some code that I have for VaryByCustom setup:

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);
}

这就是我的行动是如何布置:

That's how my action is decorated:

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

这就是我试图清理缓存(我也试过它没有任何PARAMS和用户登陆,但这些都不工作:

And that's how I tried to clean up that cache (I also tried it without any params and with 'userlogin' but none of these worked:

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

这是剃刀视图部分:

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

任何帮助将非常AP preciated。

Any help will be much appreciated.

感谢。

推荐答案

这是为我工作使用OutputCacheManager的为removeItems 方法,而不是的removeItem 解决方案。这是一个有点更棘手,因为使用RouteValueDictionary需要。例如,以下为我工作:

Solution that worked for me is using OutputCacheManager's RemoveItems method instead of RemoveItem. It is a bit more tricky because requires using RouteValueDictionary. Example that worked for me below:

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

这篇关于如何使用ASP.NET MVC 4.0 DonutOutputCache VaryByCustom是无效缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆