1
1
.. index ::
2
2
single: Cache; Invalidation
3
3
single: Cache; Tags
4
+ single: Cache; Namespaces
4
5
5
6
Cache Invalidation
6
7
==================
@@ -13,10 +14,11 @@ several cached items, keeping them in sync can be difficult.
13
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
+ * Namespace based 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 and namespace based invalidation was introduced in Symfony 3.2.
20
22
21
23
Using Cache Tags
22
24
----------------
@@ -81,6 +83,39 @@ your fronts and have very fast invalidation checks::
81
83
new RedisAdapter('redis://localhost')
82
84
);
83
85
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
+
84
119
Using Cache Expiration
85
120
----------------------
86
121
0 commit comments