Browse Source

extracting CallbackFiller class

feature/specify-what-to-cache
Bryan Donovan 10 years ago
parent
commit
5bcc45fc6b
  1. 31
      lib/caching.js
  2. 19
      lib/callback_filler.js
  3. 31
      lib/multi_caching.js

31
lib/caching.js

@ -1,6 +1,7 @@
/*jshint maxcomplexity:15*/
/*jshint -W072 */
var domain = require('domain');
var CallbackFiller = require('./callback_filler');
var caching = function(args) {
args = args || {};
@ -21,7 +22,7 @@ var caching = function(args) {
// do we handle a cache error the same as a cache miss?
self.ignoreCacheErrors = args.ignoreCacheErrors || false;
self.queues = {};
var callbackFiller = new CallbackFiller();
/**
* Wraps a function in cache. I.e., the first time the function is run,
@ -43,44 +44,34 @@ var caching = function(args) {
options = undefined;
}
if (self.queues[key]) {
self.queues[key].push({cb: cb, domain: process.domain});
if (callbackFiller.queues[key]) {
callbackFiller.queues[key].push({cb: cb, domain: process.domain});
return;
}
self.queues[key] = [{cb: cb, domain: process.domain}];
function fillCallbacks(err, data) {
var waiting = self.queues[key];
delete self.queues[key];
waiting.forEach(function(task) {
var taskDomain = task.domain || domain.create();
taskDomain.bind(task.cb)(err, data);
});
}
callbackFiller.queues[key] = [{cb: cb, domain: process.domain}];
self.store.get(key, options, function(err, result) {
if (err && (!self.ignoreCacheErrors)) {
fillCallbacks(err);
callbackFiller.fill(key, err);
} else if (result) {
fillCallbacks(null, result);
callbackFiller.fill(key, null, result);
} else {
domain
.create()
.on('error', function(err) {
fillCallbacks(err);
callbackFiller.fill(key, err);
})
.bind(work)(function(err, data) {
if (err) {
fillCallbacks(err);
callbackFiller.fill(key, err);
return;
}
self.store.set(key, data, options, function(err) {
if (err && (!self.ignoreCacheErrors)) {
fillCallbacks(err);
callbackFiller.fill(key, err);
} else {
fillCallbacks(null, data);
callbackFiller.fill(key, null, data);
}
});
});

19
lib/callback_filler.js

@ -0,0 +1,19 @@
var domain = require('domain');
function CallbackFiller() {
this.queues = {};
}
CallbackFiller.prototype.fill = function(key, err, data) {
var self = this;
var waiting = self.queues[key];
delete self.queues[key];
waiting.forEach(function(task) {
var taskDomain = task.domain || domain.create();
taskDomain.bind(task.cb)(err, data);
});
};
module.exports = CallbackFiller;

31
lib/multi_caching.js

@ -1,5 +1,6 @@
var async = require('async');
var domain = require('domain');
var CallbackFiller = require('./callback_filler');
/**
* Module that lets you specify a hierarchy of caches.
@ -10,7 +11,7 @@ var multi_caching = function(caches) {
throw new Error('multi_caching requires an array of caches');
}
self.queues = {};
var callbackFiller = new CallbackFiller();
function get_from_highest_priority_cache(key, options, cb) {
if (typeof options === 'function') {
@ -88,26 +89,16 @@ var multi_caching = function(caches) {
options = undefined;
}
if (self.queues[key]) {
self.queues[key].push({cb: cb, domain: process.domain});
if (callbackFiller.queues[key]) {
callbackFiller.queues[key].push({cb: cb, domain: process.domain});
return;
}
self.queues[key] = [{cb: cb, domain: process.domain}];
function fillCallbacks(err, data) {
var waiting = self.queues[key];
delete self.queues[key];
waiting.forEach(function(task) {
var taskDomain = task.domain || domain.create();
taskDomain.bind(task.cb)(err, data);
});
}
callbackFiller.queues[key] = [{cb: cb, domain: process.domain}];
get_from_highest_priority_cache(key, function(err, result, index) {
if (err) {
return fillCallbacks(err);
return callbackFiller.fill(key, err);
} else if (result) {
var caches_to_update = caches.slice(0, index);
var opts = {
@ -121,17 +112,17 @@ var multi_caching = function(caches) {
}
set_in_multiple_caches(caches_to_update, opts, function(err) {
fillCallbacks(err, result);
callbackFiller.fill(key, err, result);
});
} else {
domain
.create()
.on('error', function(err) {
fillCallbacks(err);
callbackFiller.fill(key, err);
})
.bind(work)(function(err, data) {
if (err) {
fillCallbacks(err);
callbackFiller.fill(key, err);
return;
}
var opts = {
@ -145,9 +136,9 @@ var multi_caching = function(caches) {
}
set_in_multiple_caches(caches, opts, function(err) {
if (err) {
fillCallbacks(err);
callbackFiller.fill(key, err);
} else {
fillCallbacks(null, data);
callbackFiller.fill(key, null, data);
}
});
});

Loading…
Cancel
Save