OutputCache is sending wrong Vary header when the call hits the cache

前端 未结 3 2249
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-20 06:59

I have an action method that I want to cache:

[OutputCache(Duration=60*5, Location=OutputCacheLocation.Any, VaryByCustom=\"index\")]
public ActionResult Index()
         


        
3条回答
  •  有刺的猬
    2021-02-20 07:45

    I am using a custom cache provider and in this case there is a simple solution for this. On the BeginRequest, based on the user authentication status, we set a context information to not run cache:

    HttpContext.Current.Items["NoCache"] = "1";
    

    And then on our GetVaryBy method we return null if this information is set:

    public override string GetVaryByCustomString(HttpContext context, string custom)
    {
        if (HttpContext.Current.Items["NoCache"] != null)
            return null;
    
        // remaining code here
    }
    

    And then on the cache methods, we can test the same. For instance:

    public override object Add(string key, object entry, DateTime utcExpiry)
    {
        if (HttpContext.Current.Items["NoCache"] != null)
            return null;
    
        // remaining code here
    }
    

提交回复
热议问题