Symfony2 ESI缓存+上次修改

时间:2013-04-10 12:29:46

标签: caching symfony esi

我正在使用symfony2网站,我需要缓存几个页面,但有些部分必须保持未缓存状态(如用户菜单等)。我已经检查了文档,ESI似乎是为此而构建的。

我开始在我的项目中为我的博客文章页面实现缓存。我按照Symfony2文档中的建议设置了最后一次修改和Etag的缓存验证。

我的所有页面的标题中都有一个用户菜单。它使用ESI呈现,我确保它不会缓存。据我所知,它不起作用。我的整个博客文章页面每次都与用户菜单一起完全缓存。它保存在浏览器缓存中,只在我实际更新博客文章时更新(这是正确的)。

这是我的博客文章控制器的代码:

public function showAction($slug)
{
$response = new Response();
$response->setETag(md5($response->getContent()));
$date = $article->getModifiedAt();
$response->setLastModified($date);
$response->setPublic();
$response->headers->addCacheControlDirective('must-revalidate', true);
// Check that the Response is not modified for the given Request
if ($response->isNotModified($this->getRequest())) {
    // return the 304 Response immediately
        return $response;
    } else {
    //do stuff
    return  $this->render('NewsBundle:News:show.html.twig', array(
            'article' => $article,
        ),$response); 
}

我的用户菜单控制器:

public function userMenuAction()
{
$response = new Response();
$response->setSharedMaxAge(0);

return $this->render('MainBundle:Views:userMenu.html.twig', array(
             'user' => $user,
      ),$response);
}

我的ESI路线

ESI_userMenu:
pattern:  user-menu
defaults: { _locale: en, _controller: MainBundle:Default:userMenu }

ESI渲染:

{% render url('ESI_userMenu') with {}, {'standalone': true} %}

当我加载我的博客文章页面时,我注意到用户菜单也被缓存了。 我已经测试了更多,并且发现如果我不使用“isNotModified”,但是设置了到期时间,ESI确实有效。

有没有办法让ESI使用我用于博客文章的“isNotModified”结构?

由于

编辑:

ESI似乎无法使用验证缓存...

见这里: With Symfony2 why are ESI tags inside cached responses ignored?

1 个答案:

答案 0 :(得分:2)

ESI似乎不适用于验证缓存,但仅适用于到期缓存。请注意,我在2.0.23中进行了测试,也许这个问题在以后的版本中得到了解决。

With Symfony2 why are ESI tags inside cached responses ignored?