如何在javascript中的redis中获取所有键和值? [英] how to get all keys and values in redis in javascript?

查看:112
本文介绍了如何在javascript中的redis中获取所有键和值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JavaScript创建节点API.我已经将redis用作我的键值存储.我在应用程序中创建了一个redis-client,并且能够获取垂直密钥的值.

I am creating a node API using javascript. I have used redis as my key value store. I created a redis-client in my app and am able to get values for perticular key.

我想检索所有键及其值.到目前为止,我已经做到了:

I want to retrieve all keys along with their values. So Far I have done this :

app.get('/jobs', function (req, res) {
    var jobs = [];
    client.keys('*', function (err, keys) {
        if (err) return console.log(err);
        if(keys){
            for(var i=0;i<keys.length;i++){
                client.get(keys[i], function (error, value) {
                    if (err) return console.log(err);
                    var job = {};
                    job['jobId']=keys[i];
                    job['data']=value;
                    jobs.push(job);
                });  
            }
            console.log(jobs);
            res.json({data:jobs});
        }
    });
});

但是我总是得到空白数组作为响应.

but I always get blank array in response.

有没有办法在javascript中做到这一点?

is there any way to do this in javascript?

谢谢

推荐答案

首先,您遇到的问题是在 for 循环中, client.get 是通过 asynchronous 回调调用的,其中 synchronous for 循环不会等待异步回调,因此下一行 res是.json({data:jobs}); 在异步回调之前,在 for 循环之后立即被调用.在行 res.json({data:jobs}); 时,数组 jobs 仍然为空 [] 并返回响应.

First of all, the issue in your question is that, inside the for loop, client.get is invoked with an asynchronous callback where the synchronous for loop will not wait for the asynchronous callback and hence the next line res.json({data:jobs}); is getting called immediately after the for loop before the asynchronous callbacks. At the time of the line res.json({data:jobs}); is getting invoked, the array jobs is still empty [] and getting returned with the response.

为减轻这种情况,您应该使用任何答应模块,例如 async bluebird ES6 Promise 等.

To mitigate this, you should use any promise modules like async, bluebird, ES6 Promise etc.

使用 async 模块修改的代码,

app.get('/jobs', function (req, res) {
    var jobs = [];
    client.keys('*', function (err, keys) {
        if (err) return console.log(err);
        if(keys){
            async.map(keys, function(key, cb) {
               client.get(key, function (error, value) {
                    if (error) return cb(error);
                    var job = {};
                    job['jobId']=key;
                    job['data']=value;
                    cb(null, job);
                }); 
            }, function (error, results) {
               if (error) return console.log(error);
               console.log(results);
               res.json({data:results});
            });
        }
    });
});

但是从 Redis 文档中可以看到,键用于调试和特殊操作,例如更改键空间布局,不建议用于生产环境.

But from the Redis documentation, it is observed that usage of Keys are intended for debugging and special operations, such as changing your keyspace layout and not advisable to production environments.

因此,我建议使用另一个名为 redisscan 的模块,如下所示,该模块使用 SCAN而不是 Redis 文档中建议的 KEYS .

Hence, I would suggest using another module called redisscan as below which uses SCAN instead of KEYS as suggested in the Redis documentation.

类似

var redisScan = require('redisscan');
var redis     = require('redis').createClient();


redisScan({
        redis: redis,
        each_callback: function (type, key, subkey, value, cb) {
            console.log(type, key, subkey, value);
            cb();
        },
        done_callback: function (err) {
            console.log("-=-=-=-=-=--=-=-=-");
            redis.quit();
        }
    });

这篇关于如何在javascript中的redis中获取所有键和值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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