diff --git a/lib/caching.js b/lib/caching.js index 9e2f70b..d4c7134 100644 --- a/lib/caching.js +++ b/lib/caching.js @@ -44,12 +44,9 @@ var caching = function(args) { options = undefined; } - if (callbackFiller.queues[key]) { - callbackFiller.queues[key].push({cb: cb, domain: process.domain}); - return; - } - - callbackFiller.queues[key] = [{cb: cb, domain: process.domain}]; + var hasKey = callbackFiller.has(key); + callbackFiller.add(key, {cb: cb, domain: process.domain}); + if (hasKey) { return; } self.store.get(key, options, function(err, result) { if (err && (!self.ignoreCacheErrors)) { diff --git a/lib/callback_filler.js b/lib/callback_filler.js index 163aa7e..4914bf1 100644 --- a/lib/callback_filler.js +++ b/lib/callback_filler.js @@ -16,4 +16,16 @@ CallbackFiller.prototype.fill = function(key, err, data) { }); }; +CallbackFiller.prototype.has = function(key) { + return this.queues[key]; +}; + +CallbackFiller.prototype.add = function(key, funcObj) { + if (this.queues[key]) { + this.queues[key].push(funcObj); + } else { + this.queues[key] = [funcObj]; + } +}; + module.exports = CallbackFiller; diff --git a/lib/multi_caching.js b/lib/multi_caching.js index 22a61a4..b384a50 100644 --- a/lib/multi_caching.js +++ b/lib/multi_caching.js @@ -96,12 +96,9 @@ var multiCaching = function(caches) { options = undefined; } - if (callbackFiller.queues[key]) { - callbackFiller.queues[key].push({cb: cb, domain: process.domain}); - return; - } - - callbackFiller.queues[key] = [{cb: cb, domain: process.domain}]; + var hasKey = callbackFiller.has(key); + callbackFiller.add(key, {cb: cb, domain: process.domain}); + if (hasKey) { return; } getFromHighestPriorityCache(key, function(err, result, index) { if (err) {