如果第一个响应与AppCache(Symfony2)是私密的,那么它是否正确?

我试图使用httpcaching。 在我的控制器中,我设置了一个响应,如下所示:

$response->setPublic(); $response->setMaxAge(120); $response->setSharedMaxAge(120); $response->setLastModified($lastModifiedAt); 

开发模式

在开发环境中,第一个响应是200以下标题:

 cache-control:max-age=120, public, s-maxage=120 last-modified:Wed, 29 Feb 2012 19:00:00 GMT 

接下来的2分钟,每个响应是一个304以下标题:

 cache-control:max-age=120, public, s-maxage=120 

这基本上是我所期望的。

产品模式

在产品模式下,响应标头是不同的。 请注意,在app.php中我将内核包装在AppCache中。

第一个响应是200以下标题:

 cache-control:must-revalidate, no-cache, private last-modified:Thu, 01 Mar 2012 11:17:35 GMT 

所以这是一个私人的无caching响应。

接下来的每一个请求都是我所期望的。 304以下标题:

 cache-control:max-age=120, public, s-maxage=120 

我应该担心吗? 这是一个预期的行为?

如果我把Varnish或Akamai服务器放在它的前面会发生什么?

我做了一些debugging,我发现这个响应是私有的,因为最后修改的头文件。 HttpCache内核使用EsiResponseCacheStrategy来更新caching的响应( HttpCache :: handle()方法)。

 if (HttpKernelInterface::MASTER_REQUEST === $type) { $this->esiCacheStrategy->update($response); } 

如果使用Last-Response或ETag( EsiResponseCacheStrategy :: add()方法),EsiResponseCacheStrategy 会将响应变为不可caching的响应:

 if ($response->isValidateable()) { $this->cacheable = false; } else { // ... } 

如果存在Last-Response或ETag标题,则Response :: isValidateable()返回true。

它导致覆盖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

更新。

由于我不再有权访问原始代码,因此我尝试使用最新的Symfony标准版来重现场景 。

响应标题现在更一致,但似乎仍然是错误的。

只要我在响应中设置了Last-Modified标头,浏览器的第一个响应就是:

 Cache-Control:must-revalidate, no-cache, private 

第二个反应有一个预期的:

 Cache-Control:max-age=120, public, s-maxage=120 

如果我避免发送If-Modified-Since头,每个请求都会返回must-revalidate, no-cache, private

如果请求是在proddev环境中进行的,则无关紧要。

我面临同样的问题。 我不得不提供“公共”标题我的CDN。 默认情况下,当在prod模式下启用网关caching时,它返回200 OK与私有,nocache必须validation标头。

我这样解决了问题。

在app.php中,在我向用户发送响应($ respond-> send)之前,我已经将caching控制头覆盖为空,并将caching头设置为public和max age(某些值)。

// app.php中的代码片断

  $response = $kernel->handle($request); $response->headers->set('Cache-Control', ''); $response->setPublic(); $response->setMaxAge(86400); $response->send(); 

你所经历的行为是有意的。 Symfony2 Docs明确描述了使用私有公共的情况,默认是私有的