I'm using VaryByCustom
to create an output cache on a per-browser and per-user basis:
我正在使用VaryByCustom在每个浏览器和每个用户的基础上创建输出缓存:
[OutputCache(Duration = 6000, VaryByParam = "*", VaryByCustom="browser;userName")]
(I've overridden GetVaryByCustomString()
to make this work.)
(我已经重写了GetVaryByCustomString()以使其工作。)
I need to be able to remove a single user's output cache, without invalidating the output cache of different users, if possible. I've read about HttpResponse.RemoveOutputCacheItem()
, but that works by removing the output cache based on path. Is there any way to do this based on the VaryByCustom string?
我需要能够删除单个用户的输出缓存,如果可能的话,不会使不同用户的输出缓存失效。我已经阅读了HttpResponse.RemoveOutputCacheItem(),但这可以通过删除基于路径的输出缓存来实现。有没有办法根据VaryByCustom字符串执行此操作?
3 个解决方案
#1
1
You can take the advantage of VaryByCustom
property in [OutputCache]
by overriding HttpApplication.GetVaryByCustomString
and check HttpContext.Current.User.IsAuthenticated.
您可以通过覆盖HttpApplication.GetVaryByCustomString并检查HttpContext.Current.User.IsAuthenticated来利用[OutputCache]中的VaryByCustom属性。
This is what I will create in Global.asax.cs file:
这是我将在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);
}
And then use it in OutputCache attribute:
然后在OutputCache属性中使用它:
[OutputCache(Duration = 10, VaryByParam = "none", VaryByCustom = "UserName")]
public ActionResult Profiles()
{
//...
}
but be careful that the username should be immutable in this case!
但请注意,在这种情况下用户名应该是不可变的!
#2
0
Why not have the user in the parameters and then it would be per-user through the VaryByParam.
为什么不让用户参与参数,然后通过VaryByParam为每个用户。
#3
0
perhaps use
Response.Cache.SetVaryByCustom(string custom);
in an ActionFilter the you can build up the string including browser version and user
在ActionFilter中,您可以构建包括浏览器版本和用户的字符串
#1
1
You can take the advantage of VaryByCustom
property in [OutputCache]
by overriding HttpApplication.GetVaryByCustomString
and check HttpContext.Current.User.IsAuthenticated.
您可以通过覆盖HttpApplication.GetVaryByCustomString并检查HttpContext.Current.User.IsAuthenticated来利用[OutputCache]中的VaryByCustom属性。
This is what I will create in Global.asax.cs file:
这是我将在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);
}
And then use it in OutputCache attribute:
然后在OutputCache属性中使用它:
[OutputCache(Duration = 10, VaryByParam = "none", VaryByCustom = "UserName")]
public ActionResult Profiles()
{
//...
}
but be careful that the username should be immutable in this case!
但请注意,在这种情况下用户名应该是不可变的!
#2
0
Why not have the user in the parameters and then it would be per-user through the VaryByParam.
为什么不让用户参与参数,然后通过VaryByParam为每个用户。
#3
0
perhaps use
Response.Cache.SetVaryByCustom(string custom);
in an ActionFilter the you can build up the string including browser version and user
在ActionFilter中,您可以构建包括浏览器版本和用户的字符串