我如何知道“当前年龄”?缓存页面? [英] How can I tell the "current age" of a cached page?
问题描述
我想知道浏览器如何确定缓存的资源是否过期。
I'm wondering how the browser determines whether a cached resource has expired or not.
假设我已经设置了 max-age
标头为300。我在14:00发出了请求,三分钟后,我又对同一资源发出了请求。因此,浏览器如何判断资源尚未过期(当前年龄
小于180岁<< c $ c>最大年龄)?浏览器是否为每个请求的资源保留到期日期或当前年龄?如果是这样,我如何在发出请求时检查当前年龄?
Assume that I have set the max-age
header to 300. I made a request at 14:00, 3 minutes later I made another request to the same resource. So how can the browser tell the resource haven't expired (the current age
which is 180 is less than the max-age
)? Does the browser hold a "expiry date" or "current age" for every requested resource? If so how can I inspect the "current age" at the time I made the request?
推荐答案
检查哪些浏览器存储在其浏览器中缓存
要更好地了解浏览器缓存的工作方式,请检查浏览器实际存储在缓存中的内容:
Check what browsers store in their cache
To have a better understanding on how the browser cache works, check what the browsers actually store in their cache:
- Firefox:导航至
about:cache
。 - Chrome::导航到
chrome:// cache
。
- Firefox: Navigate to
about:cache
. - Chrome: Navigate to
chrome://cache
.
请注意,每个缓存条目(请求的URL)都有一个键。与该键相关联,您将找到整个响应详细信息(状态代码,标题和内容)。有了这些详细信息,浏览器便能够确定所请求资源的期限以及它是否过期。
Note that there's a key for each cache entry (requested URL). Associated with the key, you will find the whole response details (status codes, headers and content). With those details, the browser is able to determine the age of a requested resource and whether it's expired or not.
RFC 7234 ,这是HTTP / 1.1中缓存的当前参考。 ,向您介绍了有关缓存应如何工作的故事的大部分内容:
The RFC 7234, the current reference for caching in HTTP/1.1, tells you a good part of the story about how cache is supposed to work:
正确的缓存操作会保留 HTTP传输的语义
,同时消除了已保存在缓存中的
信息的传输。尽管缓存是HTTP的完全可选功能
,但是可以假定重用缓存的响应是
所希望的,并且当没有
要求或本地配置阻止它时,这种重用是默认行为。 [...]
Proper cache operation preserves the semantics of HTTP transfers while eliminating the transfer of information already held in the cache. Although caching is an entirely OPTIONAL feature of HTTP, it can be assumed that reusing a cached response is desirable and that such reuse is the default behavior when no requirement or local configuration prevents it. [...]
每个缓存条目由一个缓存键和一个或多个HTTP
响应组成,这些响应与使用相同键的先前请求相对应。
缓存条目的最常见形式是
检索请求的成功结果:即,对<$ c的 200
(OK)响应$ c> GET 请求,其中
包含由请求
目标。但是,也有可能
缓存永久重定向,否定结果(例如 404
(未找到)),
不完整的结果(例如 206
(部分内容)),以及对方法
方法的响应(如果该方法的定义允许此类缓存)
并定义了适合用作缓存键的内容。
Each cache entry consists of a cache key and one or more HTTP
responses corresponding to prior requests that used the same key.
The most common form of cache entry is a successful result of a
retrieval request: i.e., a 200
(OK) response to a GET
request, which
contains a representation of the resource identified by the request
target. However, it is also possible to
cache permanent redirects, negative results (e.g., 404
(Not Found)),
incomplete results (e.g., 206
(Partial Content)), and responses to
methods other than GET
if the method's definition allows such caching
and defines something suitable for use as a cache key.
主缓存键由请求方法和目标URI组成。
但是,由于当今常用的HTTP缓存通常限制
来缓存对 GET
的响应,因此许多缓存只是拒绝了其他方法
和仅将URI用作主缓存键。 [...]
The primary cache key consists of the request method and target URI.
However, since HTTP caches in common use today are typically limited
to caching responses to GET
, many caches simply decline other methods
and use only the URI as the primary cache key. [...]
定义了一些有关将响应存储在缓存中的规则:
Some rules are defined regarding storing responses in caches:
除非有以下内容,否则缓存不得存储对任何请求的响应:
A cache MUST NOT store a response to any request, unless:
-
请求方法被缓存理解,并定义为
可缓存,并且
The request method is understood by the cache and defined as being cacheable, and
响应状态代码被缓存理解,
the response status code is understood by the cache, and
no-store
缓存指令不会在请求或响应头字段中显示
,并且
the no-store
cache directive does not appear
in request or response header fields, and
private
响应指令如果共享了缓存,则
不会出现在响应中,并且
the private
response directive does not
appear in the response, if the cache is shared, and
授权
如果共享了缓存,则标头字段不会在请求中出现
,除非
响应明确允许它,并且
the Authorization
header field does
not appear in the request, if the cache is shared, unless the
response explicitly allows it, and
响应如下:
-
包含一个
Expires
标头字段,或
包含最大年龄
响应指令,或
包含 s-maxage
响应指令
并共享缓存,或者
contains a s-maxage
response directive
and the cache is shared, or
包含一个缓存控制扩展,该缓存控制扩展允许
对其进行缓存,或者
contains a Cache Control Extension that allows it to be cached, or
的状态代码为
包含一个 public
响应指令。
请注意,上面列出的任何要求都可以由缓存控制扩展覆盖。 [...]
Note that any of the requirements listed above can be overridden by a cache-control extension. [...]
这篇关于我如何知道“当前年龄”?缓存页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!