Wednesday, August 31, 2016

Redis command to get all available keys on Redis Cluster?

Leave a Comment

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?

1 Answers

Answers 1

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]])];     }) } 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment