Symfony2跳过AppCache中的默认设置

Symfony2跳过AppCache中的默认设置,symfony,caching,Symfony,Caching,当我在控制器中显式设置TTL时 $response->setMaxAge(60); $response->setSharedMaxAge(60); 一切正常,我得到了正确的响应标题: X-Symfony-Cache GET /hello/show/: miss, store ... and then ... X-Symfony-Cache GET /hello/show/: stale, invalid, store ... and then ... X-Symfony-Cache

当我在控制器中显式设置TTL时

$response->setMaxAge(60);
$response->setSharedMaxAge(60);
一切正常,我得到了正确的响应标题:

X-Symfony-Cache GET /hello/show/: miss, store
... and then ...
X-Symfony-Cache GET /hello/show/: stale, invalid, store
... and then ...
X-Symfony-Cache GET /hello/show/: fresh
但当我没有在控制器中显式设置TTL并希望使用默认TTL设置时:

class AppCache extends HttpCache {

    protected function getOptions() {
        return array(
            'debug' => true,
            'default_ttl' => 60,
            'private_headers' => array('Authorization', 'Cookie'),
            'allow_reload' => false,
            'allow_revalidate' => false,
            'stale_while_revalidate' => 2,
            'stale_if_error' => 60,
        );
    }
}
我总是得到一个“小姐”


看起来Symfony没有考虑默认的ttl设置。为什么?我找到了原因。这是由于私人/公共响应。文件规定:

公共与私人回应»

网关缓存和代理缓存都被视为“共享”缓存 缓存的内容由多个用户共享。如果是用户特定的 响应曾经被共享缓存错误地存储过,可能是这样 稍后返回给任意数量的不同用户。想象一下如果你的 帐户信息被缓存,然后返回到每个后续的 用户谁要求他们的帐户页

为了处理这种情况,可以将每个响应设置为公共或 私人:

public: Indicates that the response may be cached by both private and shared caches;
private: Indicates that all or part of the response message is intended for a single user and must not be cached by a shared cache.
Symfony保守地默认每个响应都是私有的。接受 共享缓存(如Symfony2反向代理)的优势 响应需要明确设置为公共

因此,要使Symfony使用默认的\u ttl设置,您必须明确地将响应设置为公共,如下所示:

$response->setPublic();
$response->setPublic();