From 8a6345ded7df6ad116dd3b33be24a125c4af7c29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Mon, 22 Jan 2018 16:41:22 -0500 Subject: [PATCH] possible fix for pubsub --- src/database/mongo.js | 1 - src/database/mongo/hash.js | 21 +++++++++++++++++++++ src/database/mongo/pubsub.js | 34 ++++------------------------------ src/database/redis.js | 1 - src/database/redis/pubsub.js | 11 ++++++----- src/pubsub.js | 6 +++--- 6 files changed, 34 insertions(+), 40 deletions(-) diff --git a/src/database/mongo.js b/src/database/mongo.js index 4042cf6025..be6789389b 100644 --- a/src/database/mongo.js +++ b/src/database/mongo.js @@ -122,7 +122,6 @@ mongoModule.init = function (callback) { require('./mongo/sets')(db, mongoModule); require('./mongo/sorted')(db, mongoModule); require('./mongo/list')(db, mongoModule); - require('./mongo/pubsub')(db, mongoModule); callback(); }); }; diff --git a/src/database/mongo/hash.js b/src/database/mongo/hash.js index 84d66660c0..d11cb0aacd 100644 --- a/src/database/mongo/hash.js +++ b/src/database/mongo/hash.js @@ -1,5 +1,7 @@ 'use strict'; +var pubsub = require('../../pubsub'); + module.exports = function (db, module) { var helpers = module.helpers.mongo; @@ -16,6 +18,25 @@ module.exports = function (db, module) { cache.hits = 0; module.objectCache = cache; + pubsub.on('mongo:hash:cache:del', function (key) { + cache.del(key); + }); + + pubsub.on('mongo:hash:cache:reset', function () { + cache.reset(); + }); + + module.delObjectCache = function (key) { + pubsub.publish('mongo:hash:cache:del', key); + cache.del(key); + }; + + module.resetObjectCache = function () { + pubsub.publish('mongo:hash:cache:reset'); + cache.reset(); + }; + + module.setObject = function (key, data, callback) { callback = callback || helpers.noop; if (!key || !data) { diff --git a/src/database/mongo/pubsub.js b/src/database/mongo/pubsub.js index 157d4652bd..83ff676294 100644 --- a/src/database/mongo/pubsub.js +++ b/src/database/mongo/pubsub.js @@ -1,34 +1,8 @@ 'use strict'; -var nconf = require('nconf'); +var mubsub = require('mubsub'); -module.exports = function (db, mongoModule) { - var pubsub; +var db = require('../mongo'); +var client = mubsub(db.client); - if (!nconf.get('redis')) { - var mubsub = require('mubsub'); - var client = mubsub(db); - pubsub = client.channel('pubsub'); - mongoModule.pubsub = pubsub; - } else { - pubsub = require('../../pubsub'); - } - - pubsub.on('mongo:hash:cache:del', function (key) { - mongoModule.objectCache.del(key); - }); - - pubsub.on('mongo:hash:cache:reset', function () { - mongoModule.objectCache.reset(); - }); - - mongoModule.delObjectCache = function (key) { - pubsub.publish('mongo:hash:cache:del', key); - mongoModule.objectCache.del(key); - }; - - mongoModule.resetObjectCache = function () { - pubsub.publish('mongo:hash:cache:reset'); - mongoModule.objectCache.reset(); - }; -}; +module.exports = client.channel('pubsub'); diff --git a/src/database/redis.js b/src/database/redis.js index 55037393cb..e88663cf70 100644 --- a/src/database/redis.js +++ b/src/database/redis.js @@ -50,7 +50,6 @@ redisModule.init = function (callback) { require('./redis/sets')(redisClient, redisModule); require('./redis/sorted')(redisClient, redisModule); require('./redis/list')(redisClient, redisModule); - require('./redis/pubsub')(redisClient, redisModule); callback(); }); diff --git a/src/database/redis/pubsub.js b/src/database/redis/pubsub.js index 3daf0f40cc..f7724bb016 100644 --- a/src/database/redis/pubsub.js +++ b/src/database/redis/pubsub.js @@ -7,10 +7,10 @@ var EventEmitter = require('events').EventEmitter; var channelName; -var PubSub = function (redisModule) { +var PubSub = function (db) { var self = this; - var subClient = redisModule.connect(); - this.pubClient = redisModule.connect(); + var subClient = db.connect(); + this.pubClient = db.connect(); channelName = 'db:' + nconf.get('redis:database') + 'pubsub_channel'; subClient.subscribe(channelName); @@ -35,6 +35,7 @@ PubSub.prototype.publish = function (event, data) { this.pubClient.publish(channelName, JSON.stringify({ event: event, data: data })); }; -module.exports = function (redisClient, redisModule) { - redisModule.pubsub = new PubSub(redisModule); +module.exports = function () { + var db = require('../redis'); + return new PubSub(db); }; diff --git a/src/pubsub.js b/src/pubsub.js index 2364612b88..270ae37bf8 100644 --- a/src/pubsub.js +++ b/src/pubsub.js @@ -32,9 +32,9 @@ function get() { pubsub = new EventEmitter(); pubsub.publish = pubsub.emit.bind(pubsub); } else if (nconf.get('redis')) { - pubsub = require('./database/redis').pubsub; - } else { - pubsub = require('./database').pubsub; + pubsub = require('./database/redis/pubsub'); + } else if (nconf.get('mongo')) { + pubsub = require('./database/mongo/pubsub'); } if (!pubsub) {