Skip to content

Commit 9f775bb

Browse files
[Cache] Doc forkable adapters, aka namespace based invalidation
1 parent 0ffc261 commit 9f775bb

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

components/cache/cache_invalidation.rst

Lines changed: 37 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; Namespaces
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+
* Namespace based 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 and namespace based invalidation was introduced in Symfony 3.2.
2022

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

86+
Using Cache Namespaces
87+
----------------------
88+
89+
By using adapters that implement the
90+
:method:`Symfony\\Component\\Cache\\Adapter\\ForkableAdapterInterface::fork`
91+
method, you can create context-dependent variations of your cached items.
92+
93+
Forks work by cloning existing adapters into new adapters that share everything
94+
with their parent (esp. the data store connection) but add a ``$namespace``
95+
prefix to all their keys, in a way that makes a cache key in a forked adapter
96+
unable to collide with the same key in its parent adapter.
97+
98+
You can use forks everywhere you would otherwise manually prefix your cache keys
99+
in your code. A typical example is storing different HTML pages per e.g.
100+
``Vary: User-Agent`` or ``Vary: Accept-Encoding`` HTTP headers::
101+
102+
$perUserAgentCache = $httpCache->fork($varyingUserAgent);
103+
$perAcceptEncodingPerUserAgentCache = $perUserAgentCache->fork($varyingAcceptEncoding);
104+
105+
// Here, several $pageItem objects can be stored per $someUrl:
106+
// one per $varyingUserAgent and per $varyingAcceptEncoding
107+
$pageItem = $perAcceptEncodingPerUserAgentCache->getItem($someUrl);
108+
109+
Forks are organized in subtrees so that clearing one branch of the tree clears all
110+
sub-trees recursively::
111+
112+
$httpCache->clear();
113+
// both $perUserAgentCache and $perAcceptEncodingPerUserAgentCache are now also empty
114+
115+
.. note::
116+
117+
Invalidating by tags affects all parents and children forks.
118+
84119
Using Cache Expiration
85120
----------------------
86121

0 commit comments

Comments
 (0)