c06cba81f4
- uses queue-async to parallelize the call to invalidate
42 lines
1.2 KiB
JavaScript
42 lines
1.2 KiB
JavaScript
var queue = require('queue-async');
|
|
|
|
/**
|
|
* @param {Array|Object} cacheBackends each backend backend should respond to `invalidate(cacheObject, callback)` method
|
|
* @constructor
|
|
*/
|
|
function SurrogateKeysCache(cacheBackends) {
|
|
this.cacheBackends = Array.isArray(cacheBackends) ? cacheBackends : [cacheBackends];
|
|
}
|
|
|
|
module.exports = SurrogateKeysCache;
|
|
|
|
|
|
/**
|
|
* @param response should respond to `header(key, value)` method
|
|
* @param cacheObject should respond to `key() -> String` method
|
|
*/
|
|
SurrogateKeysCache.prototype.tag = function(response, cacheObject) {
|
|
response.header('Surrogate-Key', cacheObject.key());
|
|
};
|
|
|
|
/**
|
|
* @param cacheObject should respond to `key() -> String` method
|
|
* @param {Function} callback
|
|
*/
|
|
SurrogateKeysCache.prototype.invalidate = function(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);
|
|
});
|
|
};
|