How to get all redis keys in cache in Laravel?

834 views Asked by At

It appears that the Cache facade in Laravel doesn't allow you to get all the keys that are currently being cached in Redis.

I want to create an endpoint so I can retrieve this info and know if my entries are working properly.

I tried using the Redis facade with no success using the following commands and their respective errors

Redis::keys("*");

"Cannot use 'KEYS' with redis-cluster."


Redis::scan("cursor");

"Cannot use 'SCAN' with redis-cluster."
1

There are 1 answers

0
Ali Raza On

In Redis, cluster, scan is recommended over key if you have lots of keys. However, you should use it correctly. Try using this way.

use Illuminate\Support\Facades\Redis;

$cursor = '0'; // Start with initial cursor

do {
    // Scan for keys with current cursor
    list($cursor, $keys) = Redis::scan($cursor);

    foreach ($keys as $key) {
      echo "Key: $key\n";
   }
} while ($cursor !== '0'); // Continue scanning until cursor is '0'

Reference: Laravel and redis scan