Search code examples
node.jsredisnode-redis

Is this the correct way to reuse the redis client?


I have created a redis client module named redisConnection.js. It's contents are as follows

var redis = require('redis').createClient();

exports.exposeConnection = function(){

 return redis;

}; 

Now whenever I want to get make use of redis I just require the module and call the exposeConnection method. I wanted to know if this is right way to reuse the connection. I am hoping that redis connection is being instantiated only once and not every time I call the module. If not is there a better way reuse it?


Solution

  • That's almost similar to what I would I do(and what I have done in my previous apps).

    Although instead of exposing it through a function I would just do this:

    module.exports = require('redis').createClient();
    

    So then later I can just do redis = require('./local-redis') instead of redis = require('./local-redis').exposeConnection().

    This is a very simple but reliable architecture.