如果第一个响应是AppCache(Symfony2)的私有,这样可以吗?

Jak*_*las 141 caching cache-control http-caching symfony

我正在尝试使用http缓存.在我的控制器中,我按如下方式设置响应:

$response->setPublic();
$response->setMaxAge(120);
$response->setSharedMaxAge(120);
$response->setLastModified($lastModifiedAt);
Run Code Online (Sandbox Code Playgroud)

开发模式

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

cache-control:max-age=120, public, s-maxage=120
last-modified:Wed, 29 Feb 2012 19:00:00 GMT
Run Code Online (Sandbox Code Playgroud)

在接下来的2分钟内,每个响应都是304,包含以下标题:

cache-control:max-age=120, public, s-maxage=120
Run Code Online (Sandbox Code Playgroud)

这基本上是我所期望的.

刺激模式

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

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

cache-control:must-revalidate, no-cache, private
last-modified:Thu, 01 Mar 2012 11:17:35 GMT
Run Code Online (Sandbox Code Playgroud)

所以这是一个私有的无缓存响应.

每一个下一个请求都是我所期望的那样; 带有以下标题的304:

cache-control:max-age=120, public, s-maxage=120
Run Code Online (Sandbox Code Playgroud)

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

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

我做了一些调试,我认为响应是私有的,因为最后修改过的头文件.HttpCache内核使用EsiResponseCacheStrategy来更新缓存的响应(HttpCache :: handle()方法).

if (HttpKernelInterface::MASTER_REQUEST === $type) {
    $this->esiCacheStrategy->update($response);
}
Run Code Online (Sandbox Code Playgroud)

如果EsiResponseCacheStrategy 使用Last-Response或ETag(EsiResponseCacheStrategy :: add()方法),则将响应转换为非缓存:

if ($response->isValidateable()) {
    $this->cacheable = false;
} else {
    // ... 
}
Run Code Online (Sandbox Code Playgroud)

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

它导致覆盖Cache-Control头(EsiResponseCacheStrategy :: update()方法):

if (!$this->cacheable) {
    $response->headers->set('Cache-Control', 'no-cache, must-revalidate');

    return;
}
Run Code Online (Sandbox Code Playgroud)

我在Symfony2用户组上提出了这个问题但到目前为止我没有得到答案:https://groups.google.com/d/topic/symfony2/6lpln11POq8/discussion

更新.

由于我无法再访问原始代码,因此尝试使用最新的Symfony标准版重现该方案.

响应标头现在更加一致,但似乎仍然是错误的.

一旦我Last-Modified在响应上设置了标题,浏览器的第一个响应就是:

Cache-Control:must-revalidate, no-cache, private
Run Code Online (Sandbox Code Playgroud)

第二个回应是预期的:

Cache-Control:max-age=120, public, s-maxage=120
Run Code Online (Sandbox Code Playgroud)

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

如果请求是在做不要紧,proddev环境了.

小智 9

我遇到了同样的问题.我不得不提供'公共'标题我的cdn.默认情况下,在prod模式下启用网关缓存时,它会返回200 OK,私有,nocache必须验证标头.

我这样解决了问题.

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

//来自app.php的代码片段

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


Uda*_*dan -4

您所经历的行为是有意为之的。Symfony2 文档明确描述了使用privatepublic 的情况,默认为private