Skip to content

Commit d30112d

Browse files
[Cache] Add chapter about invalidation, tags, etc.
1 parent b97d442 commit d30112d

File tree

4 files changed

+147
-8
lines changed

4 files changed

+147
-8
lines changed

components/cache.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
The Cache Component
77
===================
88

9-
The Cache component provides a strict `PSR-6`_ implementation for adding
9+
The Cache component provides an extended `PSR-6`_ implementation for adding
1010
cache to your applications. It is designed to have a low overhead and it
1111
ships with ready to use adapters for the most common caching backends.
1212

@@ -42,7 +42,7 @@ meaning of some key concepts:
4242
Basic Usage
4343
-----------
4444

45-
This component is a strict implementation of `PSR-6`_, which means that the API
45+
This component is an implementation of `PSR-6`_, which means that its basic API
4646
is the same as defined in the standard. Before starting to cache information,
4747
create the cache pool using any of the built-in adapters. For example, to create
4848
a filesystem-based cache, instantiate :class:`Symfony\\Component\\Cache\\Adapter\\FilesystemAdapter`::
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
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 Namespaces
85+
----------------------
86+
87+
By using adapters that implement the
88+
:method:`Symfony\\Component\\Cache\\Adapter\\ForkableAdapterInterface::fork`
89+
method from :class:`Symfony\\Component\\Cache\\Adapter\\ForkableAdapterInterface`,
90+
you can create context-dependent variations of your cached items::
91+
92+
$childCache = $parentCache->fork('child-namespace-name');
93+
$siblingCache = $parentCache->fork('sibling-namespace-name');
94+
$subChildCache = $childCache->fork('sub-child-namespace-name');
95+
96+
Clearing a parent adapter also clears all its forks::
97+
98+
$childCache->clear();
99+
// both $childCache and $subChildCache are now empty
100+
// but $parentCache and $siblingCache weren't affected
101+
102+
.. note::
103+
104+
Invalidating by tags affects all forks.
105+
106+
Using Cache Expiration
107+
----------------------
108+
109+
If your data is valid only for a limited period of time, you can specify their
110+
lifetime or their expiration date with the PSR-6 interface, as explained in the
111+
:doc:`/components/cache/cache_items` article.

components/cache/cache_items.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ In the Cache component they are represented by the
1313
Cache Item Keys and Values
1414
--------------------------
1515

16-
The **key** of a cache item is a UTF-8 encoded string which acts as its
16+
The **key** of a cache item is a plain string which acts as its
1717
identifier, so it must be unique for each cache pool. You can freely choose the
1818
keys, but they should only contain letters (A-Z, a-z), numbers (0-9) and the
1919
``_`` and ``.`` symbols. Other common symbols (such as ``{``, ``}``, ``(``,

components/cache/cache_pools.rst

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
single: APC Cache, APCu Cache
44
single: Doctrine Cache
55
single: Redis Cache
6+
single: PDO Cache, Doctrine DBAL Cache
67

78
Cache Pools
89
===========
@@ -41,7 +42,7 @@ Filesystem Cache Adapter
4142
~~~~~~~~~~~~~~~~~~~~~~~~
4243

4344
This adapter is useful when you want to improve the application performance but
44-
can't install tools like APC or Redis in the server. This adapter stores the
45+
can't install tools like APCu or Redis in the server. This adapter stores the
4546
contents as regular files in a set of directories on the local file system::
4647

4748
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
@@ -83,7 +84,7 @@ degrade performance significantly::
8384
Redis Cache Adapter
8485
~~~~~~~~~~~~~~~~~~~
8586

86-
This adapter stores the contents in the memory of the server. Unlike the APCu
87+
This adapter stores the contents in the memory of a Redis server. Unlike the APCu
8788
adapter, it's not limited to the shared memory of the current server, so you can
8889
store contents in a cluster of servers if needed.
8990

@@ -102,6 +103,36 @@ the ``\Redis``, ``\RedisArray``, ``\RedisCluster`` or ``\Predis`` classes::
102103
$defaultLifetime = 0
103104
);
104105

106+
The :method:`Symfony\\Component\\Cache\\Adapter\\RedisAdapter::createConnection`
107+
helper allows creating a connection to a Redis server using a DSN configuration::
108+
109+
$redisConnection = RedisAdapter::createConnection('redis://localhost');
110+
111+
See the method's docblock for more options.
112+
113+
PDO & Doctrine DBAL Cache Adapter
114+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
115+
116+
This adapter stores the cached items a SQL database accessed through a PDO or a
117+
Doctrine DBAL connection::
118+
119+
use Symfony\Component\Cache\Adapter\PdoAdapter;
120+
121+
$cache = new PdoAdapter(
122+
// a PDO, a Doctrine DBAL connection or DSN for lazy connecting through PDO
123+
$databaseConnectionOrDSN,
124+
// the string prefixed to the keys of the items stored in this cache
125+
$namespace = '',
126+
// in seconds; applied to cache items that don't define their own lifetime
127+
// 0 means to store the cache items indefinitely (i.e. until the database is cleared)
128+
$defaultLifetime = 0,
129+
// an array of options for configuring the database connection
130+
$options = array()
131+
);
132+
133+
.. versionadded:: 3.2
134+
The PDO & Doctrine DBAL adapter was introduced in Symfony 3.2.
135+
105136
Chain Cache Adapter
106137
~~~~~~~~~~~~~~~~~~~
107138

@@ -141,9 +172,6 @@ that external cache and save them in the Symfony cache of your application::
141172
The adapter accepts two additional optional arguments: the namespace (``''`` by
142173
default) and the default lifetime (``0`` by default).
143174

144-
Another use case for this adapter is to get statistics and metrics about the
145-
cache hits (``getHits()``) and misses (``getMisses()``).
146-
147175
Doctrine Cache Adapter
148176
~~~~~~~~~~~~~~~~~~~~~~
149177

0 commit comments

Comments
 (0)