Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis command to get all available keys on Redis Cluster?

I am using this

redisManager.redisClient.keys('*example*', function (err, keys) {
})

But it only gives keys from only one of the redis cluster. How can I get keys from all cluster?

like image 428
Albin Mathew Avatar asked Mar 17 '16 06:03

Albin Mathew


People also ask

How can I see all Redis keys?

To list the keys in the Redis data store, use the KEYS command followed by a specific pattern. Redis will search the keys for all the keys matching the specified pattern. In our example, we can use an asterisk (*) to match all the keys in the data store to get all the keys.

How can I get total number of keys in Redis?

The first command you can use to get the total number of keys in a Redis database is the DBSIZE command. This simple command should return the total number of keys in a selected database as an integer value. The above example command shows that there are 203 keys in the database at index 10.

Which command is used to obtain all the keys in a database?

The Redis KEYS command returns all the keys in the database that match a pattern (or all the keys in the key space). Similar commands for fetching all the fields stored in a hash is HGETALL and for all fetching the members of a SMEMBERS. The keys in Redis themselves are stored in a dictionary (aka a hash table).

How do I view the contents of Redis cache?

By clicking on a Redis key name, all its contents will open in a new editor tab. With a collection type Redis key, clicking on it will reveal the individual elements under the key name. Clicking the individual element will display its contents in a new editor tab.


1 Answers

You can't get keys for all nodes using a single command. You have to get keys for all nodes and merge them. Reference - https://github.com/antirez/redis/issues/1962

You can do something like.

var redis = require('redis');

redisConfig = new Array(
    {"port": 1234, "host": "192.168.1.2"},
    {"port": 5678, "host": "192.168.1.3"}
);

keys    = new Array();
allKeys = new Array();

for(i = 0; i < redisConfig.length; i++){
    redisClient = redis.createClient(redisConfig[i].port, redisConfig[i].host);    
      keys[i] = redisClient.keys('*example*', function (err, keys) {
      allkeys = [...new Set([...allKeys ,...keys[i]])];
    })
}
like image 187
Shivam Mathur Avatar answered Oct 01 '22 19:10

Shivam Mathur