@@ -87,14 +87,13 @@ generate and return the value::
87
87
Creating Sub-Namespaces
88
88
-----------------------
89
89
90
- All cache adapters provided by the component implement the
91
- :method: `Symfony\\ Contracts\\ Cache\\ NamespacedPoolInterface::withSubNamespace ` method
92
- from the :class: `Symfony\\ Contracts\\ Cache\\ NamespacedPoolInterface `.
93
-
94
90
.. versionadded :: 7.3
95
91
96
- Support for `` NamespacedPoolInterface `` was added in Symfony 7.3.
92
+ Cache sub-namespaces were introduced in Symfony 7.3.
97
93
94
+ All cache adapters provided by the component implement the
95
+ :class: `Symfony\\ Contracts\\ Cache\\ NamespacedPoolInterface ` to provide the
96
+ :method: `Symfony\\ Contracts\\ Cache\\ NamespacedPoolInterface::withSubNamespace ` method.
98
97
This method allows namespacing cached items by transparently prefixing their keys::
99
98
100
99
$subCache = $cache->withSubNamespace('foo');
@@ -105,11 +104,17 @@ This method allows namespacing cached items by transparently prefixing their key
105
104
return '...';
106
105
});
107
106
108
- In this example, cache item keyed ``my_cache_key `` will be transparently stored within
109
- the cache pool under a logical namespace called ``foo ``.
107
+ In this example, the cache item will use the ``my_cache_key `` key, but it will be
108
+ stored internally under the ``foo `` namespace. This is handled transparently for
109
+ you, so you **don't ** need to manually prefix keys like ``foo.my_cache_key ``.
110
+
111
+ This is useful when using namespace-based cache invalidation to isolate or
112
+ invalidate a subset of cached data based on some context. Typical examples
113
+ include namespacing by user ID, locale, or entity ID and hash::
110
114
111
- Sub-namespacing allows implementing namespace-based cache invalidation, where the name
112
- of a namespace is computed by hashing some context info.
115
+ $userCache = $cache->withSubNamespace((string) $userId);
116
+ $localeCache = $cache->withSubNamespace($request->getLocale());
117
+ $productCache = $cache->withSubNamespace($productId.'_'.$productChecksum);
113
118
114
119
.. _cache_stampede-prevention :
115
120
0 commit comments