1

I have redis client and mongodb, I want to speed up my database, for doing this I am fetching the users details from redis cache and want to save it into mongodb using node.js.

Your answers will be very helpful for me.

client.smembers('login',function(err,obj){
    if(obj.length === 0)
                    callback(obj);
    obj.forEachDone(function(reply,done){
        console.log(reply);
        client.hgetall(reply,function(err,value){
            value.login_value = decodeURI(value.login_value);
            done(value);
    });     
    },this,function(objArr){
            objArr.sort(utils.compare);
            console.log(objArr);
});
    });
thor
  • 21,418
  • 31
  • 87
  • 173
Irrfan23
  • 362
  • 5
  • 17
  • Can your give us your code? – Vincent Apr 21 '16 at 11:17
  • How will you speed up database when it's being used as data store for cache – Saleem Apr 21 '16 at 11:29
  • since i am new to the node.js i want to save the data store in redis into mongodb @saleem – Irrfan23 Apr 21 '16 at 17:33
  • Array.prototype.forEachDone = function(fn, scope, lastfn) { var ObjArr = new Array(); for(var i = 0, c = 0, len = this.length; i < len; i++) { fn.call(scope, this[i], function(obj) { ObjArr.push(obj); ++c === len && lastfn(ObjArr); }); } } remaing code is here @Vincent – Irrfan23 Apr 21 '16 at 17:36

1 Answers1

0

Can you be more clear please. But if i understood you well what you want to do is save data that is on your Redis cache to MongoDB. Well to begin with, i am also a bit new to MongoDB and Redis caching as well but on my app, i realized that i can be making all the saving data to MongoDB, that is get the User Id for example from Redis user cache and then do a simple MongoDB update by Id which can be found here https://mongodb.github.io/node-mongodb-native/markdown-docs/insert.html. More easily,

collection.update(criteria, update[[, options], callback]);

Another thing you should do is to set a timeout when inserting a user to cache. For example you can set a quicker timeout if your app involves a lot of updating of docs so as to always make sure that your Redis cache is updated quite often. If your app is not that involving though you can update the Redis cache lets say every two hours and it would be fine. The Redis set expire can be found at this GitHub answer https://github.com/NodeRedis/node_redis/issues/1000#issuecomment-193155582 . More easily,

client.set(key, value, 'EX', 60 * 60 * 24, callback);

I know that this answer is late and you probably got a fix for it, but i have answered it for future reference for people who might experience the same problem as you. Thank You.

Ike Mawira
  • 751
  • 11
  • 21