Reputation: 3530
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?
Upvotes: 2
Views: 578
Reputation: 6860
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() });
}
})
}
Upvotes: 3
Reputation: 686
You can use Iced-CoffeeScript for that , like https://github.com/Terebinth/britvic/blob/master/server/models/hive_iced.iced#L101
Upvotes: 1
Reputation: 26
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.
Upvotes: 1