Created
January 13, 2025 01:44
-
-
Save ClaraLeigh/134c1beeb9bafb7f58c94a2acfd04bdd to your computer and use it in GitHub Desktop.
Laravel redis cache, forget all with prefix
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
\Cache::macro('forgetPrefix', function ($prefix) { | |
// if cache store is redis | |
if (config('cache.default') !== 'redis') { | |
\Log::warning('Cache clear attempted on non-redis cache store'); | |
return; | |
} | |
$connection = \Illuminate\Support\Facades\Redis::connection(config('cache.stores.redis.connection')); | |
$client = $connection->client(); | |
$client_prefix = config('cache.prefix'); | |
$prefix = $client_prefix.$prefix; | |
$list = $client->keys($prefix.'*'); | |
foreach ($list as $key) { | |
$key = \Illuminate\Support\Str::after($key, $client_prefix); | |
cache()->forget($key); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Quick hack to clear all cache keys that start with a prefix, add to the AppServiceProvider.