在node.js中执行多个redis查询的更简洁的方法

在node.js中执行多个redis查询的更简洁的方法,node.js,redis,Node.js,Redis,我编写了一些node.js代码来检索redis DB中的数据,但我不是很高兴,我想对其进行改进… 基本上,我在redis数据库中有一个记录“人”。从“people”中,我得到一个“person:I”(I是一个整数)列表,对于每个“person:I”,我进行额外的查询以检索键为“person:I”的散列 我就是这样做的: db.smembers("people", function(err1, people){ var jsonObj; var jsonArr = []; if(!er

我编写了一些node.js代码来检索redis DB中的数据,但我不是很高兴,我想对其进行改进…
基本上,我在redis数据库中有一个记录“人”。从“people”中,我得到一个“person:I”(I是一个整数)列表,对于每个“person:I”,我进行额外的查询以检索键为“person:I”的散列

我就是这样做的:

db.smembers("people", function(err1, people){
  var jsonObj;
  var jsonArr = [];
  if(!err1) {
    var i = 0;
    res.writeHead(200, {'content-type': 'application/json'});
    // people will provide a list like [person:1, person:2, ..., person:n]
    people.forEach(function(person){
      // In redis I get the hash with key "person:i" 
      db.hgetall(person, function(err2,obj){
        if(!err2){
          // Add person into array
          jsonArr.push({ "id" : person.substring(7), "lastname" : obj["lastname"], "firstname" : obj["firstname"]});

          // I'm not happy with this part where I check if I reached the last item of people array....
          i = i + 1;
          if(i == people.length){
            res.write(JSON.stringify(jsonArr));
            res.end();
          }
        } else {
          var jsonObj = { "error" : "database error", "message" : "Cannot get hash " + person}; 
          res.write(JSON.stringify(jsonObj));
          res.end();
        }
      });
    });
  } else {
    jsonObj = { "error" : "database error", "message" : err1.message };
    res.writeHead(200, {'content-type': 'application/json'});
    res.write(JSON.stringify(jsonObj));
    res.end();
  }
});

最干净(至少是更干净)的方法是什么?

您正在寻找的是一个异步控制流系统。一个例子是或

另一种方法是对流程进行抽象,为Person创建一个数据模型,用于获取单个Person对象,并创建一个Person模型,该模型是一个Person集合,其中包括一个方法,用于按照您使用的结构获取多个Person

编辑:我找到了与节点兼容的控制流/异步库的全面列表:

编辑:在查看代码后,我想到了另一种替代方法,该方法相当具体,但没有直接解决问题的控制流性质

通过改变您的模式,在
people
键中只存储个人ID,您可以使用redis的命令打开自己的窗口,这将使整个集合能够在单个命令中获取。要在redis中执行此操作,请执行以下操作:

> SADD people 1 2 3 4
> HMSET person:1 firstname John lastname Smith
> HMSET person:2 firstname Jane lastname Smith
> HMSET person:3 firstname John lastname Doe
> HMSET person:4 firstname Jane lastname Doe
> SORT people GET # GET person:*->firstname GET person:*->lastname
 1) "1"
 2) "Jane"
 3) "Doe"
 4) "2"
 5) "Jane"
 6) "Smith"
 7) "3"
 8) "John"
 9) "Doe"
10) "4"
11) "Jane"
12) "Doe"

这在
people
键中增加了节省内存的好处,并通过
SORT
命令的
by
limit
选项启用分页/排序。

您正在寻找的是一个异步控制流系统。一个例子是或

另一种方法是对流程进行抽象,为Person创建一个数据模型,用于获取单个Person对象,并创建一个Person模型,该模型是一个Person集合,其中包括一个方法,用于按照您使用的结构获取多个Person

编辑:我找到了与节点兼容的控制流/异步库的全面列表:

编辑:在查看代码后,我想到了另一种替代方法,该方法相当具体,但没有直接解决问题的控制流性质

通过改变您的模式,在
people
键中只存储个人ID,您可以使用redis的命令打开自己的窗口,这将使整个集合能够在单个命令中获取。要在redis中执行此操作,请执行以下操作:

> SADD people 1 2 3 4
> HMSET person:1 firstname John lastname Smith
> HMSET person:2 firstname Jane lastname Smith
> HMSET person:3 firstname John lastname Doe
> HMSET person:4 firstname Jane lastname Doe
> SORT people GET # GET person:*->firstname GET person:*->lastname
 1) "1"
 2) "Jane"
 3) "Doe"
 4) "2"
 5) "Jane"
 6) "Smith"
 7) "3"
 8) "John"
 9) "Doe"
10) "4"
11) "Jane"
12) "Doe"

这在
people
键中还可以节省内存,并通过
SORT
命令的
by
limit
选项启用分页/排序。

感谢您的建议。问题是我现在不能轻易地修改模式。我在node.js中寻找流量控制的最佳实践,因为我有几个地方需要更干净的代码。非常感谢您的帮助(我将明确了解您描述的解决方法)。感谢您的回答!它让我找到了最适合我的方法:)
对邮件进行排序desc limit 0 10 get message:->text
以获取最后10封邮件。感谢您的建议。问题是我现在不能轻易地修改模式。我在node.js中寻找流量控制的最佳实践,因为我有几个地方需要更干净的代码。非常感谢您的帮助(我将明确了解您描述的解决方法)。感谢您的回答!它让我找到了最适合我的方法:)
对消息进行排序desc limit 0 10 get message:->text
以获取最后10条消息。