Redis command to get all available keys?

后端 未结 15 2588
攒了一身酷
攒了一身酷 2020-12-04 04:30

Is there a Redis command for fetching all keys in the database? I have seen some python-redis libraries fetching them. But was wondering if it is possible from redis-client.

相关标签:
15条回答
  • 2020-12-04 05:12

    If your redis is a cluster,you can use this script

    #!/usr/bin/env bash
    redis_list=("172.23.3.19:7001,172.23.3.19:7002,172.23.3.19:7003,172.23.3.19:7004,172.23.3.19:7005,172.23.3.19:7006")
    
    arr=($(echo "$redis_list" | tr ',' '\n'))
    
    for info in ${arr[@]}; do
      echo "start :${info}"
      redis_info=($(echo "$info" | tr ':' '\n'))
      ip=${redis_info[0]}
      port=${redis_info[1]}
      echo "ip="${ip}",port="${port}
      redis-cli -c -h $ip -p $port set laker$port '湖人总冠军'
      redis-cli -c -h $ip -p $port keys \*
    
    done
    
    echo "end"
    
    0 讨论(0)
  • 2020-12-04 05:13

    Try to look at KEYS command. KEYS * will list all keys stored in redis.

    EDIT: please note the warning at the top of KEYS documentation page:

    Time complexity: O(N) with N being the number of keys in the database, under the assumption that the key names in the database and the given pattern have limited length.

    UPDATE (V2.8 or greater): SCAN is a superior alternative to KEYS, in the sense that it does not block the server nor does it consume significant resources. Prefer using it.

    0 讨论(0)
  • 2020-12-04 05:13
    redis-cli -h <host> -p <port> keys * 
    

    where * is the pattern to list all keys

    0 讨论(0)
  • 2020-12-04 05:14

    Take a look at following Redis Cheat Sheet. To get a subset of redis keys with the redis-cli i use the command

    KEYS "prefix:*"
    
    0 讨论(0)
  • 2020-12-04 05:14

    If you are using Laravel Framework then you can simply use this:

    $allKeyList = Redis::KEYS("*");
    
    print_r($allKeyList);
    

    In Core PHP:

    $redis = new Redis();
    
    $redis->connect('hostname', 6379);
    
    $allKeyList = $redis->keys('*');
    
    print_r($allKeyList);
    
    0 讨论(0)
  • 2020-12-04 05:15

    You can simply connect to your redis server using redis-cli, select your database and type KEYS *, please remember it will give you all the keys present in selected redis database.

    0 讨论(0)
提交回复
热议问题