为什么要为请求缓存控制HTTP标头? [英] Why cache-control HTTP header for requests?

查看:79
本文介绍了为什么要为请求缓存控制HTTP标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I recently went through this article. It says that not only response, the requests can also include cache-control options.

尽管我理解了它,服务器响应可以有意义地利用它,但我不清楚为什么请求中需要它们.不幸的是,我在互联网上找不到任何能解决我问题的答案.

Though I understood, it can be utilized by server responses meaningfully, I don't clearly understand why they are needed in requests. Unfortunately I could find nothing on internet which got answers to my problem.

有人有想法吗?

推荐答案

HTTP/1.1缓存和控制缓存行为的相关标头在

HTTP/1.1 cache and the associated headers that control cache behavior are defined in the RFC 7234. Caching is an entirely optional feature of HTTP though.

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

The Cache-Control header is used to specify directives for caches along the request/response chain. Such cache directives are unidirectional in that the presence of a directive in a request does not imply that the same directive is to be given in the 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 标头:

Find below the directives that can be used in the request for the 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. [...]

此指令使用参数语法的令牌形式:例如, max-age = 5 而不是 max-age ="5" .[...]

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. [...]

此指令使用参数语法的令牌形式:例如, max-stale = 10 而不是 max-stale ="10" .[...]

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. [...]

此指令使用参数语法的令牌形式:例如, min-fresh = 20 而不是 min-fresh ="20" .[...]

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 也定义了

In a similar way, RFC 7234 also defines the directives that can be used in the response for the Cache-Control header.

这篇关于为什么要为请求缓存控制HTTP标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆