| 329 | | 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 after the fact. 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: |
|---|
| | 329 | 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: |
|---|
| 395 | | ### Template Cache Key |
|---|
| 396 | | |
|---|
| 397 | | Whatever caching storage strategy you use, symfony will use common naming rules for identifying template cache parts. Put it simply, symfony uses the external URL form of the internal URI as a key to a template cache part. For instance, the template cache key of a page called with: |
|---|
| 398 | | |
|---|
| 399 | | http://www.myapp.com/user/show/id/12 |
|---|
| 400 | | |
|---|
| 401 | | is: |
|---|
| 402 | | |
|---|
| 403 | | www_myapp_com/all/user/show/id/12 |
|---|
| 404 | | |
|---|
| 405 | | The first part of the cache key is the host name, where dots are replaced by underscores for compatibility with file systems. Then comes the list of VARY headers (usually a single `all/` keyword), and then the page external URL. |
|---|
| 406 | | |
|---|
| 407 | | What the cache engine does with htis key depends no the caching strategy defined in the `factories.yml`. Let's see what happens with the default cache factory: `sFileCache`. When using the filesystem to store cache parts, symfony writes all template cache files under a special subdirectory of the `cache/` directory called `sf_template_cache_dir`: |
|---|
| 408 | | |
|---|
| 409 | | cache/ # sf_root_cache_dir |
|---|
| 410 | | [APP_NAME]/ # sf_base_cache_dir |
|---|
| 411 | | [ENV_NAME]/ # sf_cache_dir |
|---|
| 412 | | template/ # sf_template_cache_dir |
|---|
| 413 | | |
|---|
| 414 | | For instance, the template cache of the `user/show/id/12` page is stored in: |
|---|
| 415 | | |
|---|
| 416 | | cache/frontend/prod/template/www_myapp_com/all/user/show/id/12.cache |
|---|
| 417 | | \----filecache config----/\---unique template cache key---/ |
|---|
| 418 | | |
|---|
| 419 | | ### Clearing template cache from a cache key (new in symfony 1.1) |
|---|
| 420 | | |
|---|
| 421 | | If you know the cache key of a template cache part, symfony offers an alternative to using the `remove` method of the view cache manager, called `removePattern`: |
|---|
| 422 | | |
|---|
| 423 | | [php] |
|---|
| 424 | | $cacheManager->remove('user/show?id=12'); // Uses internal URI |
|---|
| 425 | | // Is equivalent to |
|---|
| 426 | | $cacheManager->removePattern('www_myapp_com/all/user/show/id/12'); // Uses cache key |
|---|
| 427 | | |
|---|
| 428 | | The great advantage of `removePattern` is that it accepts keys with wildcards. You can do for instance: |
|---|
| 429 | | |
|---|
| 430 | | [php] |
|---|
| 431 | | $cacheManager->removePattern('*/all/user/show/id/12'); // Remove for all hosts |
|---|
| 432 | | $cacheManager->removePattern('*/all/user/show/*/*'); // Remove for all user records |
|---|
| 433 | | |
|---|
| 434 | | Another good example if with applications handling several languages, where the language code appears in all URLs. The link to a user profile page should look like this: |
|---|
| | 394 | ### Clearing several cache parts at once (new in symfony 1.1) |
|---|
| | 395 | |
|---|
| | 396 | The `remove()` method accepts keys with wildcards. It allows you to remove several cache parts with a single call. You can do for instance: |
|---|
| | 397 | |
|---|
| | 398 | [php] |
|---|
| | 399 | $cacheManager->remove('user/show?id=*'); // Remove for all user records |
|---|
| | 400 | |
|---|
| | 401 | 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: |
|---|
| 441 | | $cache->removePattern('*/all/*/user/show/id/12'); |
|---|
| 442 | | |
|---|
| 443 | | The `removePattern()` method is available for all the caching strategies that you can define in the `factories.yml` (not only `sfFileCache`, but also `sfAPCCache`, `sfEAcceleratorCache`, `sfMemcacheCache`, `sfSQLiteCache`, and `sfXCacheCache`). |
|---|
| | 408 | $cache->removePattern('user/show?sf_culture=*&id=12'); |
|---|
| | 409 | |
|---|
| | 410 | This also works for partials: |
|---|
| | 411 | |
|---|
| | 412 | [php] |
|---|
| | 413 | $cacheManager->remove('@sf_cache_partial?module=user&action=_my_partial&sf_cache_key=*'); // Remove for all keys |
|---|
| | 414 | |
|---|
| | 415 | 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: |
|---|
| | 416 | |
|---|
| | 417 | [php] |
|---|
| | 418 | $cacheManager->remove('user/show?id=*'); // Remove for all user records for the current host |
|---|
| | 419 | $cacheManager->remove('user/show?id=*', 'life.askeet.com'); // Remove for all user records for the host life.askeet.com |
|---|
| | 420 | $cacheManager->remove('user/show?id=*', '*'); // Remove for all user records for every hosts |
|---|
| | 421 | |
|---|
| | 422 | 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`). |
|---|