Symfony2: HTTP Cache [part 2] -> Edge Side Includes


ESI allows you to cache PARTS of your application on the same Response, so for example, you could have a website with a static home page which can be cached for 24 hours while it could contain a small part of it which shows mabe the latest forum posts. You would want to generate the home one time, but get the recent forum posts each 5 minutes, that´s when you know you need to use ESI.

To enable it:

framework:
    esi: { enabled: true }

To use it, you render your home as you would normally do, but on the template, in the part of your html where you want to put the latest forum blocks (as an example) you put this (using Twig):

{% render 'SomeCrazyBundle:Forum:latest' with {}, {'standalone': true} %}

The standalone=true will tell Symfony2 to use ESI. The action latestAction from the ForumController will now be able to return the latest forum posts setting the MaxAge to whatever it needs (15 for example)

If you test this refreshing the page many times, you will see how the block changes but the home dont (set 3 seconds and 10 seconds for the home for fast testing)

Advertisements