我正在开发一个asp.net MVC Web应用程序,我需要知道在为我的操作方法定义OutputCache时是否存在任何差异,如下所示: -
[OutputCache(Duration = 0, Location = OutputCacheLocation.Client, VaryByParam = "*")]
VS
[OutputCache(NoStore = true, Duration = 0, Location="None", VaryByParam = "*")]
VS
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
以上三种设置都会阻止缓存数据,或者每种设置都有不同的含义吗?
第二个问题定义duration=0
&之间的主要区别是什么NoStore=true
?他们俩会阻止缓存吗?谢谢
该NoStore
属性用于通知代理服务器和浏览器,它们不应通过Cache-Control: no-store
在请求标头内设置来存储缓存内容的永久副本.
持续时间只是指定应该缓存控制器动作的内容多长时间,例如10秒.这将设置Cache-Control: max-age
为> = 0.并且还将Expires
标头设置为有效的时间戳.
对于您的初始问题,不,这三种变体的含义不同.
[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)]
.
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
甚至创建一个公共缓存控件......
Cache-Control: public, no-store, max-age=0 Expires: Fri, 03 Jan 2014 12:36:38 GMT
在SO上有一篇很好的文章讨论了max-age = 0和no-cache等之间的区别.
最后,这三个可能会阻止缓存您的数据,但仍然有不同的含义.