本文共 3067 字,大约阅读时间需要 10 分钟。
http缓存
Caching is a technique that can help network connections be faster, because the less things need to be transferred, the better.
缓存是一种可以帮助网络连接更快的技术,因为需要传输的内容越少越好。
Many resources can be very large, and be very expensive in terms of time and also actual cost (on mobile, for example) to retrieve.
许多资源可能非常大,并且在时间和检索的实际成本(例如在移动设备上)方面都非常昂贵。
There are different caching strategies that are made available by HTTP and used by browsers.
HTTP提供了不同的缓存策略,浏览器使用了它们。
First, the Cache-Control
header can tell the browser to never use a cached version of a resource without first checking the ETag value (more on this later), by using the no-cache
value:
首先,通过使用no-cache
值, Cache-Control
标头可以告诉浏览器从不使用资源的缓存版本,而无需先检查ETag值(稍后会详细介绍):
Cache-Control: no-cache
A more restrictive no-store
option tells the browser (and all the intermediary network devices) the not even store the resource in its cache:
限制性更强的no-store
选项告诉浏览器(和所有中间网络设备)甚至不将资源存储在其缓存中:
Cache-Control: no-store
If Cache-Control
has the max-age
value, that’s used to determine the number of seconds this resource is valid as a cache:
如果Cache-Control
具有max-age
值,则该值用于确定此资源作为缓存有效的秒数:
Cache-Control: max-age=3600
Expires
标头 (The Expires
header)When an HTTP request is sent, the browser checks if it has a copy of that page in the cache, based on the URL you required.
发送HTTP请求后,浏览器会根据您所需的URL检查它是否在缓存中具有该页面的副本。
If there is, it checks the page for freshness.
如果存在,它将检查页面的新鲜度 。
A page is fresh if the value is less than the current datetime.
如果值小于当前日期时间,则页面是新鲜的。
The Expires header takes this form:
Expires标头采用以下形式:
Expires: Sat, 01 Dec 2018 16:00:00 GMT
There are different ways to perform a conditional get. All are based on using the If-*
request headers:
有多种执行条件获取的方法。 所有这些都基于使用If-*
请求标头:
using If-Modified-Since
and Last-Modified
使用If-Modified-Since
和Last-Modified
using If-None-Match
and ETag
使用If-None-Match
和ETag
If-Modified-Since
和Last-Modified
(Using If-Modified-Since
and Last-Modified
)The browser can send a request to the server and instead of just asking for the page, it adds an , based on the Last-Modified
header value it got from the currently cached page.
浏览器可以向服务器发送请求,而不仅仅是请求页面,它还基于从当前缓存的页面获取的Last-Modified
标头值添加 。
This tells the server to only return a response body (the page content) if the resource has been updated since that date.
这告诉服务器如果自该日期以来资源已更新,则仅返回响应正文(页面内容)。
Otherwise the server returns a 304 Not Modified
response.
否则,服务器将返回304 Not Modified
响应。
If-None-Match
和ETag
(Using If-None-Match
and ETag
)The Web Server (depending on the setup, how page are served, etc) can send an .
Web服务器(取决于设置,如何提供页面等)可以发送 。
That is the identifier of a resource. Every time the resource changes, for example it’s updated, the ETag should change as well.
那是资源的标识符。 每当资源更改(例如更新)时,ETag也应更改。
It’s like a checksum.
就像校验和。
The browser sends an that contains one (or more) ETag value.
浏览器发送一个 ,其中包含一个(或多个)ETag值。
If none match, the server returns the fresh version of the resource, otherwise a 304 Not Modified
response.
如果没有匹配项,则服务器返回资源的最新版本,否则返回304 Not Modified
响应。
翻译自:
http缓存
转载地址:http://otqgb.baihongyu.com/