Skip to content

[Cache] Enable namespace-based invalidation by prefixing keys with backend-native namespace separators #59813

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 23, 2025

Conversation

nicolas-grekas
Copy link
Member

@nicolas-grekas nicolas-grekas commented Feb 19, 2025

Q A
Branch? 7.3
Bug fix? no
New feature? yes
Deprecations? no
Issues Fix #45599
License MIT

This PR adds a NamespacedPoolInterface that describes how one can add namespace prefixes to cache pool keys.

This solves #45599 by allowing to use the : separator for Redis:

$cache->withSubNamespace('some-prefix')->get($key, fn () => ...);

Most importantly, this enables namespace-based cache invalidation, typically with namespaces generated by hashing some context.

@stof
Copy link
Member

stof commented Feb 19, 2025

If pushNamespace returns a new adapter, what is the use case for popNamespace (which also returns a new adapter, and so won't return the "parent" ArrayAdapter for instance) ?

@nicolas-grekas
Copy link
Member Author

@stof you're totally right! I removed popNamespace and renamed the pushNamespace method to withSubNamespace.

@nicolas-grekas nicolas-grekas force-pushed the cache-ns-relax branch 4 times, most recently from 904427d to 349fa7b Compare February 20, 2025 09:51
@nicolas-grekas
Copy link
Member Author

Ready. Tests should be green after merge.

@nicolas-grekas nicolas-grekas force-pushed the cache-ns-relax branch 2 times, most recently from b733583 to 80e4605 Compare February 21, 2025 07:52
@nicolas-grekas nicolas-grekas force-pushed the cache-ns-relax branch 3 times, most recently from da4eacf to b9b63de Compare March 13, 2025 15:23
@nicolas-grekas
Copy link
Member Author

PR ready (failures will be fixed by merging)

@nicolas-grekas
Copy link
Member Author

Any comment @symfony/mergers ?

Copy link
Member

@kbond kbond left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

@nicolas-grekas nicolas-grekas merged commit 6ef1ab4 into symfony:7.3 Mar 23, 2025
8 of 11 checks passed
@nicolas-grekas nicolas-grekas deleted the cache-ns-relax branch March 23, 2025 17:41
@fabpot fabpot mentioned this pull request May 2, 2025
@IndraGunawan
Copy link
Contributor

thanks for this great feature @nicolas-grekas

what type-hint that should we use in the constructor to have this ability with autowiring? or NamespacedPoolInterface should be extends CacheInterface ?

@nicolas-grekas
Copy link
Member Author

Can you try using an intersection type of both? That should work!

@IndraGunawan
Copy link
Contributor

i did (https://symfony-devs.slack.com/archives/C3EQ7S3MJ/p1748498675962069?thread_ts=1748446854.832469&cid=C3EQ7S3MJ) but it does not work

[Symfony\Component\DependencyInjection\Exception\RuntimeException]                                                                                                                                                  
  Cannot autowire service "App\Command\TestCommand": argument "$appCache" of method "__construct()" has type "Symfony\Contracts\Cache\CacheInterface&Symfony\Contracts\Cache\NamespacedPoolInterface" but this class  
   was not found.      

@nicolas-grekas
Copy link
Member Author

Can you open an issue?

@IndraGunawan
Copy link
Contributor

issue created #60583

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Cache] Allow colon in key key (with redis at least)
6 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy