本文介绍了用户代理是否可以在其请求中将max-age设置为大于零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读Http缓存 rfc后,我对 max-age 行为有疑问.

I have a doubt about max-age behaviour after reading the Http Cache rfc.

场景:

用户代理

GET /foo

原始服务器响应标头

cache-control: max-age=120

服务器告诉用户代理,请求的资源应在2分钟后重新验证.

Server tells user agent that the resource requested should be revalidated after 2 minutes.

1分钟零几秒钟后,用户代理再次发出请求,并指定1分钟的 max-age :

After 1 minute and few seconds, User agent makes another request, specifying a max-age of 1 minute:

用户代理

cache-control: max-age=60
GET /foo

据我了解,此请求应绕过用户代理缓存.
为什么?
尽管Origin Server告诉客户端应该将资源缓存2分钟,但是用户代理需要的资源最多只能使用1分钟( max-age = 60 ).
从第一个 GET 开始经过1分钟零几秒钟之后,该资源无效(从User Agent的角度来看),并且请求应直接发送到原始服务器(或任何其他缓存层).

From what I understand, this request should bypass the user agent cache.
Why?
Although the Origin Server told the client that the resource should be cached for 2 minutes, User agent needs a resource that is at most 1 minute old (max-age = 60).
After 1 minute and few seconds from the first GET, that resource is not valid (from the User Agent point of view) and a request should go straight to the origin server (Or any other cache layers).

我是对的吗?是否可以从用户代理中指定大于零的 max-age ?常见的浏览器支持/支持吗?

Am I right?Is it possible to specify, from User Agent, a max-age greater than zero?Is it supported/honored by the common browsers?

在我工作的地方,我们有一个.NET自定义缓存机制,其工作原理如下:客户端从缓存中获取"AT MOST" X秒之前的资源时,可以指定 max-age .

Where I work, we have a .NET custom caching mechanism that works like this; clients can specify a max-age when they need a resource from the cache that is "AT MOST" X seconds old.

推荐答案

没有任何疑问. RFC7234 5.2.1.1 包含示例 max-age =5 ,它当然大于零.定义也很清楚(强调我的意思):

There is no need for any doubt.RFC7234 Section 5.2.1.1 includes an example max-age=5 which is of course greater than zero.The definition is also clear (emphasis mine):

指定的秒数"可以是任何非负整数(在第1.2.1节).因此,答案是肯定的.

"The specified number of seconds" can be any non-negative integer (defined in Section 1.2.1).So the answer is a definite yes.

此外,我在上面引用的定义还说明了您方案中的缓存行为.但是在此之前,我应该纠正以下问题:

Additionally, the definition I quoted above also explains the cache behavior in your scenario.But before I get to that, I should correct the following:

不正确.

max-age = 120 指令意味着服务器告诉所有缓存(而不是用户代理),必须在2分钟后将响应视为过期.摘自第5.2.2.8节(重点是我):

The max-age=120 directive means that the server tells all caches, not the user agent, that the response must be considered stale after 2 minutes.From Section 5.2.2.8 (emphasis mine):

如您所见,没有重新验证的要求.如果直到10分钟后才没有对同一个资源的请求,那么直到10分钟后才会进行任何重新验证.

As you can see, there is no revalidation requirement.If there are no requests to the same resource until 10 minutes later, there won't be any revalidation until 10 minutes later.

此外,来自第5.2节(重点是我):

Also, from Section 5.2 (emphasis mine):

仅缓存,而不是用户代理.

Just caches, not user agent.

请求/响应链中的每个参与者都使用相同的Cache-Control标头接收相同的响应,但是Cache-Control标头的预期接收者只是缓存.请记住,仅仅是因为您收到了它,并不意味着它适合您.

Each participant in the request/response chain receives the same response with the same Cache-Control header, but the intended recipients of the Cache-Control header are just caches.Remember, just because you receive it, doesn't mean it is for you.

在您的其他情况下,您的评估是正确的.我在这里引用它:

For the rest of your scenario, your assessment is correct.I'll quote it here:

...

据我了解,此请求应绕过用户代理缓存.为什么?

From what I understand, this request should bypass the user agent cache. Why?

由于在请求时,存储的响应的时间超过60秒.很明显,如果存储的响应时间为65秒,则不能使用 max-age = 60 指令来满足请求.因此,缓存仅遵循其接收的指令.

Because at the time of the request, the age of the stored response is more than 60 seconds.It should be obvious that if the age of a stored response is, say, 65 seconds, it cannot be used to satisfy a request with a max-age=60 directive.Thus, the cache simply obeys the directive it receives.

实际上,如第5.2 节(从源头而不是我的角度强调大写):

In fact, any standards compliant HTTP cache whether integrated in a browser or separate is required to obey the directive it receives, as stated in Section 5.2 (uppercase emphasis from source, not mine):

根据您的描述,您正在使用的自定义缓存机制似乎符合标准.因此,我对开发人员的补充,特别是如果习惯"是指内部开发".

Based on what you described, the custom caching mechanism you have at work seems to be standards compliant.So, my complements to the developers, particularly if by "custom" you mean "developed in house".

这篇关于用户代理是否可以在其请求中将max-age设置为大于零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 03:07