如果第一个响应是AppCache(Symfony2)私有的,可以吗? [英] Is it fine if first response is private with AppCache (Symfony2)?
问题描述
我正在尝试使用http缓存。在我的控制器中,我将响应设置如下:
I'm trying to use http caching. In my controller I'm setting a response as follows:
$response->setPublic();
$response->setMaxAge(120);
$response->setSharedMaxAge(120);
$response->setLastModified($lastModifiedAt);
开发模式
在开发环境中,第一个响应是带有以下标头的200:
In dev environment first response is a 200 with following headers:
cache-control:max-age=120, public, s-maxage=120
last-modified:Wed, 29 Feb 2012 19:00:00 GMT
接下来的2分钟,每个响应都是带有以下标头的304:
For next 2 minutes every response is a 304 with following headers:
cache-control:max-age=120, public, s-maxage=120
这基本上就是我的期望。
This is basically what I expect it to be.
生产模式
在生产模式下,响应头不同。请注意,在app.php中,我将内核包装在AppCache中。
In prod mode response headers are different. Note that in app.php I wrap the kernel in AppCache.
第一个响应是带有以下标头的200:
First response is a 200 with following headers:
cache-control:must-revalidate, no-cache, private
last-modified:Thu, 01 Mar 2012 11:17:35 GMT
所以这是一个私有的不缓存响应。
So it's a private no-cache response.
每个下一个请求都是几乎是我所期望的;具有以下标头的304:
Every next request is pretty much what I'd expect it to be; a 304 with following headers:
cache-control:max-age=120, public, s-maxage=120
我应该为此担心吗?
这是预期的行为吗?
我做了一些调试,我发现由于上次修改的标头,响应是私有的。 HttpCache内核使用EsiResponseCacheStrategy 更新缓存响应( HttpCache :: handle()方法)。
I did a bit of debugging and I figured that response is private because of last-modified header. HttpCache kernel uses EsiResponseCacheStrategy to update the cached response (HttpCache::handle() method).
if (HttpKernelInterface::MASTER_REQUEST === $type) {
$this->esiCacheStrategy->update($response);
}
EsiResponseCacheStrategy 如果使用Last-Response或ETag( EsiResponseCacheStrategy :: add()方法):
EsiResponseCacheStrategy turns a response into non cacheable if it uses either Last-Response or ETag (EsiResponseCacheStrategy::add() method):
if ($response->isValidateable()) {
$this->cacheable = false;
} else {
// ...
}
Response :: isValidateable()如果最后一个返回true -Response或ETag标头存在。
Response::isValidateable() returns true if Last-Response or ETag header is present.
结果为覆盖Cache-Control标头( EsiResponseCacheStrategy :: update()方法):
if (!$this->cacheable) {
$response->headers->set('Cache-Control', 'no-cache, must-revalidate');
return;
}
我在Symfony2用户组上问了这个问题,但没有得到答案到目前为止:> https://groups.google.com/d/topic/symfony2/6lpln11POq8/discussion
I asked this question on Symfony2 user group but I didn't get an answer so far: https://groups.google.com/d/topic/symfony2/6lpln11POq8/discussion
更新。
由于我再也无法访问原始文件了我尝试使用最新的Symfony标准重现此场景的代码版本。
Since I no longer have access to the original code I tried to reproduce the scenario with the latest Symfony standard edition.
响应标头现在更加一致,但似乎仍然是错误的。
Response headers are more consistent now, but still seem to be wrong.
我在响应上设置了 Last-Modified
标头后,浏览器的第一个响应a:
As soon as I set a Last-Modified
header on the response, the first response made by a browser has a:
Cache-Control:must-revalidate, no-cache, private
第二个响应是预期的:
Cache-Control:max-age=120, public, s-maxage=120
如果我避免发送 If-Modified-Since
标头,每个请求都返回必须重新验证,无缓存,私有
。
If I avoid sending If-Modified-Since
header, every request returns must-revalidate, no-cache, private
.
请求是在 prod
还是 dev
环境中进行的都没关系
It doesn't matter if the request was made in prod
or dev
environment anymore.
推荐答案
我也遇到了同样的问题。我必须在CDN中提供 public标题。默认情况下,在prod模式下启用网关缓存时,它会返回200 OK(带有私有),nocache必须验证标头。
I have faced same problem. I had to supply 'public' headers my cdn. By default when gateway caching is enabled in prod mode, it returns 200 OK with private, nocache must validate headers.
我以此方式解决了问题。
I solved problem this way.
在app.php中,在我向用户发送响应($ respond-> send)之前,我已将缓存控制标头覆盖为空白,并将缓存标头设置为public和max age(某些值)
In app.php, before I send response to user ($respond->send), I have overwritten the cache control header to blank and set cache headers to public and max age(some value).
//来自app.php的代码段
//code snippet from app.php
$response = $kernel->handle($request);
$response->headers->set('Cache-Control', '');
$response->setPublic();
$response->setMaxAge(86400);
$response->send();
这篇关于如果第一个响应是AppCache(Symfony2)私有的,可以吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!