Expire Output Cache ASP.Net MVC

不羁的心 提交于 2019-11-28 03:52:11
Simon_Weaver

Be careful about using "None" vs. "".

  • If you send "" then the HttpHeader for Vary is not sent.
  • If you send "None" then the HttpHeader for Vary is sent.

I used Fiddler to verify this behavior.

This seems to have an impact on whether or not the browser goes back to the server to check for latest version (causing a 304). At least in Chrome it does. You want to use Varies="" if you know for sure you aren't going to want to update the file before it has expired.

I'd recommend using Varies="" as I did in this post. For my javascript file I dont want the browser going back and making another Http request until it has expired. 304 is unnecessary.

HttpResponse.RemoveOutputCacheItem() is probably the method you want to use. If you can figure out what name the actions are cached under, you can remove just the specific action (try setting a breakpoint or dumping all of the names of cached items to the screen)

Otherwise, I'd iterate through the entire output cache and just clear every item.

Not knowing the difference between "None" and "" for the VaryByParam, I was using this attribute:

[OutputCache(Location=OutputCacheLocation.ServerAndClient, Duration=int.MaxValue, VaryByParam="none")]

And this code to "fix" the Vary: * problem:

this.Response.Cache.SetOmitVaryStar(true);

Which I found referenced at ASP.NET caching tests find a bug with VaryByParam

The difference between a OutputCache directive set to "Client" and "ServerAndClient" is that "ServerAndClient" outputs the Vary field. This is impacting IE in that IE is sending requests regardless. The use of the vary:* header can disable all client caching (http://msdn2.microsoft.com/en-us/library/system.web.httpcachepolicy.setomitvarystar.aspx).

The only way to remove the vary:* header and thus allow client caching was through code:

it seems that output cache doesn't put anything in HttpContent.Cache because when I loop through it the collection is empty:

For Each elem As DictionaryEntry In HttpContext.Cache
  HttpContext.Cache.Remove(elem.Key)
Next

Here is my action attribute:

<OutputCache(Duration:=600, VaryByParam:="pagename")> _
Function Index(ByVal pagename As String) As ActionResult
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!