Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 282
Akshat Jiwan Sharma Avatar asked Oct 11 '13 06:10

Akshat Jiwan Sharma


1 Answers

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.

like image 92
Farid Nouri Neshat Avatar answered Oct 08 '22 00:10

Farid Nouri Neshat