search

Home  >  Q&A  >  body text

How to get all Redis keys in cache in Laravel

The caching facade in Laravel doesn't seem to allow you to get all keys currently cached in Redis.

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

I tried using the Redis facade without 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."

P粉986860950P粉986860950286 days ago430

reply all(1)I'll reply

  • P粉627027031

    P粉6270270312024-02-18 12:01:48

    In Redis and cluster, if you have many keys, it is recommended to scan instead of keys. However, you should use it correctly. Try this approach.

    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'

    refer to: Laravel and redis scanning

    reply
    0
  • Cancelreply