使用VaryByCustom使缓存无效

Sha*_*ain 5 c# caching cache-invalidation asp.net-mvc-4

我想使用VaryByCustom属性使缓存无效.以下代码用于缓存设置.

public override string GetVaryByCustomString(HttpContext context, string arg)
{
    if (!string.IsNullOrWhiteSpace(arg))
    {
        if (context.User.Identity.Name != null)
        {
            return context.User.Identity.Name;
        }
    }
    return base.GetVaryByCustomString(context, arg);
}
Run Code Online (Sandbox Code Playgroud)

Vla*_*žić 1

你是这样使用的吗:

确保你用属性来装饰你的动作:

 [OutputCache(Duration = 3600, VaryByParam = "*", Location = OutputCacheLocation.Server, VaryByCustom = 
  "YourStringThatIsArgParamInYourGlobalAsaxMethod")]
  public ActionResult MyActionOnTheController(string myParam)
 {
 }
Run Code Online (Sandbox Code Playgroud)