Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I use redis client in node.js

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

I load the DB in the next way:

 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)
               }
    })

I notice that where my application is started, it takes 30~ sec. for the DB query to execute. In this time, no other request from Express module are served.

How can I slove this issue? Why is no a asynchronous?

share|improve this question
1  
API of redis asynchronously. But the loop for - no. –  stdob-- Sep 5 at 11:06
    
What is "do some commands with the result"? What is replies.length? –  robertklep Sep 5 at 11:38

3 Answers 3

If you're worried about the uptime then you should use ZSCAN

Use the COUNT option to get more data on every call but remember:

While SCAN does not provide guarantees about the number of elements returned at every iteration, it is possible to empirically adjust the behavior of SCAN using the COUNT option

And at every result use the setImmediate function to iterate over to the next 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() });
                }
    })
}
share|improve this answer

As stdob said, the only part of your code that is not asynchronous is the for loop part. Personally, I would create a child process and run the DB query as well as whatever you decide to do with the output. If that doesn't work for your application, you may just have to delay starting your program for those 30 seconds or handle the cache differently.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.