SurrogateKeysCache now accepts several cache backends

- uses queue-async to parallelize the call to invalidate
This commit is contained in:
Raul Ochoa
2015-04-27 16:22:37 +02:00
parent ad5514dd02
commit c06cba81f4
+19 -4
View File
@@ -1,9 +1,11 @@
var queue = require('queue-async');
/**
* @param cacheBackend should respond to `invalidate(cacheObject, callback)` method
* @param {Array|Object} cacheBackends each backend backend should respond to `invalidate(cacheObject, callback)` method
* @constructor
*/
function SurrogateKeysCache(cacheBackend) {
this.cacheBackend = cacheBackend;
function SurrogateKeysCache(cacheBackends) {
this.cacheBackends = Array.isArray(cacheBackends) ? cacheBackends : [cacheBackends];
}
module.exports = SurrogateKeysCache;
@@ -22,5 +24,18 @@ SurrogateKeysCache.prototype.tag = function(response, cacheObject) {
* @param {Function} callback
*/
SurrogateKeysCache.prototype.invalidate = function(cacheObject, callback) {
this.cacheBackend.invalidate(cacheObject, callback);
var invalidationQueue = queue(this.cacheBackends.length);
this.cacheBackends.forEach(function(cacheBackend) {
invalidationQueue.defer(function(cacheBackend, done) {
cacheBackend.invalidate(cacheObject, done);
}, cacheBackend);
});
invalidationQueue.awaitAll(function(err, result) {
if (err) {
return callback(err);
}
callback(null, result);
});
};