在 redis 中使用 nodejs 扫描字符串 [英] Scan strings using nodejs in redis

查看:50
本文介绍了在 redis 中使用 nodejs 扫描字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 redis、redis-scanner 模块扫描 redis 服务器上的字符串,但它不起作用..

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

请在下面找到我的代码,由 node js 编写.任何帮助将不胜感激

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

推荐答案

您可以使用 redis 2.8.0 版本中提供的 scan 命令.查看 http://redis.io/commands/scan 中的文档.

You can use the scan command available in redis from version 2.8.0. Check the documentation from 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

这篇关于在 redis 中使用 nodejs 扫描字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆