Vimal Jesurathinam
Vimal Jesurathinam

Reputation: 11

Scan strings using nodejs in redis

I am trying to scan the string on redis server by using redis, redis-scanner module but it is not working..

Please find my code as below and written by node js. Any help would appreciated

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);

Upvotes: 1

Views: 8993

Answers (1)

raghav
raghav

Reputation: 451

You can use the scan command available in redis from version 2.8.0. Check the documentation from http://redis.io/commands/scan.

Sample code:

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

Upvotes: 7

Related Questions