diff --git a/lib/caching.js b/lib/caching.js index 6cb1841..7c35836 100644 --- a/lib/caching.js +++ b/lib/caching.js @@ -1,6 +1,7 @@ /*jshint maxcomplexity:15*/ /*jshint -W072 */ var domain = require('domain'); +var CallbackFiller = require('./callback_filler'); var caching = function(args) { args = args || {}; @@ -21,7 +22,7 @@ var caching = function(args) { // do we handle a cache error the same as a cache miss? self.ignoreCacheErrors = args.ignoreCacheErrors || false; - self.queues = {}; + var callbackFiller = new CallbackFiller(); /** * Wraps a function in cache. I.e., the first time the function is run, @@ -43,44 +44,34 @@ var caching = function(args) { options = undefined; } - if (self.queues[key]) { - self.queues[key].push({cb: cb, domain: process.domain}); + if (callbackFiller.queues[key]) { + callbackFiller.queues[key].push({cb: cb, domain: process.domain}); return; } - self.queues[key] = [{cb: cb, domain: process.domain}]; - - function fillCallbacks(err, data) { - var waiting = self.queues[key]; - delete self.queues[key]; - - waiting.forEach(function(task) { - var taskDomain = task.domain || domain.create(); - taskDomain.bind(task.cb)(err, data); - }); - } + callbackFiller.queues[key] = [{cb: cb, domain: process.domain}]; self.store.get(key, options, function(err, result) { if (err && (!self.ignoreCacheErrors)) { - fillCallbacks(err); + callbackFiller.fill(key, err); } else if (result) { - fillCallbacks(null, result); + callbackFiller.fill(key, null, result); } else { domain .create() .on('error', function(err) { - fillCallbacks(err); + callbackFiller.fill(key, err); }) .bind(work)(function(err, data) { if (err) { - fillCallbacks(err); + callbackFiller.fill(key, err); return; } self.store.set(key, data, options, function(err) { if (err && (!self.ignoreCacheErrors)) { - fillCallbacks(err); + callbackFiller.fill(key, err); } else { - fillCallbacks(null, data); + callbackFiller.fill(key, null, data); } }); }); diff --git a/lib/callback_filler.js b/lib/callback_filler.js new file mode 100644 index 0000000..163aa7e --- /dev/null +++ b/lib/callback_filler.js @@ -0,0 +1,19 @@ +var domain = require('domain'); + +function CallbackFiller() { + this.queues = {}; +} + +CallbackFiller.prototype.fill = function(key, err, data) { + var self = this; + + var waiting = self.queues[key]; + delete self.queues[key]; + + waiting.forEach(function(task) { + var taskDomain = task.domain || domain.create(); + taskDomain.bind(task.cb)(err, data); + }); +}; + +module.exports = CallbackFiller; diff --git a/lib/multi_caching.js b/lib/multi_caching.js index 18bfe5e..72d67df 100644 --- a/lib/multi_caching.js +++ b/lib/multi_caching.js @@ -1,5 +1,6 @@ var async = require('async'); var domain = require('domain'); +var CallbackFiller = require('./callback_filler'); /** * Module that lets you specify a hierarchy of caches. @@ -10,7 +11,7 @@ var multi_caching = function(caches) { throw new Error('multi_caching requires an array of caches'); } - self.queues = {}; + var callbackFiller = new CallbackFiller(); function get_from_highest_priority_cache(key, options, cb) { if (typeof options === 'function') { @@ -88,26 +89,16 @@ var multi_caching = function(caches) { options = undefined; } - if (self.queues[key]) { - self.queues[key].push({cb: cb, domain: process.domain}); + if (callbackFiller.queues[key]) { + callbackFiller.queues[key].push({cb: cb, domain: process.domain}); return; } - self.queues[key] = [{cb: cb, domain: process.domain}]; - - function fillCallbacks(err, data) { - var waiting = self.queues[key]; - delete self.queues[key]; - - waiting.forEach(function(task) { - var taskDomain = task.domain || domain.create(); - taskDomain.bind(task.cb)(err, data); - }); - } + callbackFiller.queues[key] = [{cb: cb, domain: process.domain}]; get_from_highest_priority_cache(key, function(err, result, index) { if (err) { - return fillCallbacks(err); + return callbackFiller.fill(key, err); } else if (result) { var caches_to_update = caches.slice(0, index); var opts = { @@ -121,17 +112,17 @@ var multi_caching = function(caches) { } set_in_multiple_caches(caches_to_update, opts, function(err) { - fillCallbacks(err, result); + callbackFiller.fill(key, err, result); }); } else { domain .create() .on('error', function(err) { - fillCallbacks(err); + callbackFiller.fill(key, err); }) .bind(work)(function(err, data) { if (err) { - fillCallbacks(err); + callbackFiller.fill(key, err); return; } var opts = { @@ -145,9 +136,9 @@ var multi_caching = function(caches) { } set_in_multiple_caches(caches, opts, function(err) { if (err) { - fillCallbacks(err); + callbackFiller.fill(key, err); } else { - fillCallbacks(null, data); + callbackFiller.fill(key, null, data); } }); });