|
|
@ -12,6 +12,9 @@ var CallbackFiller = require('./callback_filler'); |
|
|
|
* with every value returned from cache or from a wrapped function. This lets you specify |
|
|
|
* which values should and should not be cached. If the function returns true, it will be |
|
|
|
* stored in cache. By default it caches everything except undefined. |
|
|
|
* |
|
|
|
* If an underlying cache specifies its own isCacheableValue function, that function will |
|
|
|
* be used instead of the multiCaching's _isCacheableValue function. |
|
|
|
*/ |
|
|
|
var multiCaching = function(caches, options) { |
|
|
|
var self = {}; |
|
|
@ -31,6 +34,19 @@ var multiCaching = function(caches, options) { |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* If the underlying cache specifies its own isCacheableValue function (such |
|
|
|
* as how node-cache-manager-redis does), use that function, otherwise use |
|
|
|
* self._isCacheableValue function. |
|
|
|
*/ |
|
|
|
function getIsCacheableValueFunction(cache) { |
|
|
|
if (cache.store && typeof cache.store.isCacheableValue === 'function') { |
|
|
|
return cache.store.isCacheableValue; |
|
|
|
} else { |
|
|
|
return self._isCacheableValue; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
function getFromHighestPriorityCache(key, options, cb) { |
|
|
|
if (typeof options === 'function') { |
|
|
|
cb = options; |
|
|
@ -43,7 +59,10 @@ var multiCaching = function(caches, options) { |
|
|
|
if (err) { |
|
|
|
return next(err); |
|
|
|
} |
|
|
|
if (self._isCacheableValue(result)) { |
|
|
|
|
|
|
|
var _isCacheableValue = getIsCacheableValueFunction(cache); |
|
|
|
|
|
|
|
if (_isCacheableValue(result)) { |
|
|
|
// break out of async loop.
|
|
|
|
return cb(err, result, i); |
|
|
|
} |
|
|
@ -59,7 +78,13 @@ var multiCaching = function(caches, options) { |
|
|
|
function setInMultipleCaches(caches, opts, cb) { |
|
|
|
opts.options = opts.options || {}; |
|
|
|
async.each(caches, function(cache, next) { |
|
|
|
cache.store.set(opts.key, opts.value, opts.options, next); |
|
|
|
var _isCacheableValue = getIsCacheableValueFunction(cache); |
|
|
|
|
|
|
|
if (_isCacheableValue(opts.value)) { |
|
|
|
cache.store.set(opts.key, opts.value, opts.options, next); |
|
|
|
} else { |
|
|
|
next(); |
|
|
|
} |
|
|
|
}, cb); |
|
|
|
} |
|
|
|
|
|
|
@ -78,11 +103,14 @@ var multiCaching = function(caches, options) { |
|
|
|
|
|
|
|
cb(err, result); |
|
|
|
|
|
|
|
if (self._isCacheableValue(result) && index) { |
|
|
|
if (index) { |
|
|
|
var cachesToUpdate = caches.slice(0, index); |
|
|
|
async.each(cachesToUpdate, function(cache, next) { |
|
|
|
// We rely on the cache module's default TTL
|
|
|
|
cache.set(key, result, next); |
|
|
|
var _isCacheableValue = getIsCacheableValueFunction(cache); |
|
|
|
if (_isCacheableValue(result)) { |
|
|
|
// We rely on the cache module's default TTL
|
|
|
|
cache.set(key, result, next); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
}); |
|
|
|