ESI caching in Symfony 3

1.3k Views Asked by At

I have enabled ESI in my Symfony 3 app 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:

{{foo}}

Now, I have another method that renders 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:

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

So I want to have mainly non cached view (baz) and nest barAction() inside it and have it cached.

But, I got:

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

even when I explicit set it to public, I got:

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

every time I refresh page and I got:

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

and if I refresh invalid becomes valid. But I can't set cache.

EDIT:

I read about ESI and validation cache and it seems that they don't work together. So I tried validation cache and added

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

instead of ETag. And still same result...

0

There are 0 best solutions below