Ivo Tijan
Ivo Tijan
Jul 23, 2017 · 1 min read

Thank you very much for this post! I put console log to my connection event and noticed that every time I ran the app, there was as much connections established as there were modules using redis. So I went googling and stumbled upon your post. It got me thinking and I did it a little differently:

caching.js:

let cacheModule = function cacheModule() {
const client = redis.createClient();
this.set = function set(key, value) {
if(!_.isUndefined(key) && !_.isUndefined(value)) {
client.set(key, JSON.stringify(value));
}
};
...
}
// immediately create a redis instance
// that way we make sure that all modules are running the same instance
module.exports = new cacheModule();

Then where I want to use it:

const Cache = require("path/to/caching.js");const cache = Cache;cache.set(key, data);

    Ivo Tijan

    Written by

    Ivo Tijan

    Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
    Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
    Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade