[13.x] Add Redis cache prefix clearing#60705
Draft
DGarbs51 wants to merge 1 commit into
Draft
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds an opt-in way for Redis cache stores to clear only keys managed by the configured Laravel cache prefix instead of flushing the selected Redis database.
It introduces:
Illuminate\Contracts\Cache\CanFlushPrefixRedisStore::flushPrefix()Repository::flushPrefix()/supportsFlushingPrefix()php artisan cache:clear --prefixcache.stores.redis.flush_scope, configurable withREDIS_CACHE_FLUSH_SCOPE=prefixThe existing Redis cache
flush()behavior is unchanged and continues to callFLUSHDBunless the new prefix mode is explicitly requested.Why
Laravel's default Redis configuration has historically encouraged isolating cache data in a separate logical Redis database. That is increasingly difficult with managed Redis-compatible services:
SELECTcannot be used in cluster mode: https://redis.io/docs/latest/commands/select/SELECTis accepted only for compatibility and does not perform an operation: https://redis.io/docs/latest/commands/select/FLUSHDB/FLUSHALLalways flush all keys across the cluster: https://docs.aws.amazon.com/AmazonElastiCache/latest/dg/SupportedCommands.htmlThat means applications running on modern Redis Cluster, Redis Cloud, Redis Software, or ElastiCache Serverless deployments need a first-party way to clear only Laravel cache keys without clearing unrelated data that shares database zero.
Backwards Compatibility
This is intended to be backwards compatible:
RedisStore::flush()still callsflushdb().cache:clearstill uses the existing flush behavior by default.cache:clear --prefixis passed or whencache.stores.redis.flush_scopeis set toprefix.Tests
vendor/bin/pint --dirtygit diff --checkvendor/bin/phpunit tests/Cache/CacheRedisStoreTest.php tests/Cache/ClearCommandTest.php tests/Cache/CacheEventsTest.phpvendor/bin/phpunit tests/CacheThe full cache suite passes. PHPUnit reports the existing notices/skips from the suite: 8 notices and 13 skipped tests.