简体   繁体   English

HTTP Cache-Control 标头 max-age 与 max-age,必须重新验证

[英]HTTP Cache-Control header max-age vs max-age, must-revalidate

I want to find difference between max-age alone and max-age with must-revalidate in Cache-Control header.我想在 Cache-Control 标头中找到单独的 max-age 和带有 must-revalidate 的 max-age 之间的区别。

I did verification on chrome with both of these.我用这两种方法对 chrome 进行了验证。

  1. max-age=10 - It means that the response is no longer valid after 10 seconds and client has to reach to server to get the fresh result and should not serve the cached response or clear the response after 10 seconds. max-age=10 - 这意味着响应在 10 秒后不再有效,客户端必须到达服务器以获得新结果,并且不应该在 10 秒后提供缓存的响应或清除响应。

  2. max-age=10, must-revalidate - Here must-revalidate defines the same thing, but there can be 304 response from server and browser can use the cached data even after 10 seconds. max-age=10, must-revalidate - 这里 must-revalidate 定义了相同的东西,但是服务器可以有 304 响应,浏览器即使在 10 秒后也可以使用缓存的数据。

Assuming, difference with scenario 1, browser has to clear the cache and should expect fresh response from server, while in scenario 2 browser can keep the cached response and expect 304 status or fresh response.假设与场景 1 不同,浏览器必须清除缓存并期待来自服务器的新响应,而在场景 2 中,浏览器可以保留缓存响应并期待 304 状态或新响应。

But, practically in chrome above statements are not true, even if we respond with 403 chrome still serves the response from cache and by that mean there is no difference.但是,实际上在 chrome 中,上面的陈述是不正确的,即使我们用 403 响应,chrome 仍然会从缓存中获取响应,这意味着没有区别。 Below is the code sample for rest api.下面是 rest api 的代码示例。

var ccpublicWrongResp =  3;
var ccpublicWrongRespCount  = 0;
app.get('/ccpublic', function (req, res) {
  console.log(req.url + " - " + new Date().getSeconds());
  ccpublicWrongRespCount++;
   if(ccpublicWrongRespCount >= ccpublicWrongResp){
    console.log("Sending 304");
    res.statusCode = (304);
    res.send();
  }else{
    res.set('Cache-Control', 'public, max-age=12');
    res.send("Please check after 12 seconds ");
  }  
});

var j = 1;
app.get('/mustrevalidate', function (req, res) {
  console.log(req.url + " - " + new Date().getSeconds());
  j++;
  ccpublicWrongRespCount++;
  if(ccpublicWrongRespCount >= ccpublicWrongResp){
    console.log("Sending 304");
    res.statusCode = (304);
    res.send();
  }else{
    res.set('Cache-Control', 'public, max-age=12, must-revalidate');
    res.send(j + " with must-revalidate");
  }
});

The first thing to understand is that caches will attempt to revalidate a stale resource (rather than get a full response) whenever possible.首先要了解的是,缓存将尽可能尝试重新验证陈旧的资源(而不是获得完整的响应)。 That's why you see the same behavior with your two tests.这就是为什么您在两个测试中看到相同行为的原因。

Next, understand that caches are allowed to serve stale responses in some cases;接下来,了解在某些情况下允许缓存提供陈旧的响应 in particular, if they can't connect to the origin server.特别是,如果他们无法连接到原始服务器。

Now we can see the purpose of must-revalidate .现在我们可以看到must-revalidate的目的。 It just means that the cache should never serve stale responses.这只是意味着缓存永远不应该提供陈旧的响应。

The must-revalidate response directive indicates that once it has become stale, a cache MUST NOT use the response to satisfy subsequent requests without successful validation on the origin server. must-revalidate响应指令表明,一旦它变得陈旧,缓存不能使用响应来满足后续请求,而无需在源服务器上成功验证。

Put differently, you were interpreting the directive to mean "Must revalidate instead of getting a full response", but it actually means "Must revalidate instead of serving stale content, even in those situations where the cache would otherwise be able to do so."换句话说,您将指令解释为“必须重新验证而不是获得完整响应”,但它实际上意味着“必须重新验证而不是提供过时的内容,即使在缓存能够这样做的情况下也是如此”。

Note that in most cases the use of this header is inappropriate :请注意,在大多数情况下,使用此标头是 不合适的:

The must-revalidate directive ought to be used by servers if and only if failure to validate a request on the representation could result in incorrect operation.当且仅当未能验证对表示的请求可能导致错误操作时,服务器才应该使用must-revalidate指令。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HTTP缓存控制的最大使用期限,必须重新验证 - HTTP Cache Control max-age, must-revalidate max-age,no-Cache,必须在Cache-Control Header上重新验证,在此优先? - max-age, no-Cache,must-revalidate on Cache-Control Header, Which takes predence here? “no-cache”vs“max-age = 0,must-revalidate,proxy-revalidate” - “no-cache” ​vs “max-age=0, must-revalidate, proxy-revalidate” http请求中的cache-control:max-age = 0和If-Not-Modified - cache-control:max-age=0 and If-Not-Modified in http request http请求中的“ cache-control:max-age = 0” - `cache-control: max-age=0` in http request 同时使用“ Expires:0”,“ no-cache”,“ no-store”,“ max-age = 0,必须重新验证”-做什么用? - Simultaneous use of “Expires: 0”, “no-cache”, “no-store”, “max-age=0, must-revalidate” - what for? 在Cache-Control HTTP标头中使用max-age和s-maxage是否有意义? - Does it make sense to have max-age and s-maxage in the Cache-Control HTTP header? CloudFront “年龄” header 对“缓存控制:私有; 最大年龄=3600” - CloudFront “age” header effect on “cache-control: private; max-age=3600” 即使使用Etag,更改静态文件的URL仍然返回304,max-age:0,必须重新验证和无缓存 - Changed static file's URL still returns 304 even with Etag, max-age: 0, must-revalidate and no-cache 缓存控制:在公共或最大年龄之前? - Cache-Control: before public or max-age?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM