1
1
.. index ::
2
2
single: Cache; Invalidation
3
3
single: Cache; Tags
4
+ single: Cache; Hierarchical
4
5
5
6
Cache Invalidation
6
7
==================
@@ -10,13 +11,14 @@ change in the state of your model. The most basic kind of invalidation is direct
10
11
items deletion. But when the state of a primary resource has spread accross
11
12
several cached items, keeping them in sync can be difficult.
12
13
13
- The Symfony Cache component provides two mechanisms to help solve this problem:
14
+ The Symfony Cache component provides three mechanisms to help solve this problem:
14
15
15
16
* Tags based invalidation for managing data dependencies;
17
+ * Hierarchical invalidation for context dependent data;
16
18
* Expiration based invalidation for time related dependencies.
17
19
18
20
.. versionadded :: 3.2
19
- Tags based invalidation was introduced in Symfony 3.2.
21
+ Tags based and hierarchical invalidations were introduced in Symfony 3.2.
20
22
21
23
Using Cache Tags
22
24
----------------
@@ -81,6 +83,48 @@ your fronts and have very fast invalidation checks::
81
83
new RedisAdapter('redis://localhost')
82
84
);
83
85
86
+ Using Cache Hierarchies
87
+ -----------------------
88
+
89
+ By using adapters that implement
90
+ :class: `Symfony\\ Component\\ Cache\\ Adapter\\ ContextAwareAdapterInterface `,
91
+ you can create nested cache pools from existing ones. Each nested cache pool
92
+ deals with its own set of cached items, but clearing their parents clears them
93
+ recursively.
94
+
95
+ Nested cache pools are derivated from their parents by calling
96
+ :method: `Symfony\\ Component\\ Cache\\ Adapter\\ ContextAwareAdapterInterface::withContext `.
97
+ The method takes a single ``$context `` argument, which is a string that
98
+ identifies and isolates their cached items subsets. Appart from this, derivated
99
+ pools share everything with their parents, esp. any database connection they might
100
+ manage.
101
+
102
+ You can use such contextualized hierarchies everywhere you would otherwise
103
+ directly prefix your cache keys, this prefix being the "context" of the cached
104
+ items that use it in their keys.
105
+
106
+ For example, you could use hierachical pools to cache language dependent
107
+ variations of your content::
108
+
109
+ $databaseCachePool = new PdoAdapter($pdoConnection);
110
+
111
+ $lang = 'en';
112
+ $enCachePool = $databaseCachePool->withContext($lang);
113
+
114
+ $lang = 'fr';
115
+ $frCachePool = $databaseCachePool->withContext($lang);
116
+
117
+ // Get the same "front-page" item but from different lang context
118
+ $enFrontPage = $enCachePool->getItem('front-page');
119
+ $frFrontPage = $frCachePool->getItem('front-page');
120
+
121
+ // This clears also $enCachePool and $frCachePool
122
+ $databaseCachePool->clear();
123
+
124
+ .. note ::
125
+
126
+ Invalidating by tags affects both parents and children pools.
127
+
84
128
Using Cache Expiration
85
129
----------------------
86
130
0 commit comments