|
|
@ -40,38 +40,37 @@ var caching = function (args) { |
|
|
|
ttl = undefined; |
|
|
|
} |
|
|
|
|
|
|
|
self.store.get(key, function (err, result) { |
|
|
|
if (err && (!self.ignoreCacheErrors)) { |
|
|
|
cb(err); |
|
|
|
} else if (result) { |
|
|
|
cb.call(cb, null, result); |
|
|
|
} else if (self.queues[key]) { |
|
|
|
if (self.queues[key]) { |
|
|
|
self.queues[key].push(cb); |
|
|
|
} else { |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
self.queues[key] = [cb]; |
|
|
|
|
|
|
|
work(function () { |
|
|
|
var work_args = Array.prototype.slice.call(arguments, 0); |
|
|
|
if (work_args[0]) { // assume first arg is an error
|
|
|
|
self.queues[key].forEach(function (done) { |
|
|
|
done.call(null, work_args[0]); |
|
|
|
function fillCallbacks(err, data){ |
|
|
|
self.queues[key].forEach(function(f){ |
|
|
|
f(err, data); |
|
|
|
}); |
|
|
|
delete self.queues[key]; |
|
|
|
} |
|
|
|
|
|
|
|
self.store.get(key, function (err, result) { |
|
|
|
if (err && (!self.ignoreCacheErrors)) { |
|
|
|
fillCallbacks(err); |
|
|
|
} else if (result) { |
|
|
|
fillCallbacks(null, result); |
|
|
|
} else { |
|
|
|
work(function (err, data) { |
|
|
|
if (err) { |
|
|
|
fillCallbacks(err); |
|
|
|
return; |
|
|
|
} |
|
|
|
// Subsequently assume second arg is result.
|
|
|
|
self.store.set(key, work_args[1], ttl, function (err) { |
|
|
|
self.store.set(key, data, ttl, function (err) { |
|
|
|
if (err && (!self.ignoreCacheErrors)) { |
|
|
|
self.queues[key].forEach(function (done) { |
|
|
|
done.call(null, err); |
|
|
|
}); |
|
|
|
fillCallbacks(err); |
|
|
|
} else { |
|
|
|
self.queues[key].forEach(function (done) { |
|
|
|
done.apply(null, work_args); |
|
|
|
}); |
|
|
|
fillCallbacks(err, data); |
|
|
|
} |
|
|
|
|
|
|
|
delete self.queues[key]; |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|