Skip to content

Commit e899545

Browse files
[Cache] Tell about namespace-based invalidation
1 parent 32fab7e commit e899545

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

components/cache.rst

+27
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,33 @@ generate and return the value::
8484
Use cache tags to delete more than one key at the time. Read more at
8585
:doc:`/components/cache/cache_invalidation`.
8686

87+
Creating Sub-Namespaces
88+
-----------------------
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+
.. versionadded:: 7.3
95+
96+
Support for ``NamespacedPoolInterface`` was added in Symfony 7.3.
97+
98+
This method allows namespacing cached items by transparently prefixing their keys::
99+
100+
$subCache = $cache->withSubNamespace('foo');
101+
102+
$subCache->get('my_cache_key', function (ItemInterface $item): string {
103+
$item->expiresAfter(3600);
104+
105+
return '...';
106+
});
107+
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``.
110+
111+
Sub-namespacing allows implementing namespace-based cache invalidation, where the name
112+
of a namespace is computed by hashing some context info.
113+
87114
.. _cache_stampede-prevention:
88115

89116
Stampede Prevention

0 commit comments

Comments
 (0)