I'm working on a symfony2 website where I need several pages cached, but some parts have to stay uncached (like a user menu etc). I've checked the documentation and ESI seems to be built exactly for this.

I started implementing caching in my project for my blog article page. I set cache validation with last modified and Etag as suggested in the Symfony2 docs.

I have a user menu in the header of all my pages. It renders with ESI and I make sure it doesnt cache. As far as I can see it's not working though. My entire blog article page is cached completely every time, together with the user menu. It's saved into the browser cache and only updates when I actually update the blog article (which is correct).

Here's my code for the blog article controller:

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); 
}

My user menu controller:

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

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

MY ESI routing

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

ESI render:

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

When I load my blog article page, I noticed that the user menu gets cached as well. I've tested more, and found out if I dont use "isNotModified", but set an expiration life time instead, the ESI does work.

Is there any way to get ESI to work with the "isNotModified" structure I used for the blog article?

Thanks

EDIT:

ESI doesnt seem to work with validation cache...

See here: With Symfony2 why are ESI tags inside cached responses ignored?

有帮助吗?

解决方案

ESI doesn't seem to work with validation cache, but only with expiration cache. Note that I tested in 2.0.23, perhaps the problem is solved in later versions.

With Symfony2 why are ESI tags inside cached responses ignored?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top