2011-03-03 57 views
5

我使用VaryByCustom建立在每个浏览器和每个用户为基础的输出缓存:如何在ASP.NET MVC中以每个用户为基础删除输出缓存?

[OutputCache(Duration = 6000, VaryByParam = "*", VaryByCustom="browser;userName")] 

(我重写GetVaryByCustomString(),使这项工作)

我需要如果可能的话,能够移除单个用户的输出缓存,而不会使不同用户的输出缓存失效。我已阅读HttpResponse.RemoveOutputCacheItem(),但通过删除基于路径的输出缓存起作用。有没有办法做到这一点基于VaryByCustom字符串?

回答

0

为什么不让用户在参数中,然后它是通过VaryByParam的每个用户。

+0

这将工作,但我不想添加一个否则不必要的查询字符串参数。 – 2011-03-03 22:24:35

0

也许使用

Response.Cache.SetVaryByCustom(string custom); 

在一个ActionFilter的,你可以建立包括浏览器版本和用户

+0

这就是我已经在做的事情。我需要的是能够为用户重置缓存,即使他们正在使用相同的浏览器。 – 2011-03-07 13:59:50

1

您可以通过覆盖HttpApplication.GetVaryByCustomString采取VaryByCustom属性的优势[OutputCache]和检查字符串HttpContext.Current.User.IsAuthenticated.

这是我将在Global.asax.cs文件中创建的内容:

public override string GetVaryByCustomString(HttpContext context, string custom) 
    { 
     if (custom == "UserName") 
     { 
      if (context.Request.IsAuthenticated) 
      { 
       return context.User.Identity.Name; 
      } 
      return null; 
     } 

     return base.GetVaryByCustomString(context, custom); 
    } 

然后在属性的OutputCache使用它:

[OutputCache(Duration = 10, VaryByParam = "none", VaryByCustom = "UserName")] 
public ActionResult Profiles() 
{ 
    //... 
} 

但要注意的是,用户名应该是在这种情况下,一成不变的!

相关问题