|
|
@ -47,12 +47,27 @@ 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 = {}; |
|
|
|
} |
|
|
|
|
|
|
|
if (!cb) { |
|
|
|
return getFromHighestPriorityCachePromise(key, options); |
|
|
|
} |
|
|
|
|
|
|
|
var i = 0; |
|
|
|
async.eachSeries(caches, function(cache, next) { |
|
|
|
var callback = function(err, result) { |
|
|
@ -72,11 +87,29 @@ var multiCaching = function(caches, options) { |
|
|
|
}; |
|
|
|
|
|
|
|
cache.store.get(key, options, callback); |
|
|
|
}, cb); |
|
|
|
}, function(err, result) { |
|
|
|
return cb(err, result); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
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 || {}; |
|
|
|
|
|
|
|
if (!cb) { |
|
|
|
return setInMultipleCachesPromise(caches, opts); |
|
|
|
} |
|
|
|
|
|
|
|
async.each(caches, function(cache, next) { |
|
|
|
var _isCacheableValue = getIsCacheableValueFunction(cache); |
|
|
|
|
|
|
@ -85,7 +118,20 @@ var multiCaching = function(caches, options) { |
|
|
|
} else { |
|
|
|
next(); |
|
|
|
} |
|
|
|
}, cb); |
|
|
|
}, function(err, result) { |
|
|
|
cb(err, result); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
function getAndPassUpPromise(key) { |
|
|
|
return new Promise(function(resolve, reject) { |
|
|
|
self.getAndPassUp(key, function(err, result) { |
|
|
|
if (err) { |
|
|
|
return reject(err); |
|
|
|
} |
|
|
|
resolve(result); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@ -96,13 +142,15 @@ var multiCaching = function(caches, options) { |
|
|
|
* @param {function} cb |
|
|
|
*/ |
|
|
|
self.getAndPassUp = function(key, cb) { |
|
|
|
if (!cb) { |
|
|
|
return getAndPassUpPromise(key); |
|
|
|
} |
|
|
|
|
|
|
|
getFromHighestPriorityCache(key, function(err, result, index) { |
|
|
|
if (err) { |
|
|
|
return cb(err); |
|
|
|
} |
|
|
|
|
|
|
|
cb(err, result); |
|
|
|
|
|
|
|
if (index) { |
|
|
|
var cachesToUpdate = caches.slice(0, index); |
|
|
|
async.each(cachesToUpdate, function(cache, next) { |
|
|
@ -113,9 +161,29 @@ var multiCaching = function(caches, options) { |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
return cb(err, result); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
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. |
|
|
@ -145,6 +213,10 @@ var multiCaching = function(caches, options) { |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
if (!cb) { |
|
|
|
return wrapPromise(key, work, options); |
|
|
|
} |
|
|
|
|
|
|
|
var hasKey = callbackFiller.has(key); |
|
|
|
callbackFiller.add(key, {cb: cb, domain: process.domain}); |
|
|
|
if (hasKey) { return; } |
|
|
@ -165,8 +237,6 @@ var multiCaching = function(caches, options) { |
|
|
|
.on('error', function(err) { |
|
|
|
if (callbackFiller.has(key)) { |
|
|
|
callbackFiller.fill(key, err); |
|
|
|
} else { |
|
|
|
cb(err); |
|
|
|
} |
|
|
|
}) |
|
|
|
.bind(work)(function(err, data) { |
|
|
@ -211,7 +281,7 @@ var multiCaching = function(caches, options) { |
|
|
|
options: options |
|
|
|
}; |
|
|
|
|
|
|
|
setInMultipleCaches(caches, opts, cb); |
|
|
|
return setInMultipleCaches(caches, opts, cb); |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
@ -230,7 +300,7 @@ var multiCaching = function(caches, options) { |
|
|
|
options = {}; |
|
|
|
} |
|
|
|
|
|
|
|
getFromHighestPriorityCache(key, options, cb); |
|
|
|
return getFromHighestPriorityCache(key, options, cb); |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|