http - 为什么要为请求缓存控制 HTTP header ?

标签 http httpclient cache-control httpserver http-caching

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

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

有人有想法吗?

最佳答案

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


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

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 ) ]

directives 下面找到可用于 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还定义了 directives可用于 Cache-Control 的响应中标题。

关于http - 为什么要为请求缓存控制 HTTP header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47507679/

相关文章:

angularjs - Nodejs通过id获取用户

java - (HTTPLog)-静态 : isSBSettingEnabled false error and app is closing

c# - 修改每个请求的请求 header C# HttpClient PCL

Android - 远程登录成功但不保留登录状态

javascript - 从回调中创建 XMLHttpRequest 如何影响缓存?

asp.net - ScriptResource.axd 请求的特定缓存问题

c++ - Poco::HttpClientSession.receiveResponse() 在没有任何明显原因的情况下抛出 NoMessageException

java - 从浏览器上传多个文件的最佳方式

c# - httpclient post嵌套字典

asp.net - Web.config 客户端缓存设置不适用于由 HttpContext.RewritePath 重写的路径