|
|
@ -47,16 +47,25 @@ var multiCaching = function(caches, options) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
function getFromHighestPriorityCachePromise(key, options) { |
|
|
|
return new Promise(function(resolve, reject) { |
|
|
|
getFromHighestPriorityCache(key, options, function(err, result) { |
|
|
|
if (err) { |
|
|
|
return reject(err); |
|
|
|
} |
|
|
|
resolve(result); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
function getFromHighestPriorityCache(key, options, cb) { |
|
|
|
if (typeof options === 'function') { |
|
|
|
cb = options; |
|
|
|
options = {}; |
|
|
|
} |
|
|
|
|
|
|
|
var promised = false; |
|
|
|
if (!cb) { |
|
|
|
cb = Promise.defer(); |
|
|
|
promised = true; |
|
|
|
return getFromHighestPriorityCachePromise(key, options); |
|
|
|
} |
|
|
|
|
|
|
|
var i = 0; |
|
|
@ -70,10 +79,7 @@ var multiCaching = function(caches, options) { |
|
|
|
|
|
|
|
if (_isCacheableValue(result)) { |
|
|
|
// break out of async loop.
|
|
|
|
if (!promised) { |
|
|
|
return cb(err, result, i); |
|
|
|
} |
|
|
|
return cb.resolve(result); |
|
|
|
return cb(err, result, i); |
|
|
|
} |
|
|
|
|
|
|
|
i += 1; |
|
|
@ -82,25 +88,26 @@ var multiCaching = function(caches, options) { |
|
|
|
|
|
|
|
cache.store.get(key, options, callback); |
|
|
|
}, function(err, result) { |
|
|
|
if (!promised) { |
|
|
|
return cb(err, result); |
|
|
|
} |
|
|
|
|
|
|
|
return (err) ? cb.reject(err) : cb.resolve(result); |
|
|
|
return cb(err, result); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
if (promised) { |
|
|
|
return cb.promise; |
|
|
|
} |
|
|
|
function setInMultipleCachesPromise(caches, opts) { |
|
|
|
return new Promise(function(resolve, reject) { |
|
|
|
setInMultipleCaches(caches, opts, function(err, result) { |
|
|
|
if (err) { |
|
|
|
return reject(err); |
|
|
|
} |
|
|
|
resolve(result); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
function setInMultipleCaches(caches, opts, cb) { |
|
|
|
opts.options = opts.options || {}; |
|
|
|
|
|
|
|
var promised = false; |
|
|
|
if (!cb) { |
|
|
|
promised = true; |
|
|
|
cb = Promise.defer(); |
|
|
|
return setInMultipleCachesPromise(caches, opts); |
|
|
|
} |
|
|
|
|
|
|
|
async.each(caches, function(cache, next) { |
|
|
@ -112,16 +119,19 @@ var multiCaching = function(caches, options) { |
|
|
|
next(); |
|
|
|
} |
|
|
|
}, function(err, result) { |
|
|
|
if (promised) { |
|
|
|
return (err) ? cb.reject(err) : cb.resolve(result); |
|
|
|
} else { |
|
|
|
cb(err, result); |
|
|
|
} |
|
|
|
cb(err, result); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
if (promised) { |
|
|
|
return cb.promise; |
|
|
|
} |
|
|
|
function getAndPassUpPromise(key) { |
|
|
|
return new Promise(function(resolve, reject) { |
|
|
|
self.getAndPassUp(key, function(err, result) { |
|
|
|
if (err) { |
|
|
|
return reject(err); |
|
|
|
} |
|
|
|
resolve(result); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@ -132,15 +142,13 @@ var multiCaching = function(caches, options) { |
|
|
|
* @param {function} cb |
|
|
|
*/ |
|
|
|
self.getAndPassUp = function(key, cb) { |
|
|
|
var promised = false; |
|
|
|
if (!cb) { |
|
|
|
promised = true; |
|
|
|
cb = Promise.defer(); |
|
|
|
return getAndPassUpPromise(key); |
|
|
|
} |
|
|
|
|
|
|
|
getFromHighestPriorityCache(key, function(err, result, index) { |
|
|
|
if (err) { |
|
|
|
return (!promised) ? cb(err) : cb.reject(err); |
|
|
|
return cb(err); |
|
|
|
} |
|
|
|
|
|
|
|
if (index) { |
|
|
@ -154,14 +162,28 @@ var multiCaching = function(caches, options) { |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
return (!promised) ? cb(err, result) : cb.resolve(result); |
|
|
|
return cb(err, result); |
|
|
|
}); |
|
|
|
|
|
|
|
if (promised) { |
|
|
|
return cb.promise; |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
function wrapPromise(key, promise, options) { |
|
|
|
return new Promise(function(resolve, reject) { |
|
|
|
self.wrap(key, function(cb) { |
|
|
|
Promise.resolve() |
|
|
|
.then(promise) |
|
|
|
.then(function(result) { |
|
|
|
cb(null, result); |
|
|
|
}) |
|
|
|
.catch(cb); |
|
|
|
}, options, function(err, result) { |
|
|
|
if (err) { |
|
|
|
return reject(err); |
|
|
|
} |
|
|
|
resolve(result); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Wraps a function in one or more caches. |
|
|
|
* Has same API as regular caching module. |
|
|
@ -192,14 +214,7 @@ var multiCaching = function(caches, options) { |
|
|
|
} |
|
|
|
|
|
|
|
if (!cb) { |
|
|
|
cb = Promise.defer(); |
|
|
|
var work2 = work; |
|
|
|
work = function(cb) { |
|
|
|
Promise.resolve().then(work2).then(function(res) { |
|
|
|
cb(null, res); |
|
|
|
}) |
|
|
|
.catch(cb); |
|
|
|
}; |
|
|
|
return wrapPromise(key, work, options); |
|
|
|
} |
|
|
|
|
|
|
|
var hasKey = callbackFiller.has(key); |
|
|
@ -230,9 +245,6 @@ var multiCaching = function(caches, options) { |
|
|
|
} |
|
|
|
|
|
|
|
if (!self._isCacheableValue(data)) { |
|
|
|
if (typeof cb === 'object') { |
|
|
|
return cb.resolve(data); |
|
|
|
} |
|
|
|
return cb(); |
|
|
|
} |
|
|
|
|
|
|
@ -244,10 +256,6 @@ var multiCaching = function(caches, options) { |
|
|
|
}); |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
if (typeof cb === 'object') { |
|
|
|
return cb.promise; |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|