Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.2k
[Cache] Improve the docs about cache namespaces#20966
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -91,30 +91,61 @@ Creating Sub-Namespaces | ||
Cache sub-namespaces were introduced in Symfony 7.3. | ||
Sometimes you need to create context-dependent variations of data that should be | ||
cached. For example, the data used to render a dashboard page may be expensive | ||
to generate and unique per user, so you can't cache the same data for everyone. | ||
In such cases, Symfony allows you to create different cache contexts using | ||
namespaces. A cache namespace is an arbitrary string that identifies a set of | ||
related cache items. All cache adapters provided by the component implement the | ||
:class:`Symfony\\Contracts\\Cache\\NamespacedPoolInterface`, which provides the | ||
:method:`Symfony\\Contracts\\Cache\\NamespacedPoolInterface::withSubNamespace` | ||
method. | ||
This method allows you to namespace cached items by transparently prefixing their keys:: | ||
$userCache = $cache->withSubNamespace(sprintf('user-%d', $user->getId())); | ||
$userCache->get('dashboard_data', function (ItemInterface $item): string { | ||
$item->expiresAfter(3600); | ||
return '...'; | ||
}); | ||
In this example, the cache itemusesthe ``dashboard_data`` key, but it will be | ||
stored internally undera namespace based onthecurrent user ID. This is handled | ||
automatically, so you **don’t** need to manually prefix keys like ``user-27.dashboard_data``. | ||
There are no guidelines or restrictions on how to define cache namespaces. | ||
You can make them as granular or as generic as your application requires: | ||
$localeCache = $cache->withSubNamespace($request->getLocale()); | ||
$flagCache = $cache->withSubNamespace( | ||
$featureToggle->isEnabled('new_checkout') ? 'checkout-v2' : 'checkout-v1' | ||
); | ||
$channel = $request->attributes->get('_route')?->startsWith('api_') ? 'api' : 'web'; | ||
$channelCache = $cache->withSubNamespace($channel); | ||
.. tip:: | ||
You can even combine cache namespaces with :ref:`cache tags <cache-using-cache-tags>` | ||
for more advanced needs. | ||
There is no built-in way to invalidate caches by namespace. Instead, the recommended | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Couldn't this be achieved with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. It depends on the implementation: some support this, others not (they might return false or clear the full pool depending on the implementation.) | ||
approach is to change the namespace itself. For this reason, it's common to include | ||
static or dynamic versioning data in the cache namespace:: | ||
// for simple applications, an incrementing static version number may be enough | ||
$userCache = $cache->withSubNamespace(sprintf('v1-user-%d', $user->getId())); | ||
// other applications may use dynamic versioning based on the date (e.g. monthly) | ||
$userCache = $cache->withSubNamespace(sprintf('%s-user-%d', date('Ym'), $user->getId())); | ||
// or even invalidate the cache when the user data changes | ||
$checksum = hash('xxh128', $user->getUpdatedAt()->format(DATE_ATOM)); | ||
$userCache = $cache->withSubNamespace(sprintf('user-%d-%s', $user->getId(), $checksum)); | ||
.. _cache_stampede-prevention: | ||