Skip to content

Commit dd864d8

Browse files
[Cache] Doc context aware adapters, aka hierarchical invalidation
1 parent 0ffc261 commit dd864d8

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

components/cache/cache_invalidation.rst

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.. index::
22
single: Cache; Invalidation
33
single: Cache; Tags
4+
single: Cache; Hierarchical
45

56
Cache Invalidation
67
==================
@@ -10,13 +11,14 @@ change in the state of your model. The most basic kind of invalidation is direct
1011
items deletion. But when the state of a primary resource has spread accross
1112
several cached items, keeping them in sync can be difficult.
1213

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:
1415

1516
* Tags based invalidation for managing data dependencies;
17+
* Hierarchical invalidation for context dependent data;
1618
* Expiration based invalidation for time related dependencies.
1719

1820
.. 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.
2022

2123
Using Cache Tags
2224
----------------
@@ -81,6 +83,48 @@ your fronts and have very fast invalidation checks::
8183
new RedisAdapter('redis://localhost')
8284
);
8385

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+
84128
Using Cache Expiration
85129
----------------------
86130

0 commit comments

Comments
 (0)