Browse Source

Moved cache queue before of the store get function

feature/nested-cache-fetch-fix
Alejandro Torrado 10 years ago
parent
commit
46459ff1a7
  1. 45
      lib/caching.js
  2. 45
      lib/multi_caching.js

45
lib/caching.js

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

45
lib/multi_caching.js

@ -47,9 +47,23 @@ var multi_caching = function (caches) {
ttl = undefined; ttl = undefined;
} }
if (self.queues[key]) {
self.queues[key].push(cb);
return;
}
self.queues[key] = [cb];
function fillCallbacks(err, data){
self.queues[key].forEach(function(f){
f(err, data);
});
delete self.queues[key];
}
get_from_highest_priority_cache(key, function (err, result, index) { get_from_highest_priority_cache(key, function (err, result, index) {
if (err) { if (err) {
return cb(err); return fillCallbacks(err);
} else if (result) { } else if (result) {
var caches_to_update = caches.slice(0, index); var caches_to_update = caches.slice(0, index);
var opts = { var opts = {
@ -58,38 +72,21 @@ var multi_caching = function (caches) {
ttl: ttl ttl: ttl
}; };
set_in_multiple_caches(caches_to_update, opts, function (err) { set_in_multiple_caches(caches_to_update, opts, function (err) {
cb(err, result); fillCallbacks(err, result);
}); });
} else if (self.queues[key]) {
self.queues[key].push(cb);
} else { } else {
self.queues[key] = [cb]; work(function (err, data) {
work(function () { if (err) {
var work_args = Array.prototype.slice.call(arguments, 0); fillCallbacks(err, data);
if (work_args[0]) { // assume first arg is an error
self.queues[key].forEach(function (done) {
done.call(null, work_args[0]);
});
delete self.queues[key];
return; return;
} }
var opts = { var opts = {
key: key, key: key,
value: work_args[1], value: data,
ttl: ttl ttl: ttl
}; };
set_in_multiple_caches(caches, opts, function (err) { set_in_multiple_caches(caches, opts, function (err) {
if (err) { fillCallbacks(null, data);
self.queues[key].forEach(function (done) {
done.call(null, err);
});
delete self.queues[key];
return;
}
self.queues[key].forEach(function (done) {
done.apply(null, work_args);
});
delete self.queues[key];
}); });
}); });
} }

Loading…
Cancel
Save