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