12-Caching.txt 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. Chapter 12 - Caching
  2. ====================
  3. One of the ways to speed up an application is to store chunks of generated HTML code, or even full pages, for future requests. This technique is known as caching, and it can be managed on the server side and on the client side.
  4. Symfony offers a flexible server-caching system. It allows saving the full response, the result of an action, a partial, or a template fragment into a file, through a very intuitive setup based on YAML files. When the underlying data changes, you can easily clear selective parts of the cache with the command line or special action methods. Symfony also provides an easy way to control the client-side cache through HTTP 1.1 headers. This chapter deals with all these subjects, and gives you a few tips on monitoring the improvements that caching can bring to your applications.
  5. Caching the Response
  6. --------------------
  7. The principle of HTML caching is simple: Part or all of the HTML code that is sent to a user upon a request can be reused for a similar request. This HTML code is stored in a special place (the `cache/` folder in symfony), where the front controller will look for it before executing an action. If a cached version is found, it is sent without executing the action, thus greatly speeding up the process. If no cached version is found, the action is executed, and its result (the view) is stored in the cache folder for future requests.
  8. As all the pages may contain dynamic information, the HTML cache is disabled by default. It is up to the site administrator to enable it in order to improve performance.
  9. Symfony handles three different types of HTML caching:
  10. * Cache of an action (with or without the layout)
  11. * Cache of a partial, a component, or a component slot
  12. * Cache of a template fragment
  13. The first two types are handled with YAML configuration files. Template fragment caching is managed by calls to helper functions in the template.
  14. ### Global Cache Settings
  15. For each application of a project, the HTML cache mechanism can be enabled or disabled (the default), per environment, in the `cache` setting of the `settings.yml` file. Listing 12-1 demonstrates enabling the cache.
  16. Listing 12-1 - Activating the Cache, in `frontend/config/settings.yml`
  17. dev:
  18. .settings:
  19. cache: on
  20. ### Caching an Action
  21. Actions displaying static information (not depending on database or session-dependent data) or actions reading information from a database but without modifying it (typically, GET requests) are often ideal for caching. Figure 12-1 shows which elements of the page are cached in this case: either the action result (its template) or the action result together with the layout.
  22. Figure 12-1 - Caching an action
  23. ![Caching an action](/images/book/F1201.png "Caching an action")
  24. For instance, consider a `user/list` action that returns the list of all users of a website. Unless a user is modified, added, or removed (and this matter will be discussed later in the "Removing Items from the Cache" section), this list always displays the same information, so it is a good candidate for caching.
  25. Cache activation and settings, action by action, are defined in a cache.yml file located in the module `config/` directory. See Listing 12-2 for an example.
  26. Listing 12-2 - Activating the Cache for an Action, in `frontend/modules/user/config/cache.yml`
  27. list:
  28. enabled: on
  29. with_layout: false # Default value
  30. lifetime: 86400 # Default value
  31. This configuration stipulates that the cache is on for the list action, and that the layout will not be cached with the action (which is the default behavior). It means that even if a cached version of the action exists, the layout (together with its partials and components) is still executed. If the `with_layout` setting is set to `true`, the layout is cached with the action and not executed again.
  32. To test the cache settings, call the action in the development environment from your browser.
  33. http://myapp.example.com/frontend_dev.php/user/list
  34. You will notice a border around the action area in the page. The first time, the area has a blue header, showing that it did not come from the cache. Refresh the page, and the action area will have a yellow header, showing that it did come from the cache (with a notable boost in response time). You will learn more about the ways to test and monitor caching later in this chapter.
  35. >**NOTE**
  36. >Slots are part of the template, and caching an action will also store the value of the slots defined in this action's template. So the cache works natively for slots.
  37. The caching system also works for pages with arguments. The `user` module may have, for instance, a `show` action that expects an `id` argument to display the details of a user. Modify the `cache.yml` file to enable the cache for this action as well, as shown in Listing 12-3.
  38. In order to organize your `cache.yml`, you can regroup the settings for all the actions of a module under the `all:` key, also shown in Listing 12-3.
  39. Listing 12-3 - A Full `cache.yml` Example, in `frontend/modules/user/config/cache.yml`
  40. list:
  41. enabled: on
  42. show:
  43. enabled: on
  44. all:
  45. with_layout: false # Default value
  46. lifetime: 86400 # Default value
  47. Now, every call to the `user/show` action with a different `id` argument creates a new record in the cache. So the cache for this:
  48. http://myapp.example.com/user/show/id/12
  49. will be different than the cache for this:
  50. http://myapp.example.com/user/show/id/25
  51. >**CAUTION**
  52. >Actions called with a POST method or with GET parameters are not cached.
  53. The `with_layout` setting deserves a few more words. It actually determines what kind of data is stored in the cache. For the cache without layout, only the result of the template execution and the action variables are stored in the cache. For the cache with layout, the whole response object is stored. This means that the cache with layout is much faster than the cache without it.
  54. If you can functionally afford it (that is, if the layout doesn't rely on session-dependent data), you should opt for the cache with layout. Unfortunately, the layout often contains some dynamic elements (for instance, the name of the user who is connected), so action cache without layout is the most common configuration. However, RSS feeds, pop-ups, and pages that don't depend on cookies can be cached with their layout.
  55. ### Caching a Partial, Component, or Component Slot
  56. Chapter 7 explained how to reuse code fragments across several templates, using the `include_partial()` helper. A partial is as easy to cache as an action, and its cache activation follows the same rules, as shown in Figure 12-2.
  57. Figure 12-2 - Caching a partial, component, or component slot
  58. ![Caching a partial, component, or component slot](/images/book/F1202.png "Caching a partial, component, or component slot")
  59. For instance, Listing 12-4 shows how to edit the `cache.yml` file to enable the cache on a `_my_partial.php` partial located in the `user` module. Note that the `with_layout` setting doesn't make sense in this case.
  60. Listing 12-4 - Caching a Partial, in `frontend/modules/user/config/cache.yml`
  61. _my_partial:
  62. enabled: on
  63. list:
  64. enabled: on
  65. ...
  66. Now all the templates using this partial won't actually execute the PHP code of the partial, but will use the cached version instead.
  67. [php]
  68. <?php include_partial('user/my_partial') ?>
  69. Just as for actions, partial caching is also relevant when the result of the partial depends on parameters. The cache system will store as many versions of a template as there are different values of parameters.
  70. [php]
  71. <?php include_partial('user/my_other_partial', array('foo' => 'bar')) ?>
  72. >**TIP**
  73. >The action cache is more powerful than the partial cache, since when an action is cached, the template is not even executed; if the template contains calls to partials, these calls are not performed. Therefore, partial caching is useful only if you don't use action caching in the calling action or for partials included in the layout.
  74. A little reminder from Chapter 7: A component is a light action put on top of a partial, and a component slot is a component for which the action varies according to the calling actions. These two inclusion types are very similar to partials, and support caching in the same way. For instance, if your global layout includes a component called `day` with `include_component('general/day')` in order to show the current date, set the `cache.yml` file of the `general` module as follows to enable the cache on this component:
  75. _day:
  76. enabled: on
  77. When caching a component or a partial, you must decide whether to store a single version for all calling templates or a version for each template. By default, a component is stored independently of the template that calls it. But contextual components, such as a component that displays a different sidebar with each action, should be stored as many times as there are templates calling it. The caching system can handle this case, provided that you set the `contextual` parameter to `true`, as follows:
  78. _day:
  79. contextual: true
  80. enabled: on
  81. >**NOTE**
  82. >Global components (the ones located in the application `templates/` directory) can be cached, provided that you declare their cache settings in the application `cache.yml`.
  83. ### Caching a Template Fragment
  84. Action caching applies to only a subset of actions. For the other actions--those that update data or display session-dependent information in the template--there is still room for cache improvement but in a different way. Symfony provides a third cache type, which is dedicated to template fragments and enabled directly inside the template. In this mode, the action is always executed, and the template is split into executed fragments and fragments in the cache, as illustrated in Figure 12-3.
  85. Figure 12-3 - Caching a template fragment
  86. ![Caching a template fragment](/images/book/F1203.png "Caching a template fragment")
  87. For instance, you may have a list of users that shows a link of the last-accessed user, and this information is dynamic. The `cache()` helper defines the parts of a template that are to be put in the cache. See Listing 12-5 for details on the syntax.
  88. Listing 12-5 - Using the `cache()` Helper, in `frontend/modules/user/templates/listSuccess.php`
  89. [php]
  90. <!-- Code executed each time -->
  91. <?php echo link_to('last accessed user', 'user/show?id='.$last_accessed_user_id) ?>
  92. <!-- Cached code -->
  93. <?php if (!cache('users')): ?>
  94. <?php foreach ($users as $user): ?>
  95. <?php echo $user->getName() ?>
  96. <?php endforeach; ?>
  97. <?php cache_save() ?>
  98. <?php endif; ?>
  99. Here's how it works:
  100. * If a cached version of the fragment named 'users' is found, it is used to replace the code between the <?php if (!cache($unique_fragment_name)): ?> and the <?php endif; ?> lines.
  101. * If not, the code between these lines is processed and saved in the cache, identified with the unique fragment name.
  102. The code not included between such lines is always processed and not cached.
  103. >**CAUTION**
  104. >The action (`list` in the example) must not have caching enabled, since this would bypass the whole template execution and ignore the fragment cache declaration.
  105. The speed boost of using the template fragment cache is not as significant as with the action cache, since the action is always executed, the template is partially processed, and the layout is always used for decoration.
  106. You can declare additional fragments in the same template; however, you need to give each of them a unique name so that the symfony cache system can find them afterwards.
  107. As with actions and components, cached fragments can take a lifetime in seconds as a second argument of the call to the `cache()` helper.
  108. [php]
  109. <?php if (!cache('users', 43200)): ?>
  110. The default cache lifetime (86400 seconds, or one day) is used if no parameter is given to the helper.
  111. >**TIP**
  112. >Another way to make an action cacheable is to insert the variables that make it vary into the action's routing pattern. For instance, if a home page displays the name of the connected user, it cannot be cached unless the URL contains the user nickname. Another example is for internationalized applications: If you want to enable caching on a page that has several translations, the language code must somehow be included in the URL pattern. This trick will multiply the number of pages in the cache, but it can be of great help to speed up heavily interactive applications.
  113. ### Configuring the Cache Dynamically
  114. The `cache.yml` file is one way to define cache settings, but it has the inconvenience of being invariant. However, as usual in symfony, you can use plain PHP rather than YAML, and that allows you to configure the cache dynamically.
  115. Why would you want to change the cache settings dynamically? A good example is a page that is different for authenticated users and for anonymous ones, but the URL remains the same. Imagine an `article/show` page with a rating system for articles. The rating feature is disabled for anonymous users. For those users, rating links trigger the display of a login form. This version of the page can be cached. On the other hand, for authenticated users, clicking a rating link makes a POST request and creates a new rating. This time, the cache must be disabled for the page so that symfony builds it dynamically.
  116. The right place to define dynamic cache settings is in a filter executed before the `sfCacheFilter`. Indeed, the cache is a filter in symfony, just like the the security features. In order to enable the cache for the `article/show` page only if the user is not authenticated, create a `conditionalCacheFilter` in the application `lib/` directory, as shown in Listing 12-6.
  117. Listing 12-6 - Configuring the Cache in PHP, in `frontend/lib/conditionalCacheFilter.class.php`
  118. [php]
  119. class conditionalCacheFilter extends sfFilter
  120. {
  121. public function execute($filterChain)
  122. {
  123. $context = $this->getContext();
  124. if (!$context->getUser()->isAuthenticated())
  125. {
  126. foreach ($this->getParameter('pages') as $page)
  127. {
  128. $context->getViewCacheManager()->addCache($page['module'], $page['action'], array('lifeTime' => 86400));
  129. }
  130. }
  131. // Execute next filter
  132. $filterChain->execute();
  133. }
  134. }
  135. You must register this filter in the `filters.yml` file before the `sfCacheFilter`, as shown in Listing 12-7.
  136. Listing 12-7 - Registering Your Custom Filter, in `frontend/config/filters.yml`
  137. ...
  138. security: ~
  139. conditionalCache:
  140. class: conditionalCacheFilter
  141. param:
  142. pages:
  143. - { module: article, action: show }
  144. cache: ~
  145. ...
  146. Clear the cache (to autoload the new filter class), and the conditional cache is ready. It will enable the cache of the pages defined in the pages parameter only for users who are not authenticated.
  147. The `addCache()` method of the `sfViewCacheManager` object expects a module name, an action name, and an associative array with the same parameters as the ones you would define in a `cache.yml` file. For instance, if you want to define that the `article/show` action must be cached with the layout and with a lifetime of 3600 seconds, then write the following:
  148. [php]
  149. $context->getViewCacheManager()->addCache('article', 'show', array(
  150. 'withLayout' => true,
  151. 'lifeTime' => 3600,
  152. ));
  153. >**SIDEBAR**
  154. >Alternative Caching storage
  155. >
  156. >By default, the symfony cache system stores data in files on the web server hard disk. You may want to store cache in memory (for instance, via [memcached](http://www.danga.com/memcached/)) or in a database (notably if you want to share your cache among several servers or speed up cache removal). You can easily alter symfony's default cache storage system because the cache class used by the symfony view cache manager is defined in `factories.yml`.
  157. >
  158. >The default view cache storage factory is the `sfFileCache` class:
  159. >
  160. > view_cache:
  161. > class: sfFileCache
  162. > param:
  163. > automaticCleaningFactor: 0
  164. > cacheDir: %SF_TEMPLATE_CACHE_DIR%
  165. >
  166. >You can replace the `class` with your own cache storage class or with one of the symfony alternative classes (including `sfAPCCache`, `sfEAcceleratorCache`, `sfMemcacheCache`, `sfSQLiteCache`, and `sfXCacheCache`). The parameters defined under the `param` key are passed to the constructor of the cache class as an associative array. Any view cache storage class must implement all methods found in the abstract `sfCache` class. Refer to the Chapter 19 for more information on this subject.
  167. ### Using the Super Fast Cache
  168. Even a cached page involves some PHP code execution. For such a page, symfony still loads the configuration, builds the response, and so on. If you are really sure that a page is not going to change for a while, you can bypass symfony completely by putting the resulting HTML code directly into the `web/` folder. This works thanks to the Apache `mod_rewrite` settings, provided that your routing rule specifies a pattern ending without a suffix or with `.html`.
  169. You can do this by hand, page by page, with a simple command-line call:
  170. > curl http://myapp.example.com/user/list.html > web/user/list.html
  171. After that, every time that the `user/list` action is requested, Apache finds the corresponding `list.html` page and bypasses symfony completely. The trade-off is that you can't control the page cache with symfony anymore (lifetime, automatic deletion, and so on), but the speed gain is very impressive.
  172. Alternatively, you can use the `sfSuperCache` symfony plug-in, which automates the process and supports lifetime and cache clearing. Refer to Chapter 17 for more information about plug-ins.
  173. >**SIDEBAR**
  174. >Other Speedup tactics
  175. >
  176. >In addition to the HTML cache, symfony has two other cache mechanisms, which are completely automated and transparent to the developer. In the production environment, the configuration and the template translations are cached in files stored in the myproject/cache/config/ and myproject/cache/i18n/ directories without any intervention.
  177. >
  178. >PHP accelerators (eAccelerator, APC, XCache, and so on), also called opcode caching modules, increase performance of PHP scripts by caching them in a compiled state, so that the overhead of code parsing and compiling is almost completely eliminated. This is particularly effective for the Propel classes, which contain a great amount of code. These accelerators are compatible with symfony and can easily triple the speed of an application. They are recommended in production environments for any symfony application with a large audience.
  179. >
  180. >With a PHP accelerator, you can manually store persistent data in memory, to avoid doing the same processing for each request, with the `sfProcessCache` class. And if you want to cache the result of a CPU-intensive function, you will probably use the `sfFunctionCache` object. Refer to Chapter 18 for more information about these mechanisms.
  181. Removing Items from the Cache
  182. -----------------------------
  183. If the scripts or the data of your application change, the cache will contain outdated information. To avoid incoherence and bugs, you can remove parts of the cache in many different ways, according to your needs.
  184. ### Clearing the Entire Cache
  185. The `cache:clear` task of the symfony command line erases the cache (HTML, configuration, routing, and i18n cache). You can pass it arguments to erase only a subset of the cache, as shown in Listing 12-8. Remember to call it only from the root of a symfony project.
  186. Listing 12-8 - Clearing the Cache
  187. // Erase the whole cache
  188. > php symfony cache:clear
  189. // Short syntax
  190. > php symfony cc
  191. // Erase only the cache of the frontend application
  192. > php symfony cache:clear --app=frontend
  193. // Erase only the HTML cache of the frontend application
  194. > php symfony cache:clear --app=frontend --type=template
  195. // Erase only the configuration cache of the frontend application
  196. // The built-types are config, i18n, routing, and template.
  197. > php symfony cache:clear --app=frontend --type=config
  198. // Erase only the configuration cache of the frontend application and the prod environment
  199. > php symfony cache:clear --app=frontend --type=config --env=prod
  200. >**NOTE**
  201. > Under Microsoft Windows, you may have to encapsulate options into double-quotes:
  202. > > php symfony cache:clear "--app=frontend"
  203. ### Clearing Selective Parts of the Cache
  204. When the database is updated, the cache of the actions related to the modified data must be cleared. You could clear the whole cache, but that would be a waste for all the existing cached actions that are unrelated to the model change. This is where the `remove()` method of the `sfViewCacheManager` object applies. It expects an internal URI as argument (the same kind of argument you would provide to a `link_to()`), and removes the related action cache.
  205. For instance, imagine that the `update` action of the `user` module modifies the columns of a `User` object. The cached versions of the `list` and `show` actions need to be cleared, or else the old versions, which contain erroneous data, are displayed. To handle this, use the `remove()` method, as shown in Listing 12-9.
  206. Listing 12-9 - Clearing the Cache for a Given Action, in `modules/user/actions/actions.class.php`
  207. [php]
  208. public function executeUpdate($request)
  209. {
  210. // Update a user
  211. $user_id = $request->getParameter('id');
  212. $user = UserPeer::retrieveByPk($user_id);
  213. $this->foward404Unless($user);
  214. $user->setName($request->getParameter('name'));
  215. ...
  216. $user->save();
  217. // Clear the cache for actions related to this user
  218. $cacheManager = $this->getContext()->getViewCacheManager();
  219. $cacheManager->remove('user/list');
  220. $cacheManager->remove('user/show?id='.$user_id);
  221. ...
  222. }
  223. Removing cached partials, components, and component slots is a little trickier. As you can pass them any type of parameter (including objects), it is almost impossible to identify their cached version afterwards. Let's focus on partials, as the explanation is the same for the other template components. Symfony identifies a cached partial with a special prefix (`sf_cache_partial`), the name of the module, and the name of the partial, plus a hash of all the parameters used to call it, as follows:
  224. [php]
  225. // A partial called by
  226. <?php include_partial('user/my_partial', array('user' => $user) ?>
  227. // Is identified in the cache as
  228. @sf_cache_partial?module=user&action=_my_partial&sf_cache_key=bf41dd9c84d59f3574a5da244626dcc8
  229. In theory, you could remove a cached partial with the `remove()` method if you knew the value of the parameters hash used to identify it, but this is very impracticable. Fortunately, if you add a `sf_cache_key` parameter to the `include_partial()` helper call, you can identify the partial in the cache with something that you know. As you can see in Listing 12-10, clearing a single cached partial --for instance, to clean up the cache from the partial based on a modified `User`-- becomes easy.
  230. Listing 12-10 - Clearing Partials from the Cache
  231. [php]
  232. <?php include_partial('user/my_partial', array(
  233. 'user' => $user,
  234. 'sf_cache_key' => $user->getId()
  235. ) ?>
  236. // Is identified in the cache as
  237. @sf_cache_partial?module=user&action=_my_partial&sf_cache_key=12
  238. // Clear _my_partial for a specific user in the cache with
  239. $cacheManager->remove('@sf_cache_partial?module=user&action=_my_partial&sf_cache_key='.$user->getId());
  240. To clear template fragments, use the same `remove()` method. The key identifying the fragment in the cache is composed of the same `sf_cache_partial` prefix, the module name, the action name, and the `sf_cache_key` (the unique name of the cache fragment included by the `cache()` helper). Listing 12-11 shows an example.
  241. Listing 12-11 - Clearing Template Fragments from the Cache
  242. [php]
  243. <!-- Cached code -->
  244. <?php if (!cache('users')): ?>
  245. ... // Whatever
  246. <?php cache_save() ?>
  247. <?php endif; ?>
  248. // Is identified in the cache as
  249. @sf_cache_partial?module=user&action=list&sf_cache_key=users
  250. // Clear it with
  251. $cacheManager->remove('@sf_cache_partial?module=user&action=list&sf_cache_key=users');
  252. >**SIDEBAR**
  253. >Selective Cache Clearing Can damage your Brain
  254. >
  255. >The trickiest part of the cache-clearing job is to determine which actions are influenced by a data update.
  256. >
  257. >For instance, imagine that the current application has a `publication` module where publications are listed (`list` action) and described (`show` action), along with the details of their author (an instance of the `User` class). Modifying one User record will affect all the descriptions of the user's publications and the list of publications. This means that you need to add to the `update` action of the `user` module, something like this:
  258. >
  259. > [php]
  260. > $c = new Criteria();
  261. > $c->add(PublicationPeer::AUTHOR_ID, $request->getParameter('id'));
  262. > $publications = PublicationPeer::doSelect($c);
  263. >
  264. > $cacheManager = sfContext::getInstance()->getViewCacheManager();
  265. > foreach ($publications as $publication)
  266. > {
  267. > $cacheManager->remove('publication/show?id='.$publication->getId());
  268. > }
  269. > $cacheManager->remove('publication/list');
  270. >
  271. >When you start using the HTML cache, you need to keep a clear view of the dependencies between the model and the actions, so that new errors don't appear because of a misunderstood relationship. Keep in mind that all the actions that modify the model should probably contain a bunch of calls to the `remove()` method if the HTML cache is used somewhere in the application.
  272. >
  273. >And, if you don't want to damage your brain with too difficult an analysis, you can always clear the whole cache each time you update the database...
  274. ### Clearing several cache parts at once (new in symfony 1.1)
  275. The `remove()` method accepts keys with wildcards. It allows you to remove several cache parts with a single call. You can do for instance:
  276. [php]
  277. $cacheManager->remove('user/show?id=*'); // Remove for all user records
  278. Another good example is with applications handling several languages, where the language code appears in all URLs. The URL to a user profile page should look like this:
  279. http://www.myapp.com/en/user/show/id/12
  280. To remove the cached profile of the user having an `id` of `12` in all languages, you can simply call:
  281. [php]
  282. $cache->remove('user/show?sf_culture=*&id=12');
  283. This also works for partials:
  284. [php]
  285. $cacheManager->remove('@sf_cache_partial?module=user&action=_my_partial&sf_cache_key=*'); // Remove for all keys
  286. The `remove()` method accepts two additional parameters, allowing you to define which hosts and vary headers you want to clear the cache for. This is because symfony keeps one cache version for each host and vary headers, so that two applications sharing the same code base but not the same hostname use different caches. This can be of great use, for instance, when an application interprets the subdomain as a request parameter (like `http://php.askeet.com` and `http://life.askeet.com`). If you don't set the last two parameters, symfony will remove the cache for the current host and for the `all` vary header. Alternatively, if you want to remove the cache for another host, call `remove()` as follows:
  287. [php]
  288. $cacheManager->remove('user/show?id=*'); // Remove records for the current host and all users
  289. $cacheManager->remove('user/show?id=*', 'life.askeet.com'); // Remove records for the host life.askeet.com and all users
  290. $cacheManager->remove('user/show?id=*', '*'); // Remove records for every host and all users
  291. The `remove()` method works in all the caching strategies that you can define in the `factories.yml` (not only `sfFileCache`, but also `sfAPCCache`, `sfEAcceleratorCache`, `sfMemcacheCache`, `sfSQLiteCache`, and `sfXCacheCache`).
  292. ### Clearing cache across applications (new in symfony 1.1)
  293. Clearing the cache across applications can be a problem. For instance, if an administrator modifies a record in the `user` table in a `backend` application, all the actions depending on this user in the `frontend` application need to be cleared from the cache. But the view cache manager available in the `backend` application doesn't know the `frontend` application routing rules (applications are isolated from each other). So you can't write this code in the backend:
  294. [php]
  295. $cacheManager = sfContext::getInstance()->getViewCacheManager(); // Retrieves the view cache manager of the backend
  296. $cacheManager->remove('user/show?id=12'); // The pattern is not found, since the template is cached in the frontend
  297. The solution is to initialize a `sfCache` object by hand, with the same settings as the frontend cache manager. Fortunately, all cache classes in symfony provide a `removePattern` method providing the same service as the view cache manager's `remove`.
  298. For instance, if the `backend` application needs to clear the cache of the `user/show` action in the `frontend` application for the user of `id` `12`, it can use the following:
  299. [php]
  300. $frontend_cache_dir = sfConfig::get('sf_root_cache_dir').DIRECTORY_SEPARATOR.'frontend'.DIRECTORY_SEPARATOR.SF_ENV.DIRECTORY_SEPARATOR.'template';
  301. $cache = new sfFileCache(array('cache_dir' => $frontend_cache_dir)); // Use the same settings as the ones defined in the frontend factories.yml
  302. $cache->removePattern('user/show?id=12');
  303. For different caching strategies, you just need to change the cache object initialization, but the cache removal process remains the same:
  304. [php]
  305. $cache = new sfMemcacheCache(array('prefix' => 'frontend'));
  306. $cache->removePattern('user/show?id=12');
  307. Testing and Monitoring Caching
  308. ------------------------------
  309. 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.
  310. ### Building a Staging Environment
  311. 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 `on`.
  312. To set it up, edit the `settings.yml` file of your application and add the lines shown in Listing 12-12 at the top.
  313. Listing 12-12 - Settings for a `staging` Environment, in `frontend/config/settings.yml`
  314. staging:
  315. .settings:
  316. web_debug: on
  317. cache: on
  318. In addition, create a new front controller by copying the production one (probably `myproject/web/index.php`) to a new `frontend_staging.php`. Edit it to change the arguments passed to the `getApplicationConfiguration()` method, as follows:
  319. [php]
  320. $configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'staging', true);
  321. That's it--you have a new environment. Use it by adding the front controller name after the domain name:
  322. http://myapp.example.com/frontend_staging.php/user/list
  323. ### Monitoring Performance
  324. 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.
  325. 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.
  326. 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.
  327. Figure 12-4 - Web debug toolbar for pages using caching
  328. ![Web debug toolbar for pages using caching](/images/book/F1204.png "Web debug toolbar for pages using caching")
  329. 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.
  330. ### Benchmarking
  331. 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 `off`.
  332. 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.
  333. ### Identifying Cache Parts
  334. 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.
  335. Figure 12-5 - Identification for cached elements in a page
  336. ![Identification for cached elements in a page](/images/book/F1205.png "Identification for cached elements in a page")
  337. HTTP 1.1 and Client-Side Caching
  338. --------------------------------
  339. The HTTP 1.1 protocol defines a bunch of headers that can be of great use to further speed up an application by controlling the browser's cache system.
  340. The HTTP 1.1 specifications of the World Wide Web Consortium (W3C, [http://www. w3.org/Protocols/rfc2616/rfc2616-sec14.html](http://www. w3.org/Protocols/rfc2616/rfc2616-sec14.html)) describe these headers in detail. If an action has caching enabled, and it uses the `with_layout` option, it can use one or more of the mechanisms described in the following sections.
  341. Even if some of the browsers of your website's users may not support HTTP 1.1, there is no risk in using the HTTP 1.1 cache features. A browser receiving headers that it doesn't understand simply ignores them, so you are advised to set up the HTTP 1.1 cache mechanisms.
  342. In addition, HTTP 1.1 headers are also understood by proxies and caching servers. Even if a user's browser doesn't understand HTTP 1.1, there can be a proxy in the route of the request to take advantage of it.
  343. ### Adding an ETag Header to Avoid Sending Unchanged Content
  344. When the ETag feature is enabled, the web server adds to the response a special header containing a signature of the response itself.
  345. ETag: "1A2Z3E4R5T6Y7U"
  346. The user's browser will store this signature, and send it again together with the request the next time it needs the same page. If the new signature shows that the page didn't change since the first request, the server doesn't send the response back. Instead, it just sends a `304: Not modified` header. It saves CPU time (if gzipping is enabled for example) and bandwidth (page transfer) for the server, and time (page transfer) for the client. Overall, pages in a cache with an ETag are even faster to load than pages in a cache without an ETag.
  347. In symfony, you enable the ETag feature for the whole application in `settings.yml`. Here is the default ETag setting:
  348. all:
  349. .settings:
  350. etag: on
  351. For actions in a cache with layout, the response is taken directly from the `cache/` directory, so the process is even faster.
  352. ### Adding a Last-Modified Header to Avoid Sending Still Valid Content
  353. When the server sends the response to the browser, it can add a special header to specify when the data contained in the page was last changed:
  354. Last-Modified: Sat, 23 Nov 2006 13:27:31 GMT
  355. Browsers can understand this header and, when requesting the page again, add an `If-Modified` header accordingly:
  356. If-Modified-Since: Sat, 23 Nov 2006 13:27:31 GMT
  357. The server can then compare the value kept by the client and the one returned by its application. If they match, the server returns a `304: Not modified` header, saving bandwidth and CPU time, just as with ETags.
  358. In symfony, you can set the `Last-Modified` response header just as you would for another header. For instance, you can use it like this in an action:
  359. [php]
  360. $this->getResponse()->setHttpHeader('Last-Modified', $this->getResponse()->getDate($timestamp));
  361. This date can be the actual date of the last update of the data used in the page, given from your database or your file system. The `getDate()` method of the `sfResponse` object converts a timestamp to a formatted date in the format needed for the `Last-Modified` header (RFC1123).
  362. ### Adding Vary Headers to Allow Several Cached Versions of a Page
  363. Another HTTP 1.1 header is `Vary`. It defines which parameters a page depends on, and is used by browsers and proxies to build cache keys. For example, if the content of a page depends on cookies, you can set its `Vary` header as follows:
  364. Vary: Cookie
  365. Most often, it is difficult to enable caching on actions because the page may vary according to the cookie, the user language, or something else. If you don't mind expanding the size of your cache, set the `Vary` header of the response properly. This can be done for the whole application or on a per-action basis, using the `cache.yml` configuration file or the `sfResponse` related method as follows:
  366. [php]
  367. $this->getResponse()->addVaryHttpHeader('Cookie');
  368. $this->getResponse()->addVaryHttpHeader('User-Agent');
  369. $this->getResponse()->addVaryHttpHeader('Accept-Language');
  370. Symfony will store a different version of the page in the cache for each value of these parameters. This will increase the size of the cache, but whenever the server receives a request matching these headers, the response is taken from the cache instead of being processed. This is a great performance tool for pages that vary only according to request headers.
  371. ### Adding a Cache-Control Header to Allow Client-Side Caching
  372. Up to now, even by adding headers, the browser keeps sending requests to the server even if it holds a cached version of the page. You can avoid that by adding `Cache-Control` and `Expires` headers to the response. These headers are disabled by default in PHP, but symfony can override this behavior to avoid unnecessary requests to your server.
  373. As usual, you trigger this behavior by calling a method of the `sfResponse` object. In an action, define the maximum time a page should be cached (in seconds):
  374. [php]
  375. $this->getResponse()->addCacheControlHttpHeader('max_age=60');
  376. You can also specify under which conditions a page may be cached, so that the provider's cache does not keep a copy of private data (like bank account numbers):
  377. [php]
  378. $this->getResponse()->addCacheControlHttpHeader('private=True');
  379. Using `Cache-Control` HTTP directives, you get the ability to fine-tune the various cache mechanisms between your server and the client's browser. For a detailed review of these directives, see the W3C `Cache-Control` specifications.
  380. One last header can be set through symfony: the `Expires` header:
  381. [php]
  382. $this->getResponse()->setHttpHeader('Expires', $this->getResponse()->getDate($timestamp));
  383. >**CAUTION**
  384. >The major consequence of turning on the `Cache-Control` mechanism is that your server logs won't show all the requests issued by the users, but only the ones actually received. If the performance gets better, the apparent popularity of the site may decrease in the statistics.
  385. Summary
  386. -------
  387. The cache system provides variable performance boosts according to the cache type selected. From the best gain to the least, the cache types are as follows:
  388. * Super cache
  389. * Action cache with layout
  390. * Action cache without layout
  391. * Fragment cache in the template
  392. In addition, partials and components can be cached as well.
  393. If changing data in the model or in the session forces you to erase the cache for the sake of coherence, you can do it with a fine granularity for optimum performance--erase only the elements that have changed, and keep the others.
  394. Remember to test all the pages where caching is enabled with extra care, as new bugs may appear if you cache the wrong elements or if you forget to clear the cache when you update the underlying data. A staging environment, dedicated to cache testing, is of great use for that purpose.
  395. Finally, make the best of the HTTP 1.1 protocol with symfony's advanced cache-tweaking features, which will involve the client in the caching task and provide even more performance gains.