|
| 1 | +.. index:: |
| 2 | + single: Cache; Invalidation |
| 3 | + single: Cache; Tags |
| 4 | + |
| 5 | +Cache Invalidation |
| 6 | +================== |
| 7 | + |
| 8 | +Cache invalidation is the process of removing all cached items related to a |
| 9 | +change in the state of your model. The most basic kind of invalidation is direct |
| 10 | +items deletion. But when the state of a primary resource has spread accross |
| 11 | +several cached items, keeping them in sync can be difficult. |
| 12 | + |
| 13 | +The Symfony Cache component provides three mechanisms to help solve this problem: |
| 14 | + |
| 15 | +* Tags based invalidation for managing data dependencies; |
| 16 | +* Expiration based invalidation for time related dependencies. |
| 17 | + |
| 18 | +.. versionadded:: 3.2 |
| 19 | + Tags based invalidation was introduced in Symfony 3.2. |
| 20 | + |
| 21 | +Using Cache Tags |
| 22 | +---------------- |
| 23 | + |
| 24 | +To benefit from tags based invalidation, you need to attach the proper tags to |
| 25 | +each cached items. Each tag is a plain string identifier that you can use at any |
| 26 | +time to trigger the removal of all items that had this tag attached to them. |
| 27 | + |
| 28 | +To attach tags to cached items, you need to use the |
| 29 | +:method:`Symfony\\Component\\Cache\\CacheItem::tag` method that is implemented by |
| 30 | +cache items, as returned by cache adapters:: |
| 31 | + |
| 32 | + $item = $cache->getItem('cache_key'); |
| 33 | + // ... |
| 34 | + // add one or more tags |
| 35 | + $item->tag('tag_1'); |
| 36 | + $item->tag(array('tag_2', 'tag_3')); |
| 37 | + $cache->save($item); |
| 38 | + |
| 39 | +If `$cache` implements :class:`Symfony\\Component\\Cache\\TagAwareAdapterInterface`, |
| 40 | +you can invalidate the cached items by calling |
| 41 | +:method:`Symfony\\Component\\Cache\\TagAwareAdapterInterface::invalidateTags`:: |
| 42 | + |
| 43 | + // invalidate all items related to `tag_1` |
| 44 | + $cache->invalidateTags('tag_2'); |
| 45 | + |
| 46 | + // or invalidate all items related to `tag_1` or `tag_3` |
| 47 | + $cache->invalidateTags(array('tag_1', 'tag_3')); |
| 48 | + |
| 49 | + // if you know the cache key, you can of course delete directly |
| 50 | + $cache->deleteItem('cache_key');`` |
| 51 | + |
| 52 | +Using tags invalidation is very useful when tracking cache keys becomes difficult. |
| 53 | + |
| 54 | +Tag Aware Adapters |
| 55 | +~~~~~~~~~~~~~~~~~~ |
| 56 | + |
| 57 | +To store tags, you need to wrap a cache adapter with the |
| 58 | +:class:`Symfony\\Component\\Cache\\Adapter\\TagAwareAdapter` class or implement |
| 59 | +:class:`Symfony\\Component\\Cache\\Adapter\\TagAwareAdapterInterface` and its only |
| 60 | +:method:`Symfony\\Component\\Cache\\Adapter\\TagAwareAdapterInterface::invalidateTags` |
| 61 | +method. |
| 62 | + |
| 63 | +The :class:`Symfony\\Component\\Cache\\Adapter\\TagAwareAdapter` class implements |
| 64 | +instantaneous invalidation (time complexity is ``O(N)`` where ``N`` is the number |
| 65 | +of invalidated tags). It needs one or two cache adapters: the first required |
| 66 | +one is used to store cached items; the second optional one is used to store tags |
| 67 | +and their invalidation version number (conceptually similar to their latest |
| 68 | +invalidation date). When only one adapter is used, items and tags are all stored |
| 69 | +in the same place. By using two adapters, you can e.g. store some big cached items |
| 70 | +on the filesystem or in the database and keep tags in a Redis database to sync all |
| 71 | +your fronts and have very fast invalidation checks:: |
| 72 | + |
| 73 | + use Symfony\Component\Cache\Adapter\TagAwareAdapter; |
| 74 | + use Symfony\Component\Cache\Adapter\FilesystemAdapter; |
| 75 | + use Symfony\Component\Cache\Adapter\RedisAdapter; |
| 76 | + |
| 77 | + $cache = new TagAwareAdapter( |
| 78 | + // Adapter for cached items |
| 79 | + new FilesystemAdapter(), |
| 80 | + // Adapter for tags |
| 81 | + new RedisAdapter('redis://localhost') |
| 82 | + ); |
| 83 | + |
| 84 | +Using Cache Expiration |
| 85 | +---------------------- |
| 86 | + |
| 87 | +If your data is valid only for a limited period of time, you can specify their |
| 88 | +lifetime or their expiration date with the PSR-6 interface, as explained in the |
| 89 | +:doc:`/components/cache/cache_items` article. |
0 commit comments