Cache-Control

这篇翻译不完整。请帮忙从英语翻译这篇文章

 Cache-Control 通用消息头被用于在http 请求和响应中通过指定指令来实现缓存机制。缓存指令是单向的, 这意味着在请求里设置的指令,在响应中不用包含相同的指令。

Header type  General header
Forbidden header name no
CORS-safelisted response-header yes

语法

指令不区分大小写,并且具有可选参数,可以用令牌或者带引号的字符串语法。多个指令以逗号分隔。

缓存请求指令

客户端可以在HTTP请求中使用的标准 Cache-Control 指令。

Cache-Control: max-age=<seconds>
Cache-Control: max-stale[=<seconds>]
Cache-Control: min-fresh=<seconds>
Cache-control: no-cache 
Cache-control: no-store
Cache-control: no-transform
Cache-control: only-if-cached

缓存响应指令

服务器可以在响应中使用的标准 Cache-Control 指令。

Cache-control: must-revalidate
Cache-control: no-cache
Cache-control: no-store
Cache-control: no-transform
Cache-control: public
Cache-control: private
Cache-control: proxy-revalidate
Cache-Control: max-age=<seconds>
Cache-control: s-maxage=<seconds>

 拓展Cache-Control指令

拓展缓存指令不是HTTP缓存标准的一部分,使用前请注意检查 兼容性

Cache-control: immutable 
Cache-control: stale-while-revalidate=<seconds>
Cache-control: stale-if-error=<seconds>

指令

可缓存性

public
表明响应可以被任何对象(包括:发送请求的客户端,代理服务器,等等)缓存。
private
表明响应只能被单个用户缓存,不能作为共享缓存(即代理服务器不能缓存它)。
no-cache
 强制所有缓存了该响应的缓存用户,在使用已存储的缓存数据前,发送带验证器的请求到原始服务器
only-if-cached
表明如果缓存存在,只使用缓存,无论原始服务器数据是否有更新。

到期

max-age=<seconds>
设置缓存存储的最大周期,超过这个时间缓存被认为过期(单位秒)。与Expires相反,时间是相对于请求的时间。
s-maxage=<seconds>
覆盖max-age 或者 Expires 头,但是仅适用于共享缓存(比如各个代理),并且私有缓存中它被忽略。
max-stale[=<seconds>]
表面客户端愿意接收一个已经过期的资源。 可选的设置一个时间(单位秒),表示响应不能超过的过时时间。
min-fresh=<seconds>
表示客户端希望在指定的时间内获取最新的响应。
stale-while-revalidate=<seconds>
...
stale-if-error=<seconds>
...

Revalidation and reloading 重新验证和重新加载

must-revalidate
The cache must verify the status of the stale resources before using it and expired ones should not be used.缓存必须在使用之前验证旧资源的状态,并且不可使用过期资源。
proxy-revalidate
Same as must-revalidate, but it only applies to shared caches (e.g., proxies) and is ignored by a private cache.与must-revalidate相同,但它仅适用于共享缓存(例如代理),并被私有缓存忽略。
immutable
Indicates that the response body will not change over time. The resource, if unexpired, is unchanged on the server and therefore the client should not send a conditional revalidation for it (e.g. If-None-Match or If-Modified-Since) to check for updates. Clients that aren't aware of this extension must ignore them as per the HTTP specification. In Firefox, immutable is only honored on https:// transactions. For more information, see also this blog post.

Other

no-store
The cache should not store anything about the client request or server response.
no-transform
No transformations or conversions should made to the resource. The Content-Encoding, Content-Range, Content-Type headers must not be modified by a proxy. A non- transparent proxy might, for example, convert between image formats in order to save cache space or to reduce the amount of traffic on a slow link. The no-transform directive disallows this.

Examples

Preventing caching 禁止缓存

To turn off caching, you can send the following directives. In addition, see also the Expires and Pragma headers.发送如下头可以关闭缓存。参考Expires 和 Pragma 头。

Cache-Control: no-cache, no-store, must-revalidate

Caching static assets 缓存静态资源

For the files in the application that will not change, you can usually add aggressive caching. This includes static files that are served by the application such as images, CSS files and JavaScript files, for example. In addition, see also the Expires header.对于程序中不会改变的文件,可以通过添加(主动)缓存。参考Expires头。

Cache-Control:public, max-age=31536000

Specifications 规范

Specification Title
RFC 7234 Hypertext Transfer Protocol (HTTP/1.1): Caching
RFC 5861 HTTP Cache-Control Extensions for Stale Content
draft-mcmanus-immutable-00 HTTP Immutable Responses

Browser compatibility

Feature Chrome Edge Firefox Internet Explorer Opera Safari Servo
Basic Support(Yes)(Yes)(Yes)(Yes)(Yes)(Yes)(Yes)
immutableNo supportNo support49.0No supportNo supportNo support1?
stale-while-revalidateNo support2No supportNo support3No supportNo supportNo supportNo support
stale-if-errorNo support2No supportNo support3No supportNo supportNo supportNo support
Feature Android Chrome for Android Edge Mobile Firefox for Android IE Mobile Opera Mobile Safari Mobile
Basic Support(Yes)(Yes)(Yes)(Yes)(Yes)(Yes)(Yes)
immutableNo supportNo supportNo supportNo supportNo supportNo supportNo support
stale-while-revalidateNo supportNo supportNo supportNo supportNo supportNo supportNo support
stale-if-errorNo supportNo supportNo supportNo supportNo supportNo supportNo support

1. See WebKit bug 167497.

2. See Chromium bug 348877.

3. See Bugzilla bug 995651.

See also

文档标签和贡献者

标签: 
 此页面的贡献者: fscholz, shaunsxj, paranoidjk, visten
 最后编辑者: fscholz,