我的 asp.net mvc web 应用程序中的 OutputCache 设置.防止缓存的多种语法 [英] OutputCache setting inside my asp.net mvc web application. Multiple syntax to prevent caching
问题描述
我正在开发一个 asp.net MVC Web 应用程序,我需要知道在为我的操作方法定义 OutputCache 时是否有任何差异,如下所示:-
I am working on an asp.net MVC web application and I need to know if there are any differences when defining the OutputCache for my action methods as follow:-
[OutputCache(Duration = 0, Location = OutputCacheLocation.Client, VaryByParam = "*")]
VS
[OutputCache(NoStore = true, Duration = 0, Location="None", VaryByParam = "*")]
VS
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
以上三个设置都会阻止缓存数据,还是每个设置都有不同的含义?
Will all the above three setting prevent caching the data , or each on have different meaning ?
第二个问题定义 duration=0
和定义之间的主要区别是什么?NoStore=true
?他们都会阻止缓存吗?谢谢
Second question what is the main difference between defining duration=0
& NoStore=true
? will both of them prevent caching ?
Thanks
推荐答案
NoStore
属性用于通过设置 NoStore
来通知代理服务器和浏览器它们不应存储缓存内容的永久副本code>Cache-Control: no-store 在请求头中.
The NoStore
property is used to inform proxy servers and browser that they should not store a permanent copy of the cached content by setting Cache-Control: no-store
within the request header.
Duration 只是指定控制器动作的内容应该被缓存多长时间,例如10 秒.这会将 Cache-Control: max-age
设置为 >= 0.还将 Expires
标头设置为有效时间戳.
Duration simply specifies how long the content of the controller action should be cached, e.g. 10seconds. This will set the Cache-Control: max-age
to >= 0. And also sets the Expires
header to a valid timestamp.
对于您最初的问题,不,这三个变体的含义不同.
To your initial question, no, the three variations do not have the same meaning.
[OutputCache(Duration = 0, Location = OutputCacheLocation.Client, VaryByParam = "*")]
像这样创建一个缓存头
Cache-Control: private, max-age=0
Expires: Fri, 03 Jan 2014 12:32:15 GMT
<小时>
[OutputCache(NoStore = true, Duration = 0, Location="None", VaryByParam = "*")]
创建以下缓存头:
Cache-Control: no-cache, no-store
Pragma: no-cache
Expires: -1
这基本上就是你想通过一切手段防止缓存的情况.VaryByParam 是可选的(至少在 MVC5 中)并且无论如何默认值为*",因此您可以简单地使用 [OutputCache(NoStore = true, Location = OutputCacheLocation.None)]
代替.
This is basically what you want to see if you want to prevent caching by all means. VaryByParam is optional (at least in MVC5) and the default is "*" anyways, so you can simply use [OutputCache(NoStore = true, Location = OutputCacheLocation.None)]
instead.
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
甚至创建了一个公共缓存控件...
even creates a public cache control...
Cache-Control: public, no-store, max-age=0
Expires: Fri, 03 Jan 2014 12:36:38 GMT
SO 上有一篇很好的帖子,讨论了 max-age=0 和 no-cache 等.
There is a good post on SO which discusses the difference between max-age=0 and no-cache etc..
最后,这三个可能会阻止缓存您的数据,但仍然具有不同的含义.
At the end all three might prevent caching your data but still have different meanings.
这篇关于我的 asp.net mvc web 应用程序中的 OutputCache 设置.防止缓存的多种语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!