Skip to content

Instantly share code, notes, and snippets.

@ClaraLeigh
Created January 13, 2025 01:44
Show Gist options
  • Save ClaraLeigh/134c1beeb9bafb7f58c94a2acfd04bdd to your computer and use it in GitHub Desktop.
Save ClaraLeigh/134c1beeb9bafb7f58c94a2acfd04bdd to your computer and use it in GitHub Desktop.
Laravel redis cache, forget all with prefix
\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);
}
});
@ClaraLeigh
Copy link
Author

Quick hack to clear all cache keys that start with a prefix, add to the AppServiceProvider.

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