node.js - 在 Node.js 中,我如何使用集合作为索引以编程方式从 Redis 数据库中检索许多哈希

标签 node.js asynchronous recursion schema redis

我的 redis 数据库中每个用户都有一大堆字段,我希望能够检索他们的所有记录并显示它们。 我这样做的方法是存储一组所有 userids,当我想要他们的所有记录时,我递归地迭代该集合,使用集合中的 userids 获取他们的记录并将它们添加到全局数组,然后最后返回这个全局数组。无论如何,我不是特别喜欢这种方法,并且想听听一些替代方案的建议,我觉得 node.js 或 redis 中必须有更好的功能来解决这个问题。也许有一种方法可以完全避免使用该集合,但环顾四周我看不到任何明显的东西。

这是我的伪(非常完整)node.js 代码的示例,请注意设置大小不是问题,因为它很少会 > 15。

注册函数:

var register = function(username, passwordhash, email){

    // Get new ID by incrementing idcounter
    redis.incr('db:users:idcounter', function(err, userid){

        // Setup user hash with user information, using new userid as key
        redis.hmset('db:user:'+userid, {
                'username':username,
                'passwordhash':passwordhash,
                'email':email
            },function(err, reply){

                 // Add userid to complete list of all users
                 redis.sadd('db:users:all', userid);

            }
        });
    });
}

记录检索功能: var getRecords = function(fcallback){

    // Grab a list of all the id's
    redis.smembers('db:users:all', function(err, allusersids){

        // Empty the returned (global) array
        completeArray = [];

        // Start the recursive function, on the allusersids Array.
        recursive_getNextUserHash(allusersids, fcallback);
    });  
}

用于检索单个记录的递归函数:

// Global complete Array (so recursive function has access)
var completeArray = [];

// recursive method for filling up our completeArray
var recursive_getNextUserHash = function(userArray, callback){

    // If userArray==0 this means we have cycled entire list, 
    // call the callback, and pass it the completeArray which 
    // is now full of our usernames + emails

    if(userArray.length==0){
        callback.apply(this, [completeArray]);
        return;
    }

    // If still more items, start by popping the next user
    var userid = userArray.pop();

    // grab this users information
    redis.hvals('db:user:'+userid, function(err, fields){

        // Add users information to global array
        completeArray.push({username:fields[0],email:fields[2]});

        // Now move on to the next user
        recursive_getNextUserHash(userArray, callback);
    });

}

使用会是这样的:

register('bob', 'ASDADSFASDSA', 'bob@example.com');
register('bill', 'DDDASDADSAD', 'bill@example.com');
getRecords(function(records){
    for(var i=0;i<records.length;i++){
         console.log("u:"+records[i]['username']+',@:'+records[i]['email']);
    }
});

小结:用node.js和redis检索Hash的很多字段有什么好方法?写完这个问题,我开始怀疑你在redis中是不是就是这样做的,你做了很多次往返,不管是不是这种情况,一定有办法避免可怕的递归!

最佳答案

假设您使用的是 https://github.com/mranney/node_redis - 看看 Multi 和 Exec。您可以在一个请求中发送所有命令,并立即等待所有响应。不需要递归。

关于node.js - 在 Node.js 中,我如何使用集合作为索引以编程方式从 Redis 数据库中检索许多哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9644397/

相关文章:

javascript - Sequelize 和嵌套结果?

node.js - 如何在 Vagrant VM 上安装 bcrypt?

javascript - 当用户断开连接时如何停止服务器上的计时器? (Node.js/socket.io)

asynchronous - 无论 'delay' 参数如何,Vue 异步组件都会立即加载

Angular 8 - 在前端更改检测问题上调整图像大小

c - 使用递归找到二维迷宫路径。段故障。 C

java - XSLT 处理递归深度

javascript - 错误 : listen EADDRINUSE when use (node --harmony debug. js) 次数过多

node.js - Node 牛队列错误: Missing process handler for job type JOB_NAME

javascript - 从扩展脚本中的路径递归填充 TreeView