ESI Caching in Symfony 3

I have enabled ESI in my Symfony 3 application and I have this in app_dev.php :

 $kernel = new AppKernel('dev', true); $kernel = new AppCache($kernel); 

Now I have config.yml :

 framework: esi: { enabled: true } fragments: { path: /_fragment } 

In my controller:

 /** * @Route("/foo/bar/{fooId}", name="AppBundle_Foo_bar") */ public function barAction(int $fooId, Request $request) { //some database querying from repositroy $response = $this->render('AppBundle:Foo:bar.html.twig',['foo' => $foo]); $response->setETag(md5($response->getContent())); $response->setPublic(); $response->isNotModified($request); return $response; } 

This is the view ( bar.html.twig ) that I want to cache and looks like this:

 {{foo}} 

Now I have another method that displays the main view.

 /** * @Route("/baz/{fooId}", name="AppBundle_Foo_baz") * @Template */ public function bazAction(int $fooId) { return [ 'fooId' => $fooId ]; } 

And my baz.html.twig looks like this:

 {% extends 'AppBundle::layout.html.twig' %} {% block content %} {{ render_esi(controller('AppBundle:Foo:bar', { 'fooId': fooId })) }} {% endblock content %} 

So I want to have a mostly uncached view (baz) and a barAction() socket inside it and cache it.

But I got:

 Cache-Control:must-revalidate, no-cache, private 

even when I explicitly published it, I got:

 X-Symfony-Cache:GET /baz/1: miss; 

every time I refresh the page and I got:

 GET /_fragment?_hash=aO.....Details: stale, invalid, store 

and if I update invalid it becomes valid. But I can’t set the cache.

EDIT:

I read about ESI and validation cache, and it seems that they do not work together. So I tried the validation cache and added

 $response->setSharedMaxAge(15); $response->headers->addCacheControlDirective('must-revalidate', true); 

instead of ETag. And the same result ...

+5
source share

Source: https://habr.com/ru/post/1263229/


All Articles