symfony中私有缓存和公共缓存之间的差异


Differents between private cache and public cache in symfony

我正在尝试使用带有symfony的缓存。

在我的控制器中,我写下了这个片段

    public function indexAction(Request $request)
{
    $response = $this->render('default/index.html.twig', [
        'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..'),
    ]);
    $response->setMaxAge(10);
    $response->setPublic();
    return $response;
}

在我的分支中,我有一个函数random(100),然后我在app_dev.php和app.php中也启用了$kernel = new AppCache($kernel);当刷新时,我看到相同的"随机数"持续10秒,这是可以的

如果我省略了$response->setPublic();,那么缓存将变为私有的,并且我总是有不同的数字。我也尝试登录到我的应用程序,但缓存似乎不起作用

我还尝试在appCache()中添加getOptions

class AppCache extends HttpCache
{
    protected function getOptions()
    {
        return array(
            'debug'                  => false,
            'default_ttl'            => 0,
            'private_headers'        => array('Authorization', 'Cookie'),
            'allow_reload'           => false,
            'allow_revalidate'       => false,
            'stale_while_revalidate' => 2,
            'stale_if_error'         => 60,
        );
    }
}

如果缓存是private,则告诉反向代理您不希望缓存响应。Symfony将发送此标头:

Cache-Control: private

这绕过了中间代理,因为响应是针对单个用户的。在这种情况下,您使用浏览器缓存。如果要执行其他操作,请将其设置为public

你可以在这里阅读更多关于它的信息:公共与私人回应。