header Cache-Control: max-age=0意味着内容被认为是过期的(必须立即重新获取),这实际上与Cache-Control: no-cache是一样的。
当前回答
在我最近使用IE8和Firefox 3.5进行的测试中,两者似乎都符合rfc。但是,它们对源服务器的“友好性”有所不同。IE8对待无缓存响应的语义与max-age=0相同,必须重新验证。然而,Firefox 3.5似乎将无缓存等同于无存储,这在性能和带宽使用方面都很糟糕。
乌贼缓存,默认情况下,似乎从来没有存储任何无缓存头,就像Firefox。
我的建议是,对于您希望在每个请求上检查新鲜度的非敏感资源,设置public,max-age=0,但仍然允许缓存的性能和带宽优势。对于具有相同考虑的每个用户项目,使用private,max-age=0。
我会完全避免使用无缓存,因为它似乎已经被一些浏览器和流行缓存的功能等同为无存储。
此外,不要模仿Akamai和Limelight。虽然他们本质上把大量缓存数组作为主要业务,而且应该是专家,但他们实际上有既得利益,即从他们的网络下载更多的数据。谷歌可能也不是模拟的好选择。它们似乎根据资源随机使用max-age=0或no-cache。
其他回答
这在MDN文档中直接回答了缓存控制:
大多数HTTP/1.0缓存不支持无缓存指令,所以 历史上max-age=0被用作一种变通方法。但只有 Max-age =0可能会导致在缓存时重用一个陈旧的响应 与源服务器断开连接。必须重新验证才能解决这个问题。 这就是为什么下面的例子等同于无缓存。 Cache-Control: max-age=0,必须重新验证 但是现在,您可以简单地使用无缓存代替。
在MDN文档中也有缓存验证:
It is often stated that the combination of max-age=0 and must-revalidate has the same meaning as no-cache. Cache-Control: max-age=0, must-revalidate max-age=0 means that the response is immediately stale, and must-revalidate means that it must not be reused without revalidation once it is stale — so in combination, the semantics seem to be the same as no-cache. However, that usage of max-age=0 is a remnant of the fact that many implementations prior to HTTP/1.1 were unable to handle the no-cache directive — and so to deal with that limitation, max-age=0 was used as a workaround. But now that HTTP/1.1-conformant servers are widely deployed, there's no reason to ever use that max-age=0-and-must-revalidate combination — you should instead just use no-cache.
信息= 0
这相当于单击Refresh,这意味着给我最新的副本,除非我已经有了最新的副本。
no - cache
这是按住Shift同时点击刷新,这意味着,不管发生什么都要重做。
这是一个老问题了,但是如果有人像我一样通过搜索发现了这个问题,那么IE9似乎会在使用后退和前进按钮时利用这个来配置资源的行为。当使用max-age=0时,浏览器在向后/向前按下查看资源时将使用最近的版本。如果使用了无缓存,则会重新获取资源。
关于IE9缓存的更多细节可以在这篇msdn缓存博客文章中看到。
顺便说一下,值得注意的是,一些移动设备,特别是苹果产品,如iPhone/iPad完全忽略了诸如no-cache, no-store, Expires: 0或其他任何你可能试图强迫他们不要重用过期表单页面的头文件。
This has caused us no end of headaches as we try to get the issue of a user's iPad say, being left asleep on a page they have reached through a form process, say step 2 of 3, and then the device totally ignores the store/cache directives, and as far as I can tell, simply takes what is a virtual snapshot of the page from its last state, that is, ignoring what it was told explicitly, and, not only that, taking a page that should not be stored, and storing it without actually checking it again, which leads to all kinds of strange Session issues, among other things.
我只是为了防止有人不知道为什么他们会在iphone和ipad上出现会话错误,这似乎是迄今为止最严重的问题。
关于这个问题,我已经做了相当广泛的调试器测试,这是我的结论,设备完全忽略了这些指令。
即使在日常使用中,我发现一些手机也完全无法检查新版本,例如,过期:0,然后检查最近修改的日期,以确定是否应该获得一个新的版本。
它根本不会发生,所以我被迫做的是向我需要强制更新的css/js文件添加查询字符串,这欺骗了愚蠢的移动设备,认为这是一个它没有的文件,比如:my.css?V =1,然后V =2的css/js更新。这在很大程度上是有效的。
User browsers also, by the way, if left to their defaults, as of 2016, as I continuously discover (we do a LOT of changes and updates to our site) also fail to check for last modified dates on such files, but the query string method fixes that issue. This is something I've noticed with clients and office people who tend to use basic normal user defaults on their browsers, and have no awareness of caching issues with css/js etc, almost invariably fail to get the new css/js on change, which means the defaults for their browsers, mostly MSIE / Firefox, are not doing what they are told to do, they ignore changes and ignore last modified dates and do not validate, even with Expires: 0 set explicitly.
这是一个很好的帖子,包含了很多很好的技术信息,但同样重要的是要注意到这些东西在移动设备上的支持有多差。每隔几个月,我就必须增加更多的保护层,防止他们无法遵循他们收到的头命令,或者正确地解释这些命令。
One thing that (surprisingly) hasn't been mentioned is that a request can explicitly indicate that it will accept stale data, using the max-stale directive. In that case, if the server responded with max-age=0, the cache would merely consider the response stale, and would be free to use it to satisfy the client's request [which asked for potentially-stale data]. By contrast, if the server sends no-cache that really does trump any request by the client (with max-stale) for stale data, as the cache MUST revalidate.
推荐文章
- 什么是“升级-不安全-请求”HTTP报头?
- 向Angular HttpClient添加一个HTTP头并不会发送这个头,为什么?
- HTTP 301和308状态码有什么区别?
- 什么HTTP状态码应该用于错误的输入
- 编排microservices
- 如何使HTTP请求在PHP和不等待响应
- PATCH和PUT请求的主要区别是什么?
- 我可以把我所有的http://链接都改成//吗?
- 停止缓存jQuery .load响应
- URL为AJAX请求编码一个jQuery字符串
- 编译System.Net.HttpClient的查询字符串
- 摘要认证和基本认证的区别是什么?
- Axios -删除请求与请求体和头?
- 如何在http获取请求设置报头?
- 如何使用Ruby on Rails进行HTTP请求?