总之
Expires 标头指示浏览器应在客户端缓存中缓存内容多长时间。浏览器将从客户端缓存中提供内容,直到达到到期日期。
当客户端缓存过期时,浏览器会再次向服务器发送请求。请求包含 If-None-Match 标头和它从服务器接收到的先前的 Etag 值。如果If-None-Match 标头的值仍然与服务器上当前的Etag 值匹配,它将以304 Not Modified 响应。
当您使用 Etag 和 If-None-Match 标头并希望服务器发送更新的内容时,Etag 标头的值必须更改。
更长的解释
以免假设您有以下代码。
$app = new \Slim\Slim();
$app->get("/hello", function() use ($app){
$app->etag("unique-etag-1");
echo "Hello world.\n";
});
$app->run();
然后你提出请求。
$ curl --include http://localhost:8080/hello
HTTP/1.1 200 OK
Host: localhost:8080
Connection: close
X-Powered-By: PHP/5.6.2
Content-type: text/html;charset=UTF-8
Etag: "unique-etag-1"
Hello world
在后续请求中,浏览器将发送If-None-Match 请求标头。此标头的值与先前收到的 Etag 标头的值相同。
当 Slim 收到请求时,它会将 If-None-Match 标头的值与您在 $app->etag() 调用中设置的值进行比较。如果这些匹配 304 Not Modified 将被返回。
$ curl --include --header 'If-None-Match: "unique-etag-1"' http://localhost:8080/hello
HTTP/1.1 304 Not Modified
Host: localhost:8080
Connection: close
X-Powered-By: PHP/5.6.2
Etag: "unique-etag-1"
Content-type: text/html; charset=UTF-8
如果 URI 的内容发生变化,或者由于某些其他原因您希望浏览器重新获取内容,请更改 Etag 标头的值。
$app->get("/hello", function() use ($app){
$app->etag("unique-etag-2");
echo "Hello world.\n";
});
现在,当浏览器发出新请求时,您将收到200 OK 响应。
curl --include --header 'If-None-Match: "unique-etag-1"' http://localhost:8080/hello
HTTP/1.1 200 OK
Host: localhost:8080
Connection: close
X-Powered-By: PHP/5.6.2
Content-type: text/html;charset=UTF-8
Etag: "unique-etag-2"
Hello world.