google-chrome - 如果未设置过期和/或无缓存 header ,Google Chrome 浏览器会缓存资源多长时间?

标签 google-chrome caching no-cache

Chrome 在 Glassfish 服务器上缓存资源时遇到问题。尽管存在 Last-Modified header ,但不会发送过期和无缓存 header ,并且 Chrome 会缓存资源(大约 4 MB SWF 文件)。

有时 Chrome 会得到 304 代码,有时它只是得到 200(来自缓存)。我了解 304——Chrome 可能会检查最近的上次修改日期和缓存版本来决定。但其他时候它会执行 200(来自缓存),它不会返回任何 header 信息,并且 Chrome 似乎只是假设文件尚未被修改而不是进行检查。

Google's own site states以下内容:

HTTP/S supports local caching of static resources by the browser. Some of the newest browsers (e.g. IE 7, Chrome) use a heuristic to decide how long to cache all resources that don't have explicit caching headers.

但这并没有提供明确的答案。这个启发式是否在任何地方发布?我意识到可能没有固定的答案(例如 30 天),但一些一般准则会很有用。此外,如果设置了 Last-Modified,我不明白为什么 Chrome 不先检查一下。

最佳答案

浏览器认为缓存响应新鲜的时间通常是相对于它上次修改的时间而言的:

Since origin servers do not always provide explicit expiration times, a cache MAY assign a heuristic expiration time when an explicit time is not specified, employing algorithms that use other header field values (such as the Last-Modified time)... If the response has a Last-Modified header field (Section 2.2 of [RFC7232]), caches are encouraged to use a heuristic expiration value that is no more than some fraction of the interval since that time. A typical setting of this fraction might be 10%. [https://www.rfc-editor.org/rfc/rfc7234#section-4.2.2]

Chrome(和其他浏览器)如何计算该值的详细信息可以在源代码 ( An example from Chrome v49 ) 中找到。 Chrome 似乎还会计算相对于 Last-Modified header 的值。

( Credit to this post )

关于google-chrome - 如果未设置过期和/或无缓存 header ,Google Chrome 浏览器会缓存资源多长时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9740822/

相关文章:

uitableview - 使用 SSL 和 JAAS 时在 jBoss 的哪个位置设置了 Cache-Control 和 Pragma HTTP header ?

css - Chrome 设备模式模拟媒体查询不工作

html - Chrome 和 Firefox 上的 CSS3 Transitions 在 Optimus 或 Intel IGP 配置中表现不佳。怎么修?

javascript - HTML 在 Firefox 中无法正确呈现(在 Chrome 中工作得很好)

node.js - Elasticsearch索引/缓存未清除/卡住

SQL 查询返回 Apache Ignite 缓存的空结果

php - Dijkstra算法优化/缓存

http - no-cache 属性误解

javascript - 使用 Chrome 在 Javascript 中获取对象的真实位置

PHP - 将 session 缓存限制器设置为 nocache 或使用 jQuery ajax 缓存 :false? 是否更好