Redis node.js - 链接调用以获取用户数据 [英] Redis node.js - chaining calls to get user data

查看:200
本文介绍了Redis node.js - 链接调用以获取用户数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户数据库:用户可以拥有的配置文件,产品,配置和其他内容。我试图找出一种方法来成功连接我的redis调用(一个getAll函数),以便我可以返回一个所有这些东西的对象,如:

  user = {
个人资料:{},
产品:{},
config:{},
...
}

这是我如何访问/我正在尝试做什么: / p>

  User.getAll = function(uid,next){

var user = {};
var multi = client.multi();

var key ='user'+ uid;
client.hgetall(key,function(err,profile){
user.profile = profile;
//指示配置文件准备就绪
}

key ='products:'+ uid;
client.smembers(key,function(err,arr){
key = key +':';
for(i = 0; i < arr.length; i ++){
multi.hgetall(key + arr [i]);
}
multi.exec(function(err,products){
user $ product


if(ready){//显然这不是正确。
return next(null,user);
}
}

在收到所有我想要的数据?事件发射器是正确的方法吗?

解决方案

我认为事件发射器在这里不是一个好选择。您可以跟踪所有的电话,如下所示:

  User.getAll = function(uid,next){
var requests = [first,second];
var check_request = function(_key){
var idx = requests.indexOf(_key);
if(idx!== - 1){
requests.splice(idx,1);
}
if(!requests.length){
next(user);
}
};

// ORIGINAL CODE + check_request
var user = {};
var multi = client.multi();

var key ='user'+ uid;
client.hgetall(key,function(err,profile){
user.profile = profile;
check_request(first);
}

key ='products:'+ uid;
client.smembers(key,function(err,arr){
key = key +':';
for(i = 0; i < arr.length; i ++){
multi.hgetall(key + arr [i])
}
multi.exec(function(err,products){
user。 product = products;
check_request(second);
}));
})
}

显然,你会使用更有意义的东西,然后在 requests = [first,second]; 我希望你能得到这个想法。



另请参阅 async.js 。这是一个很好的图书馆,为您简化,它可以做得更多。


I have a database of users: profiles, products, configurations, and other things that a user could own. I'm trying to figure out a way to successfully chain my redis calls (a "getAll" function) so that I can return an object with all of these things, like:

user = {
    profile: {},
    products: {},
    config: {},
    ...
}

Here is how I'm accessing them/ what I'm trying to do:

User.getAll = function(uid, next){

    var user = {};
    var multi = client.multi();

    var key = 'user' + uid;
    client.hgetall(key, function(err, profile){
        user.profile = profile;
        //signal that profile is ready
    }

    key = 'products:' + uid;
    client.smembers(key, function(err, arr){
        key = key + ':';
        for (i=0; i < arr.length; i++){
            multi.hgetall(key + arr[i]);
        }
        multi.exec(function(err, products){
            user.products = products;
            //signal that products is ready
        }));
    })

    if (ready) { //obviously this isn't correct.
      return next(null, user);
    }
}

How can I resume after I've received all the data I want? Is event emitter the right way to go?

解决方案

I think that event emitter won't be a good choice here. You can keep track of all the calls, like that:

User.getAll = function(uid, next){
    var requests = ["first", "second"];
    var check_request = function(_key) {
        var idx = requests.indexOf(_key);
        if (idx!==-1) {
            requests.splice(idx, 1);
        }
        if (!requests.length) {
            next(user);
        }
    };

    // ORIGINAL CODE + check_request
    var user = {};
    var multi = client.multi();

    var key = 'user' + uid;
    client.hgetall(key, function(err, profile){
        user.profile = profile;
        check_request("first");
    }

    key = 'products:' + uid;
    client.smembers(key, function(err, arr){
        key = key + ':';
        for (i=0; i < arr.length; i++){
            multi.hgetall(key + arr[i])
        }
        multi.exec(function(err, products){
            user.products = products;
            check_request("second");
        }));
    })
}

Obviously you will use something that makes more sense then requests = ["first", "second"];, but I hope you get the idea.

Also have a look at async.js. It's a nice library which simplify that for you and it can do much more.

这篇关于Redis node.js - 链接调用以获取用户数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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