|
| 1 | +.. index:: |
| 2 | + single: Cache; Invalidation |
| 3 | + single: Cache; Tags |
| 4 | + single: Cache; Namespaces |
| 5 | + |
| 6 | +Cache Invalidation |
| 7 | +================== |
| 8 | + |
| 9 | +Cache invalidation is the process of removing all cached items related to a |
| 10 | +change in the state of your model. The most basic kind of invalidation is direct |
| 11 | +items deletion. But when the state of a primary resource has spread accross |
| 12 | +several cached items, keeping them in sync can be difficult. |
| 13 | + |
| 14 | +The Symfony Cache component provides three mechanisms to help solve this problem: |
| 15 | + |
| 16 | +* Tags based invalidation for managing data dependencies; |
| 17 | +* Namespace based invalidation for context and sub-contexts dependend data; |
| 18 | +* Expiration based invalidation for time related dependencies. |
| 19 | + |
| 20 | +.. versionadded:: 3.2 |
| 21 | + Tags and namespace based invalidation was introduced in Symfony 3.2. |
| 22 | + |
| 23 | +Using Cache Tags |
| 24 | +---------------- |
| 25 | + |
| 26 | +To benefit from the tags based invalidation, your responsiblity is to attach the |
| 27 | +proper tags to each cached items. Each tag is a plain string identifier that you |
| 28 | +can use to trigger the removal of all items that had this tag attached to them. |
| 29 | + |
| 30 | +To attach tags to cached items, you need to use the |
| 31 | +:method:`Symfony\\Component\\Cache\\CacheItem::tag` method that is implemented by |
| 32 | +cache items, as returned by cache adapters:: |
| 33 | + |
| 34 | + $item = $cache->getItem('cache_key'); |
| 35 | + // ... |
| 36 | + // add one or more tags |
| 37 | + $item->tag('tag_1'); |
| 38 | + $item->tag(array('tag_2', 'tag_3')); |
| 39 | + $cache->save($item); |
| 40 | + |
| 41 | +If `$cache` implements :class:`Symfony\\Component\\Cache\\TagAwareAdapterInterface`, |
| 42 | +you can invalidate the cached items by calling |
| 43 | +:method:`Symfony\\Component\\Cache\\TagAwareAdapterInterface::invalidateTags`:: |
| 44 | + |
| 45 | + // invalidate all items related to `tag_1` |
| 46 | + $cache->invalidateTags('tag_2'); |
| 47 | + |
| 48 | + // or invalidate all items related to `tag_1` or `tag_3` |
| 49 | + $cache->invalidateTags(array('tag_1', 'tag_3')); |
| 50 | + |
| 51 | +Of course when you know the cache key, it's better to call |
| 52 | +``$cache->deleteItem('cache_key');``, but this key is sometimes hard to track |
| 53 | +and this is where cache tags become useful. |
| 54 | + |
| 55 | +Tag Aware Adapters |
| 56 | +~~~~~~~~~~~~~~~~~~ |
| 57 | + |
| 58 | +To store tags, you need to wrap a cache adapter with the |
| 59 | +:class:`Symfony\\Component\\Cache\\Adapter\\TagAwareAdapter` class or implement |
| 60 | +:class:`Symfony\\Component\\Cache\\Adapter\\TagAwareAdapterInterface` and its only |
| 61 | +:method:`Symfony\\Component\\Cache\\Adapter\\TagAwareAdapterInterface::invalidateTags` |
| 62 | +method by yourself. |
| 63 | + |
| 64 | +The :class:`Symfony\\Component\\Cache\\Adapter\\TagAwareAdapter` class implements |
| 65 | +instantaneous invalidation. It needs one or two cache adapters: the required one |
| 66 | +is used to store cached items; the second one is used to store tags and their |
| 67 | +invalidation version number (conceptually similar to their latest invalidation |
| 68 | +date). When only one adapter is used, items and tags are all stored in the same |
| 69 | +place. By using two adapters, you can e.g. store some big cached items on the |
| 70 | +filesystem or in the database, and keep tags in a Redis database to sync all your |
| 71 | +fronts and have very fast invalidation checks:: |
| 72 | + |
| 73 | + $cache = new TagAwareAdapter( |
| 74 | + // Adapter for cached items |
| 75 | + new FilesystemAdapter(), |
| 76 | + // Adapter for tags |
| 77 | + new RedisAdapter('redis://localhost') |
| 78 | + ); |
| 79 | + |
| 80 | +Using Cache Namespaces |
| 81 | +---------------------- |
| 82 | + |
| 83 | +By using adapters that implement the |
| 84 | +:method:`Symfony\\Component\\Cache\\Adapter\\ForkableAdapterInterface::fork` |
| 85 | +method from :class:`Symfony\\Component\\Cache\\Adapter\\ForkableAdapterInterface`, |
| 86 | +you can create context-dependent variations of your cached items:: |
| 87 | + |
| 88 | + $childCache = $parentCache->fork('child-namespace-name'); |
| 89 | + $siblingCache = $parentCache->fork('sibling-namespace-name'); |
| 90 | + $subChildCache = $childCache->fork('sub-child-namespace-name'); |
| 91 | + |
| 92 | +Clearing a parent adapter also clears all its forks:: |
| 93 | + |
| 94 | + $childCache->clear(); |
| 95 | + // both $childCache and $subChildCache are now empty |
| 96 | + // but $parentCache and $siblingCache weren't affected |
| 97 | + |
| 98 | +.. note:: |
| 99 | + |
| 100 | + Invalidating by tags affects all forks. |
| 101 | + |
| 102 | +Using Cache Expiration |
| 103 | +---------------------- |
| 104 | + |
| 105 | +If your data is valid only for a limited period of time, you can specify their |
| 106 | +lifetime or their expiration date with the PSR-6 interface, as explained in the |
| 107 | +Cache Items article. |
0 commit comments