我正在尝试使用redis,redis scanner模块扫描redis服务器上的字符串,但它不起作用。
我的代码如下,由node js编写。任何帮助都将不胜感激
var conf = require('./config.js'); //config file declarations
var restify = require('restify'); //restify included
var redis = require("redis"); //redis included
var redis_scanner = require('redis-scanner');
var client = redis.createClient(conf.get('redis_cm.redis_port'), conf.get('redis_cm.redis_server'));
client.auth(conf.get('redis_cm.auth'), function (err) {
if (err){
throw err;
}
});
client.on('connect', function() {
console.log('Connected to Redis');
});
client.select(conf.get('redis_cm.database'), function() {
console.log("Redis Database "+conf.get('redis_cm.database')+" selected successfully!..");
});
var options = {
args: ['MATCH','CM:*','COUNT','5'],
onData: function(result, done){
console.log(result);
console.log("result");
client.quit();
process.exit(1);
},
onEnd: function(err){
console.log("error");
}
};
var scanner = new redis_scanner.Scanner(client, 'SCAN', null, options);
最佳答案
您可以使用2.8.0版的redis中的scan
命令。查看http://redis.io/commands/scan中的文档。
示例代码:
var cursor = '0';
function scan(){
redisClient.scan(cursor, 'MATCH', 'CM:*', 'COUNT', '5', function(err, reply){
if(err){
throw err;
}
cursor = reply[0];
if(cursor === '0'){
return console.log('Scan Complete');
}else{
// do your processing
// reply[1] is an array of matched keys.
// console.log(reply[1]);
return scan();
}
});
}
scan(); //call scan function
关于node.js - 使用redis中的nodejs扫描字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37398437/