【问题标题】:Node.js/ Make redis call asyncNode.js/ 使 redis 调用异步
【发布时间】:2015-12-01 09:53:34
【问题描述】:

我在node.js中使用redis客户端

    var db = require("redis");
    var dbclient = db.createClient();

我用下一种方式加载数据库:

 dbclient.zrange("cache", -1000000000000000, +1000000000000000, function(err, replies){
                logger.info("Go to cache");
                for (var i=0; i < replies.length; i++){
                     (function(i){
                     // Do some commands with the result
                     })(i)
               }
    })

我注意到我的应用程序从哪里开始,需要 30~ 秒。用于执行数据库查询。此时,不会处理来自Express 模块的其他请求。

我该如何解决这个问题?为什么没有异步?

【问题讨论】:

  • redis的异步API。但是loop for - 没有。
  • 什么是"do some commands with the result"replies.length 是什么?

标签: node.js asynchronous redis


【解决方案1】:

正如 stdob 所说,您的代码中唯一不是异步的部分是 for 循环部分。就个人而言,我会创建一个child process 并运行数据库查询以及您决定对输出执行的任何操作。如果这对您的应用程序不起作用,您可能只需要延迟启动程序这 30 秒或以不同的方式处理缓存。

【讨论】:

    【解决方案2】:

    如果您担心正常运行时间,那么您应该使用ZSCAN

    使用 COUNT 选项在每次通话中获取更多数据,但请记住:

    虽然 SCAN 不保证每次迭代返回的元素数量,但可以使用 COUNT 选项凭经验调整 SCAN 的行为

    并且在每个结果中使用setImmediate 函数迭代到下一个 SCAN。

    var cursor = '0';
    
    function doscan(){
        dbclient.zscan("cache", cursor, "COUNT", "100", function(err, replies){
                    logger.info("Go to cache");
    
                    // Update the cursor position for the next scan
                    cursor = res[0];
    
                    if (cursor === '0') {
                        return console.log('Iteration complete');
                    } else {
                        for (var i=0; i < replies.length; i++){
                             (function(i){
                             // Do some commands with the result
                             })(i)
                        }
                        setImmediate(function() { doscan() });
                    }
        })
    }
    

    【讨论】:

      【解决方案3】:

      【讨论】:

        猜你喜欢
        • 2013-03-25
        • 1970-01-01
        • 2014-01-30
        • 2020-04-09
        • 2012-12-13
        • 2014-04-29
        • 1970-01-01
        • 2017-03-20
        • 2015-09-23
        相关资源
        最近更新 更多