http - Cache-Control : max-age=0 and no-cache?有什么区别

标签 http caching http-headers browser-cache cache-control

header Cache-Control: max-age=0 意味着内容被认为是陈旧的(并且必须立即重新获取),这实际上与 Cache 相同-控制:无缓存

最佳答案

我有同样的问题,并在我的搜索中找到了一些信息(您的问题作为结果之一出现)。这是我确定的...

Cache-Control header 有两个方面。一侧是 Web 服务器(又名“原始服务器”)可以发送的位置。另一端是浏览器(又名“用户代理”)可以发送它的地方。


源服务器发送时

我相信 max-age=0 只是告诉缓存(和用户代理)响应从一开始就过时了,所以他们应该重新验证响应(例如. 与 If-Not-Modified header )在使用缓存副本之前,而 no-cache 告诉他们他们必须在使用缓存副本之前重新验证缓存副本。来自 14.9.1 What is Cacheable :

no-cache

...a cache MUST NOT use the response to satisfy a subsequent request without successful revalidation with the origin server. This allows an origin server to prevent caching even by caches that have been configured to return stale responses to client requests.

换句话说,缓存有时可能会选择使用陈旧的响应(尽管我相信他们必须添加一个 Warning header ),但是 no-cache 说他们无论如何都不允许使用过时的响应。也许您希望在页面中生成棒球统计数据时使用SHOULD-revalidate 行为,但在生成响应时您希望使用MUST-revalidate 行为到电子商务购买。

虽然您在评论中说 no-cache 不应该阻止存储是正确的,但使用 no-cache 时实际上可能是另一个区别。我看到一个页面,Cache Control Directives Demystified ,这就是说(我不能保证它的正确性):

In practice, IE and Firefox have started treating the no-cache directive as if it instructs the browser not to even cache the page. We started observing this behavior about a year ago. We suspect that this change was prompted by the widespread (and incorrect) use of this directive to prevent caching.

...

Notice that of late, "cache-control: no-cache" has also started behaving like the "no-store" directive.

顺便说一句,在我看来 Cache-Control: max-age=0, must-revalidate 基本上应该与 Cache-Control: no-cache。所以也许这是一种获得 MUST-重新验证 no-cache 行为的方法,同时避免 no-cache 明显迁移到执行与 no-store 相同(即没有任何缓存)?


当由用户代理发送时

我相信shahkalpesh's answer适用于用户代理端。你也可以看看13.2.6 Disambiguating Multiple Responses .

如果用户代理发送带有 Cache-Control: max-age=0 的请求(也称为“端到端重新验证”),那么沿途的每个缓存都将重新验证其缓存条目(例如,使用 If-Not-Modified header )一直到原始服务器。如果回复是 304(未修改),则可以使用缓存的实体。

另一方面,发送带有 Cache-Control: no-cache 的请求(又名“端到端重新加载”)不会重新生效,服务器不得 响应时使用缓存副本。

关于http - Cache-Control : max-age=0 and no-cache?有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1046966/

相关文章:

java - Hibernate Session.multiLoad L2 缓存问题

c# - 为什么我必须将我的对象放入 HttpContext 缓存中两次才能永久保留它?

javascript - 如何为HTTP GET 请求设置header,并触发文件下载?

javascript - 除非文本框有文本,否则如何限制按钮

ios - 如何从给定的 NSHTTPURLResponse 获取 HTTP 协议(protocol)版本?

java - 为 HttpClient 请求转义 URL 中的 & 符号

angular - 基于数组 Angular 2 中 id 数量的动态 URL 参数

multithreading - MESI缓存协议(protocol)

c# - HTTP HEAD 响应 - 设置 Content-Length

http - Node JS 响应的正确内容类型