Many web applications contain dynamic data in each page, and this is the reason why the use of cache to enhance the response time is often limited. For instance, consider a page resulting of an action decorated by a layout including components:

example page layout

The action and the three components are all dynamic, but only the component B depends on the session.

Symfony already contains the mechanisms to cache the action or the whole page (see more in the cache chapter of the symfony book). This way, even if your action contains queries to the database, it is only executed once. The page cache type is the fastest, but it doesn't allow for data depending on the session in the page. The slot cache type is the more versatile, but it is not as efficient as the page type since the layout - and its components - still have to be executed.

slot and page cache

The great news is, symfony now supports components cache. This means that you can combine performance and versatility to achieve something like:

slot and page cache

That's right: you can declare every session-independent part of a page (action, components A and C in the example) as cached, and only the very dynamic parts (component B in the example) will be executed when the page is requested. Think about displaying data from a content management system in a page where you also want to show if the user is logged or not, and you will understand that this is a very common situation.

Add to that the ability to use components from inside a template (the result of an action), and the ability to use components from inside a partial (or the view of another component), and you have the best tool for performance in a dynamic website.

Components cache is available since the release 1300 and is documented here.

Published in