I am using the standard outputcache tag in my MVC app which works great but I need to force it to be dumped at certain times. How do I achieve this? The page that gets cached is built from a very simple route {Controller}/{PageName} - so most pages are something like this: /Pages/About-Us
我在我的MVC应用程序中使用标准的outputcache标签,它工作得很好,但我需要强制它在某些时候被转储。我该如何实现这一目标?缓存的页面是从一个非常简单的路径{Controller} / {PageName}构建的 - 所以大多数页面都是这样的:/ Pages / About-Us
Here is the output cache tag that is at the top of my .aspx view page just to be clear:
这是我的.aspx视图页面顶部的输出缓存标记,只是为了清楚:
<@ OutputCache Duration="100" VaryByParam="None" %>
So in another action on the same controller where content is updated I need to dump this cache, or even all of it - it's a very small app so not a big deal deal to dump all cached items.
因此,在内容更新的同一控制器上的另一个操作中,我需要转储此缓存,甚至全部 - 它是一个非常小的应用程序,因此转储所有缓存项目并不是一件大事。
4 个解决方案
#1
16
Be careful about using "None" vs. "".
使用“无”与“”时要小心。
- If you send "" then the HttpHeader for Vary is not sent.
- 如果您发送“”,则不会发送HttpHeader for Vary。
- If you send "None" then the HttpHeader for Vary is sent.
- 如果发送“无”,则发送HttpHeader for Vary。
I used Fiddler to verify this behavior.
我用Fiddler来验证这种行为。
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.
这似乎对浏览器是否返回服务器以检查最新版本(导致304)有影响。至少在Chrome中它确实如此。如果您确定在文件过期之前不想更新文件,则需要使用Varies =“”。
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.
我建议像我在这篇文章中所做的那样使用Varies =“”。对于我的javascript文件,我不希望浏览器返回并发出另一个Http请求,直到它过期。 304是不必要的。
#2
14
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)
HttpResponse.RemoveOutputCacheItem()可能是您要使用的方法。如果您可以确定缓存操作的名称,则可以仅删除特定操作(尝试设置断点或将缓存项的所有名称转储到屏幕)
Otherwise, I'd iterate through the entire output cache and just clear every item.
否则,我将遍历整个输出缓存并清除每个项目。
#3
6
Not knowing the difference between "None" and "" for the VaryByParam, I was using this attribute:
不知道VaryByParam的“无”和“”之间的区别,我使用的是这个属性:
[OutputCache(Location=OutputCacheLocation.ServerAndClient, Duration=int.MaxValue, VaryByParam="none")]
And this code to "fix" the Vary: *
problem:
这段代码“修复”了Vary:*问题:
this.Response.Cache.SetOmitVaryStar(true);
Which I found referenced at ASP.NET caching tests find a bug with VaryByParam
我发现在ASP.NET缓存测试中引用的是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).
设置为“Client”和“ServerAndClient”的OutputCache指令之间的区别在于“ServerAndClient”输出Vary字段。这会影响IE,因为IE无论如何都在发送请求。使用vary:*标头可以禁用所有客户端缓存(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:
删除vary:*标头的唯一方法是允许客户端缓存是通过代码:
#4
1
it seems that output cache doesn't put anything in HttpContent.Cache because when I loop through it the collection is empty:
似乎输出缓存没有在HttpContent.Cache中放置任何东西,因为当我遍历它时,集合是空的:
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
#1
16
Be careful about using "None" vs. "".
使用“无”与“”时要小心。
- If you send "" then the HttpHeader for Vary is not sent.
- 如果您发送“”,则不会发送HttpHeader for Vary。
- If you send "None" then the HttpHeader for Vary is sent.
- 如果发送“无”,则发送HttpHeader for Vary。
I used Fiddler to verify this behavior.
我用Fiddler来验证这种行为。
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.
这似乎对浏览器是否返回服务器以检查最新版本(导致304)有影响。至少在Chrome中它确实如此。如果您确定在文件过期之前不想更新文件,则需要使用Varies =“”。
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.
我建议像我在这篇文章中所做的那样使用Varies =“”。对于我的javascript文件,我不希望浏览器返回并发出另一个Http请求,直到它过期。 304是不必要的。
#2
14
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)
HttpResponse.RemoveOutputCacheItem()可能是您要使用的方法。如果您可以确定缓存操作的名称,则可以仅删除特定操作(尝试设置断点或将缓存项的所有名称转储到屏幕)
Otherwise, I'd iterate through the entire output cache and just clear every item.
否则,我将遍历整个输出缓存并清除每个项目。
#3
6
Not knowing the difference between "None" and "" for the VaryByParam, I was using this attribute:
不知道VaryByParam的“无”和“”之间的区别,我使用的是这个属性:
[OutputCache(Location=OutputCacheLocation.ServerAndClient, Duration=int.MaxValue, VaryByParam="none")]
And this code to "fix" the Vary: *
problem:
这段代码“修复”了Vary:*问题:
this.Response.Cache.SetOmitVaryStar(true);
Which I found referenced at ASP.NET caching tests find a bug with VaryByParam
我发现在ASP.NET缓存测试中引用的是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).
设置为“Client”和“ServerAndClient”的OutputCache指令之间的区别在于“ServerAndClient”输出Vary字段。这会影响IE,因为IE无论如何都在发送请求。使用vary:*标头可以禁用所有客户端缓存(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:
删除vary:*标头的唯一方法是允许客户端缓存是通过代码:
#4
1
it seems that output cache doesn't put anything in HttpContent.Cache because when I loop through it the collection is empty:
似乎输出缓存没有在HttpContent.Cache中放置任何东西,因为当我遍历它时,集合是空的:
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