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?
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?
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]])];
})
}
Have you tried using the node-redisscan
package? It allows you to use SCAN
commands on Redis clusters.
You'd have to collect each matching key with the callback. Here's how you'd do it (directly lifted from the README file) -
var redisScan = require('redisscan');
var redis = require('redis').createClient();
redisScan({
redis: redis,
each_callback: function (type, key, subKey, value, done) {
console.log(type, key, subKey, value);
done();
},
done_callback: function (err) {
if (err) throw err;
redis.quit();
}
});
You can use this code inorder to find keys from all cluster. Please refer to link for more info.
var RedisCluster = require('node-redis-cluster').RedisCluster;
var rcluster = RedisCluster.create([
{ port: 6379, host: '10.0.0.1' },
{ port: 6379, host: '10.0.0.2' },
{ port: 6379, host: '10.0.0.3' },
]);
rcluster.execAll('keys', ['*'], function(err, results) {
/* results will be
{
'10.0.0.1:6379': [ // keys],
'10.0.0.2:6379': [ // keys],
'10.0.0.3:6379': [ // keys]
}
*/
});
Keys command not recommended to be used in productions mode.
From redis docs Warning: consider KEYS as a command that should only be used in production environments with extreme care. It may ruin performance when it is executed against large databases. This command is intended for debugging and special operations, such as changing your keyspace layout. Don't use KEYS in your regular application code. If you're looking for a way to find keys in a subset of your keyspace, consider using SCAN or sets.
Use scanStream Instead
redisConnection = redisConnectionObject;
const slaves = redisConnection.nodes("slave");
config.info('number of slaves', slaves.length)
let keys = [];
let slavePromises = [];
slaves.forEach((slave) => {
let slaveCreate = new Promise((resolve) => {
const stream = slave.scanStream({
match: prefix
});
stream.on("data", (resultKeys) => {
keys = [...keys, ...resultKeys];
});
stream.on("end", () => {
resolve();
});
});
slavePromises.push(slaveCreate);
});
Promise.all(slavePromises).then(([slaveOneData, slaveTwoData]) => {
keys = _.uniq(keys);
resolveOne(keys);
});
I converted this from my C# code. You need to query across all servers and build your list of keys.
const Redis = require('ioredis');
async function getKeyList(servers, pattern) {
const keyList = [];
for (const server of servers) {
if (!server.isConnected) continue;
const keys = await server.keys(pattern);
if (keys.length === 0) continue;
keyList.push(...keys);
}
return keyList;
}
// Example usage:
const servers = [
new Redis({ host: 'server1', port: 6379 }),
new Redis({ host: 'server2', port: 6379 }),
// Add more server configurations as needed
];
const pattern = 'your_pattern_here';
getKeyList(servers, pattern)
.then((keyList) => {
console.log('Keys:', keyList);
})
.catch((error) => {
console.error('Error:', error);
});
Run command $ npm install redis
to install redis.
var redis = require('redis'),
client = redis.createClient();
client.keys('*', function (err, keys) {
if (err) return console.log(err);
for(var i = 0, len = keys.length; i < len; i++) {
console.log(keys[i]);
}
});
© 2022 - 2024 — McMap. All rights reserved.