1

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);
2
  • Could you be more specific? What exactly are you trying to do, and where in your code does it break down? What works, what doesn't work? Commented May 23, 2016 at 19:18
  • I would want to scan the redis database and need to get the keys. for that I used redis and redis-scanner nodejs modules. Please find the above script.. I am connect the redis database but I unable to scan the keys.. Any help that would be appreciated. Thanks in advance!.. Commented May 23, 2016 at 19:23

1 Answer 1

7

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
Sign up to request clarification or add additional context in comments.

2 Comments

The && reply[1].length === 0 should be removed from the if statement, a scan terminates when the cursor returns 0, regardless if there were keys returned in the last scan or not. This will cause infinite loops...
Forgot to mention, you should still process data even if it is the last result in the scan.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.