使用redis

时间:2016-05-23 18:52:42

标签: node.js redis

我正在尝试使用redis,redis-scanner模块扫描redis服务器上的字符串,但它无法正常工作..

请找到我的代码,如下所示,由节点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);

1 个答案:

答案 0 :(得分:5)

您可以使用版本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