HTML caching, if not properly handled, can create incoherence in displayed data. Each time you disable the cache for an element, you should test it thoroughly and monitor the execution boost to tweak it.
12.3.1. Building a Staging Environment
The caching system is prone to new errors in the production environment that can't be detected in the development environment, since the HTML cache is disabled by default in development. If you enable the HTML cache for some actions, you should add a new environment, called staging in this section, with the same settings as the
prod environment (thus, with cache enabled) but with
web_debug set to
To set it up, edit the
settings.yml file of your application and add the lines shown in Listing 12-12 at the top.
Listing 12-12 - Settings for a
staging Environment, in
staging: .settings: web_debug: on cache: on
In addition, create a new front controller by copying the production one (probably
myproject/web/index.php) to a new
myapp_staging.php. Edit it to change the
SF_DEBUG values, as follows:
define('SF_ENVIRONMENT', 'staging'); define('SF_DEBUG', true);
That's it — you have a new environment. Use it by adding the front controller name after the domain name:
Tip Instead of copying an existing one, you can create a new front controller with the
symfony command line. For instance, to create a
staging environment for the
myapp application, called
myapp_staging.php and where
true, just call
symfony init-controller myapp staging myapp_staging true.
12.3.2. Monitoring Performance
Chapter 16 will explore the web debug toolbar and its contents. However, as this toolbar offers valuable information about cached elements, here is a brief description of its cache features.
When you browse to a page that contains cacheable elements (action, partials, fragments, and so on), the web debug toolbar (in the top-right corner of the window) shows an ignore cache button (a green, rounded arrow), as shown in Figure 12-4. This button reloads the page and forces the processing of cached elements. Be aware that it does not clear the cache.
The last number on the right side of the debug toolbar is the duration of the request execution. If you enable cache on a page, this number should decrease the second time you load the page, since symfony uses the data from the cache instead of reprocessing the scripts. You can easily monitor the cache improvements with this indicator.
The debug toolbar also shows the number of database queries executed during the processing of the request, and the detail of the durations per category (click the total duration to display the detail). Monitoring this data, in conjunction with the total duration, will help you do fine measures of the performance improvements brought by the cache.
The debug mode greatly decreases the speed of your application, since a lot of information is logged and made available to the web debug toolbar. So the processed time displayed when you browse in the
staging environment is not representative of what it will be in production, where the debug mode is turned
To get a better view of the process time of each request, you should use benchmarking tools, like Apache Bench or JMeter. These tools allow load testing and provide two important pieces of information: the average loading time of a specific page and the maximum capacity of your server. The average loading time data is very useful for monitoring performance improvements due to cache activation.
12.3.4. Identifying Cache Parts
When the web debug toolbar is enabled, the cached elements are identified in a page with a red frame, each having a cache information box on the top left, as shown in Figure 12-5. The box has a blue background if the element has been executed, or a yellow background if it comes from the cache. Clicking the cache information link displays the identifier of the cache element, its lifetime, and the elapsed time since its last modification. This will help you identify problems when dealing with out-of-context elements, to see when the element was created and which parts of a template you can actually cache.