Slim Framework 的 HTTP 缓存不会过期
HTTP cache not expires with Slim Framework
我正在使用 Slim 框架来开发 API。
我想在 HTTP 缓存中缓存一些请求:
$app->etag('Unique-ID')
但是时间过期似乎不起作用:
$app->expires('10 seconds')
当我用 Chrome 查看 headers 时,第一次调用时我得到一个 200 状态代码:好的。
第二次调用,我收到 304 状态码:好的。
等待 30 秒。
第三次调用,我仍然收到 304 状态代码:在我看来不正常。
我不应该因为缓存已过期而获得 200 状态代码吗?
谢谢。
简而言之
Expires
header 指示浏览器应该在客户端缓存中缓存内容多长时间。浏览器将从客户端缓存中提供内容,直到达到到期日期。
当客户端缓存过期时,浏览器将再次向服务器发送请求。请求包括 If-None-Match
header 以及它从服务器收到的先前 Etag
值。如果 If-None-Match
header 的值仍然与服务器上的当前 Etag
值匹配,它将以 304 Not Modified
.
响应
当您使用 Etag
和 If-None-Match
header 并希望服务器发送更新的内容时,Etag
header 的值必须更改。
更长的解释
以免假设您有以下代码。
$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
请求 header。此 header 的值与先前收到的 Etag
header 的值相同。
当 Slim 收到请求时,它会将 If-None-Match
header 的值与您通过 $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
header 的值。
$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.
我正在使用 Slim 框架来开发 API。
我想在 HTTP 缓存中缓存一些请求:
$app->etag('Unique-ID')
但是时间过期似乎不起作用:
$app->expires('10 seconds')
当我用 Chrome 查看 headers 时,第一次调用时我得到一个 200 状态代码:好的。
第二次调用,我收到 304 状态码:好的。
等待 30 秒。
第三次调用,我仍然收到 304 状态代码:在我看来不正常。
我不应该因为缓存已过期而获得 200 状态代码吗?
谢谢。
简而言之
Expires
header 指示浏览器应该在客户端缓存中缓存内容多长时间。浏览器将从客户端缓存中提供内容,直到达到到期日期。
当客户端缓存过期时,浏览器将再次向服务器发送请求。请求包括 If-None-Match
header 以及它从服务器收到的先前 Etag
值。如果 If-None-Match
header 的值仍然与服务器上的当前 Etag
值匹配,它将以 304 Not Modified
.
当您使用 Etag
和 If-None-Match
header 并希望服务器发送更新的内容时,Etag
header 的值必须更改。
更长的解释
以免假设您有以下代码。
$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
请求 header。此 header 的值与先前收到的 Etag
header 的值相同。
当 Slim 收到请求时,它会将 If-None-Match
header 的值与您通过 $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
header 的值。
$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.