Cache-Control

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

 

通用的 Cache-Control 头是可以用在 http 请求和响应种来特别指定缓存策略的。缓存指定是单向的, 这意味着在请求里设置缓存头并不意味着响应头会包含同样的指令。

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

Syntax 语法

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

Cache request directives 缓存请求指令

客户端可以在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 response directives 缓存响应指令

客户端可以在响应中使用的标准 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>

Extension Cache-Control directives 拓展缓存指令

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

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

Directives 指令

Cacheability 可缓存

public
表示响应可以被任何缓存区缓存。
private
表示对于单个用户的响应,不能被共享缓存处理。私有的缓存区可以存储
no-cache
表示此请求或响应不能被缓存。在释放缓存的副本之前,强制将请求提交到源服务器进行验证。
only-if-cached
表示不要去获取新数据。客户端只希望获取缓存的响应,并且不去联系服务器去检查是否存在较新的副本。

Expiration 到期

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

No compatibility data found. Please contribute data for "Cache-Control" to the MDN compatibility data repository.

See also

文档标签和贡献者

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