|
|
@ -1,12 +1,10 @@ |
|
|
|
/*jshint -W072 */ |
|
|
|
|
|
|
|
var async = require('async'); |
|
|
|
var domain = require('domain'); |
|
|
|
|
|
|
|
/** |
|
|
|
* Module that lets you specify a hierarchy of caches. |
|
|
|
*/ |
|
|
|
var multi_caching = function (caches) { |
|
|
|
var multi_caching = function(caches) { |
|
|
|
var self = {}; |
|
|
|
if (!Array.isArray(caches)) { |
|
|
|
throw new Error('multi_caching requires an array of caches'); |
|
|
@ -21,40 +19,29 @@ var multi_caching = function (caches) { |
|
|
|
} |
|
|
|
|
|
|
|
var i = 0; |
|
|
|
async.forEachSeries(caches, function (cache, async_cb) { |
|
|
|
if(typeof options === 'object'){ |
|
|
|
cache.store.get(key, options, function (err, result) { |
|
|
|
if (err) { |
|
|
|
return cb(err); |
|
|
|
} |
|
|
|
if (result) { |
|
|
|
// break out of async loop.
|
|
|
|
return cb(err, result, i); |
|
|
|
} |
|
|
|
|
|
|
|
i += 1; |
|
|
|
async_cb(err); |
|
|
|
}); |
|
|
|
}else{ |
|
|
|
cache.store.get(key, function (err, result) { |
|
|
|
if (err) { |
|
|
|
return cb(err); |
|
|
|
} |
|
|
|
if (result) { |
|
|
|
// break out of async loop.
|
|
|
|
return cb(err, result, i); |
|
|
|
} |
|
|
|
async.forEachSeries(caches, function(cache, async_cb) { |
|
|
|
var callback = function(err, result) { |
|
|
|
if (err) { |
|
|
|
return cb(err); |
|
|
|
} |
|
|
|
if (result) { |
|
|
|
// break out of async loop.
|
|
|
|
return cb(err, result, i); |
|
|
|
} |
|
|
|
|
|
|
|
i += 1; |
|
|
|
async_cb(err); |
|
|
|
}); |
|
|
|
i += 1; |
|
|
|
async_cb(err); |
|
|
|
}; |
|
|
|
if (typeof options === 'object') { |
|
|
|
cache.store.get(key, options, callback); |
|
|
|
}else { |
|
|
|
cache.store.get(key, callback); |
|
|
|
} |
|
|
|
|
|
|
|
}, cb); |
|
|
|
} |
|
|
|
|
|
|
|
function set_in_multiple_caches(caches, opts, cb) { |
|
|
|
async.forEach(caches, function (cache, async_cb) { |
|
|
|
async.forEach(caches, function(cache, async_cb) { |
|
|
|
if (typeof opts.options !== 'object') { |
|
|
|
cache.store.set(opts.key, opts.value, opts.ttl, async_cb); |
|
|
|
} else { |
|
|
@ -68,8 +55,8 @@ var multi_caching = function (caches) { |
|
|
|
* |
|
|
|
* When a key is found in a lower cache, all higher levels are updated |
|
|
|
*/ |
|
|
|
self.get_and_pass_up = function (key, cb) { |
|
|
|
get_from_highest_priority_cache(key, function (err, result, index) { |
|
|
|
self.get_and_pass_up = function(key, cb) { |
|
|
|
get_from_highest_priority_cache(key, function(err, result, index) { |
|
|
|
if (err) { |
|
|
|
return cb(err); |
|
|
|
} |
|
|
@ -78,7 +65,7 @@ var multi_caching = function (caches) { |
|
|
|
|
|
|
|
if (result !== undefined && index) { |
|
|
|
var cachesToUpdate = caches.slice(0, index); |
|
|
|
async.forEach(cachesToUpdate, function (cache, async_cb) { |
|
|
|
async.forEach(cachesToUpdate, function(cache, async_cb) { |
|
|
|
cache.set(key, result, result.ttl, async_cb); |
|
|
|
}); |
|
|
|
} |
|
|
@ -95,7 +82,7 @@ var multi_caching = function (caches) { |
|
|
|
* If a key doesn't exist in a higher-priority cache but exists in a lower-priority |
|
|
|
* cache, it gets set in all higher-priority caches. |
|
|
|
*/ |
|
|
|
self.wrap = function (key, work, options, cb) { |
|
|
|
self.wrap = function(key, work, options, cb) { |
|
|
|
if (typeof options === 'function') { |
|
|
|
cb = options; |
|
|
|
options = undefined; |
|
|
@ -109,14 +96,14 @@ var multi_caching = function (caches) { |
|
|
|
self.queues[key] = [{cb: cb, domain: process.domain}]; |
|
|
|
|
|
|
|
function fillCallbacks(err, data) { |
|
|
|
self.queues[key].forEach(function (task) { |
|
|
|
self.queues[key].forEach(function(task) { |
|
|
|
var taskDomain = task.domain || domain.create(); |
|
|
|
taskDomain.bind(task.cb)(err, data); |
|
|
|
}); |
|
|
|
delete self.queues[key]; |
|
|
|
} |
|
|
|
|
|
|
|
get_from_highest_priority_cache(key, function (err, result, index) { |
|
|
|
get_from_highest_priority_cache(key, function(err, result, index) { |
|
|
|
if (err) { |
|
|
|
return fillCallbacks(err); |
|
|
|
} else if (result) { |
|
|
@ -131,16 +118,16 @@ var multi_caching = function (caches) { |
|
|
|
opts.ttl = options; |
|
|
|
} |
|
|
|
|
|
|
|
set_in_multiple_caches(caches_to_update, opts, function (err) { |
|
|
|
set_in_multiple_caches(caches_to_update, opts, function(err) { |
|
|
|
fillCallbacks(err, result); |
|
|
|
}); |
|
|
|
} else { |
|
|
|
domain |
|
|
|
.create() |
|
|
|
.on('error', function (err) { |
|
|
|
.on('error', function(err) { |
|
|
|
fillCallbacks(err); |
|
|
|
}) |
|
|
|
.bind(work)(function (err, data) { |
|
|
|
.bind(work)(function(err, data) { |
|
|
|
if (err) { |
|
|
|
fillCallbacks(err); |
|
|
|
return; |
|
|
@ -154,7 +141,7 @@ var multi_caching = function (caches) { |
|
|
|
if (typeof options !== 'object') { |
|
|
|
opts.ttl = options; |
|
|
|
} |
|
|
|
set_in_multiple_caches(caches, opts, function (err) { |
|
|
|
set_in_multiple_caches(caches, opts, function(err) { |
|
|
|
if (err) { |
|
|
|
fillCallbacks(err); |
|
|
|
} else { |
|
|
@ -166,7 +153,7 @@ var multi_caching = function (caches) { |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
self.set = function (key, value, options, cb) { |
|
|
|
self.set = function(key, value, options, cb) { |
|
|
|
var opts = { |
|
|
|
key: key, |
|
|
|
value: value, |
|
|
@ -178,7 +165,7 @@ var multi_caching = function (caches) { |
|
|
|
set_in_multiple_caches(caches, opts, cb); |
|
|
|
}; |
|
|
|
|
|
|
|
self.get = function (key, options, cb) { |
|
|
|
self.get = function(key, options, cb) { |
|
|
|
if (typeof options === 'function') { |
|
|
|
cb = options; |
|
|
|
options = false; |
|
|
@ -186,16 +173,16 @@ var multi_caching = function (caches) { |
|
|
|
get_from_highest_priority_cache(key, options, cb); |
|
|
|
}; |
|
|
|
|
|
|
|
self.del = function (key, options, cb) { |
|
|
|
self.del = function(key, options, cb) { |
|
|
|
if (typeof options === 'function') { |
|
|
|
cb = options; |
|
|
|
options = false; |
|
|
|
} |
|
|
|
async.forEach(caches, function (cache, async_cb) { |
|
|
|
if(typeof options ==='object'){ |
|
|
|
cache.store.del(key, options, async_cb ); |
|
|
|
}else{ |
|
|
|
cache.store.del(key, async_cb ); |
|
|
|
async.forEach(caches, function(cache, async_cb) { |
|
|
|
if (typeof options === 'object') { |
|
|
|
cache.store.del(key, options, async_cb); |
|
|
|
}else { |
|
|
|
cache.store.del(key, async_cb); |
|
|
|
} |
|
|
|
}, cb); |
|
|
|
}; |
|
|
|