11..index ::
22 single: Cache; Invalidation
33 single: Cache; Tags
4+ single: Cache; Namespaces
45
56Cache Invalidation
67==================
@@ -10,13 +11,14 @@ change in the state of your model. The most basic kind of invalidation is direct
1011items deletion. But when the state of a primary resource has spread accross
1112several cached items, keeping them in sync can be difficult.
1213
13- The Symfony Cache component providestwo mechanisms to help solve this problem:
14+ The Symfony Cache component providesthree 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+ Tagsand namespace based invalidation was introduced in Symfony 3.2.
2022
2123Using 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+
84119Using Cache Expiration
85120----------------------
86121