0
votes
php - How to cache rendered controllers? - Stack Overflow
Asked
Viewed 50 times
0

I would like cache rendered controllers.

I have project in Symfony 3 and I found it: https://symfony.com/doc/3.4/http_cache/esi.html

I have:

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

And in controller:

public function infoAction($maxPerPage)
{
    $em = $this->getDoctrine()->getManager(); 
    $info = $em->getRepository(Info::class)->find(1);
    $response = $this->render('info.html.twig', [info => $info]);
    $response->setSharedMaxAge(600);

    return $response;
}

And in templates:

{{ render_esi(controller('AppBundle:News:info')) }}

I don't have any errors, and all is showing good, but this is not cached... If I change the data in the database, they are immediately displayed on the page, and should not appear until 600 seconds have passed.

What am I doing wrong?

    0

    You need to have Varnish or other cache gateway in front of your application:

    https://symfony.com/doc/3.4/http_cache/esi.html

    ... All of this happens transparently at the gateway cache level (i.e. outside of your application).

    ... if you use the esi renderer (i.e. call render_esi()) and if Symfony detects that it's talking to a gateway cache that supports ESI, it generates an ESI include tag. But if there is no gateway cache or if it does not support ESI, Symfony will just merge the included page content within the main one as it would have done if you had used render().

    1
    • Thanks, but maybe is simpliest way to cache controllers?
      – lalixe
      Jan 4 2018 at 12:29

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

    Not the answer you're looking for? Browse other questions tagged or ask your own question.

     
    1

    1 Answers

    0
    votes

    You need to have Varnish or other cache gateway in front of your application:

    https://symfony.com/doc/3.4/http_cache/esi.html

    ... All of this happens transparently at the gateway cache level (i.e. outside of your application).

    ... if you use the esi renderer (i.e. call render_esi()) and if Symfony detects that it's talking to a gateway cache that supports ESI, it generates an ESI include tag. But if there is no gateway cache or if it does not support ESI, Symfony will just merge the included page content within the main one as it would have done if you had used render().