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);