Looking to hire Laravel developers? Try LaraJobs

laravel-redis-prefix-flush maintained by keepsuit

Description
Redis cache store for Laravel that flushes only the application's keys instead of the whole database
Author
Last update
2026/11/25 10:36 (dev-main)
License
Downloads
79

Comments
comments powered by Disqus

Flush only your application's keys from a shared Redis

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Laravel's Redis cache store flushes with FLUSHDB. When several applications share one Redis or Valkey database and are separated only by key prefix, php artisan cache:clear in one application empties the cache of every application, and cache:clear --locks empties the whole lock database, queues and sessions included with the default configuration. With per-application ACL users that are denied FLUSHDB, both commands fail.

This package replaces the redis cache driver with a store that deletes only the keys of the application's cache.

Installation

composer require keepsuit/laravel-redis-prefix-flush

No configuration is needed: every cache store with 'driver' => 'redis' uses the new store. It is built exactly like Laravel's own Redis store (connection, lock_connection, prefix, serializable_classes) and differs only in flush() and flushLocks(). Stores with other drivers are not affected.

The package registers its store under the redis driver name. A redis store that another service provider resolves before this package registers keeps Laravel's own store, and if another package also extends the redis driver, the extension registered last applies.

Flush scope

flush() deletes, on the cache connection, every key that starts with:

<Redis connection prefix><cache prefix>

With Laravel's default configuration (APP_NAME=Shop), that is shop-database-shop-cache-*. Keys of the same application outside the cache prefix (queues, sessions, Horizon) and keys of other applications are not touched.

  • flushLocks() does the same on the lock connection. As in Laravel, it requires the lock connection to differ from the cache connection.
  • The redis session driver stores sessions under the cache prefix, on session.connection (default when unset, like the lock connection). There, flushLocks() also deletes every session, as Laravel's own store does by flushing the whole lock database. Put sessions on their own Redis connection or database (SESSION_CONNECTION) to keep them out of the scope.
  • With an empty cache prefix, the scope is the connection prefix.
  • With both prefixes empty, every key of the database is deleted, one batch at a time. The result is the same as FLUSHDB, but the command is never sent.

Keys are found with SCAN ... MATCH ... COUNT 1000 (a hint to the server, not a fixed batch size) and deleted with DEL, as Laravel does when it forgets a key. If a SCAN or DEL fails, the flush throws instead of reporting success. FLUSHDB, FLUSHALL and KEYS are never sent. Glob characters in the prefixes are escaped, so they cannot widen the scope.

Unlike FLUSHDB, the flush is not atomic: a key written while the scan is running may survive it.

cache:clear, optimize:clear, Cache::flush(), Cache::clear(), Cache::store('redis')->flush() and Cache::tags(...)->flush() keep working and emit the same events as before.

Supported setups

  • Laravel 12.53+ and 13.
  • phpredis and Predis.
  • A single node and Redis Cluster. On a cluster every master node is scanned, and keys are deleted without multi-key commands that span hash slots.
  • Predis replication (master and replicas): keys are scanned on the master, so a replica that lags behind cannot hide keys from the flush.
  • Redis and Valkey.

cache:clear --locks and flushLocks() on the cache repository exist from Laravel 13. On Laravel 12 the store still provides flushLocks(), but Laravel offers no command to call it.

Valkey and ACL

Each application can connect with its own ACL user, limited to its prefix and denied the commands that act on the whole database:

ACL SETUSER shop on >password ~shop-database-* &* +@all -flushdb -flushall -keys
// config/database.php
'redis' => [
    'cache' => [
        'host' => env('REDIS_HOST', '127.0.0.1'),
        'username' => env('REDIS_USERNAME'),
        'password' => env('REDIS_PASSWORD'),
        'port' => env('REDIS_PORT', '6379'),
        'database' => env('REDIS_CACHE_DB', '1'),
    ],
],

On a cluster the ACL user must exist on every node. On Laravel 12, phpredis clusters ignore username: pass the credentials as 'password' => [username, password] in the cluster options.

Testing

The tests run against Valkey, a single node on port 6379, a node on port 6390 that acts as a lagging replica, and a three-master cluster on ports 6380-6382, and need the redis PHP extension:

docker compose up -d --wait
composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

License

The MIT License (MIT). Please see License File for more information.