Symfony: 11 – HTTP Cache

The most effective way to improve performance of an application is to cache the full output of a page and then bypass the application entirely on each subsequent request.

Using Edge Side Includes
Edge Side Includes (ESI) allow HTTP cache to be used to cache page fragments (even nested fragments) independently. With ESI, you can even cache an entire page for 60 minutes, but an embedded sidebar for only 5 minutes.

Gateway caches are a great way to make your website perform better. But they have one limitation: they can only cache whole pages. If you can’t cache whole pages or if parts of a page has more dynamic parts, Symfony provides a solution, based on a technology called ESI, or Edge Side Includes.

The ESI specification describes tags you can embed in your pages to communicate with the gateway cache. Only one tag is implemented in Symfony: include:

<!DOCTYPE html>
<html>
  <body>
    <!-- ... some content -->
 
    <!-- Embed the content of another page here -->
    <esi:include src="http://..." />
 
    <!-- ... more content -->
  </body>
</html>

Notice that each ESI tag has a fully-qualified URL. An ESI tag represents a page fragment that can be fetched via the given URL.

When a request is handled, the gateway cache fetches the entire page from its cache or requests it from the backend application. If the response contains one or more ESI tags, these are processed in the same way. In other words, the gateway cache either retrieves the included page fragment from its cache or requests the page fragment from the backend application again.

First, to use ESI, be sure to enable it in your application configuration:

# app/config/config.yml
framework:
  # ...
  esi: { enabled: true }