为什么 cache-control HTTP header 用于请求?

Why cache-control HTTP header for requests?

我最近浏览了 this 篇文章。它说不仅响应,请求还可以包括 cache-control 选项。

虽然我明白,它可以被服务器响应有意义地利用,但我不清楚为什么在请求中需要它们。不幸的是,我在互联网上找不到任何问题的答案。

有人有想法吗?

HTTP/1.1 缓存和控制缓存行为的关联 header 在 RFC 7234 中定义。不过,缓存是 HTTP 的一项完全可选的功能。


Cache-Control header 用于指定 指令 request/response 链上的缓存。这样的缓存指令是 单向的,因为请求中存在指令并不意味着响应中会给出相同的指令。

Cache directives are identified by a token, to be compared case-insensitively, and have an optional argument, that can use both token and quoted-string syntax. For the directives defined below that define arguments, recipients ought to accept both forms, even if one is documented to be preferred. For any directive not defined by this specification, a recipient MUST accept both forms.

Cache-Control   = 1#cache-directive

cache-directive = token [ "=" ( token / quoted-string ) ]

在下面找到可用于 Cache-Control header 请求的 指令 :

5.2.1.1. max-age

[...] The max-age request directive indicates that the client is unwilling to accept a response whose age is greater than the specified number of seconds. [...]

This directive uses the token form of the argument syntax: e.g., max-age=5 not max-age="5". [...]

5.2.1.2. max-stale

[...] The max-stale request directive indicates that the client is willing to accept a response that has exceeded its freshness lifetime. [...]

This directive uses the token form of the argument syntax: e.g., max-stale=10 not max-stale="10". [...]

5.2.1.3. min-fresh

[...] The min-fresh request directive indicates that the client is willing to accept a response whose freshness lifetime is no less than its current age plus the specified time in seconds. [...]

This directive uses the token form of the argument syntax: e.g., min-fresh=20 not min-fresh="20". [...]

5.2.1.4. no-cache

The no-cache request directive indicates that a cache MUST NOT use a stored response to satisfy the request without successful validation on the origin server.

5.2.1.5. no-store

The no-store request directive indicates that a cache MUST NOT store any part of either this request or any response to it. [...]

5.2.1.6. no-transform

The no-transform request directive indicates that an intermediary (whether or not it implements a cache) MUST NOT transform the payload [...].

5.2.1.7. only-if-cached

The only-if-cached request directive indicates that the client only wishes to obtain a stored response. [...]

以类似的方式,RFC 7234 还定义了 指令 可用于 Cache-Control header 的响应。