Skip to content

Commit

Permalink
restore caching functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
msimerson committed Feb 28, 2016
1 parent 9a39b10 commit 57c0df2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
31 changes: 15 additions & 16 deletions configfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var regex = exports.regex = {

var cfreader = exports;

var config_dirs = [
var config_dir_candidates = [
path.join(__dirname, 'config'), // Haraka ./config dir
__dirname, // npm packaged plugins
];
Expand All @@ -36,11 +36,11 @@ function get_path_to_config_dir () {
return;
}

for (var i=0; i < config_dirs.length; i++) {
for (var i=0; i < config_dir_candidates.length; i++) {
try {
var stat = fs.statSync(config_dirs[i]);
var stat = fs.statSync(config_dir_candidates[i]);
if (stat && stat.isDirectory()) {
cfreader.config_path = config_dirs[i];
cfreader.config_path = config_dir_candidates[i];
return;
}
}
Expand Down Expand Up @@ -185,14 +185,10 @@ cfreader.read_config = function(name, type, cb, options) {
// Check cache first
if (!process.env.WITHOUT_CONFIG_CACHE) {
var cache_key = cfreader.get_cache_key(name, options);
// console.log('\tcache_key: ' + cache_key);
if (cfreader._config_cache[cache_key]) {
var cached = cfreader._config_cache[cache_key];
// Make sure that any .ini file booleans are applied
if (type === 'ini' && (options && options.booleans &&
Array.isArray(options.booleans)))
{
cfreader.init_booleans(options, cached);
}
// console.log('\t' + name + ' is cached');
return cached;
}
}
Expand Down Expand Up @@ -272,6 +268,7 @@ cfreader.load_config = function (name, type, options) {
cfrType = cfreader.get_filetype_reader(type);
}

var cache_key = cfreader.get_cache_key(name, options);
try {
switch (type) {
case 'ini':
Expand All @@ -280,29 +277,31 @@ cfreader.load_config = function (name, type, options) {
case 'json':
case 'yaml':
result = cfrType.load(name);
cfreader.process_file_overrides(name, result);
cfreader.process_file_overrides(name, options, result);
break;
// case 'binary':
default:
result = cfrType.load(name, type, options, regex);
}
cfreader._config_cache[cache_key] = result;
}
catch (err) {
if (err.code !== 'EBADF') throw err;
if (cfreader._config_cache[name]) {
result = cfreader._config_cache[name];
if (cfreader._config_cache[cache_key]) {
result = cfreader._config_cache[cache_key];
}
}
return result;
};

cfreader.process_file_overrides = function (name, result) {
cfreader.process_file_overrides = function (name, options, result) {
// We might be re-loading this file:
// * build a list of cached overrides
// * remove them and add them back
var cp = cfreader.config_path;
if (cfreader._config_cache[name]) {
var ck_keys = Object.keys(cfreader._config_cache[name]);
var cache_key = cfreader.get_cache_key(name, options);
if (cfreader._config_cache[cache_key]) {
var ck_keys = Object.keys(cfreader._config_cache[cache_key]);
for (var i=0; i<ck_keys.length; i++) {
if (ck_keys[i].substr(0,1) !== '!') continue;
delete cfreader._config_cache[path.join(cp, ck_keys[i].substr(1))];
Expand Down
25 changes: 21 additions & 4 deletions test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,20 @@ var config = require('../config');
var cb = function () { return false; };
var opts = { booleans: ['arg1'] };

exports.module_config = {
exports.config = {
'new' : function (test) {
test.expect(1);
// console.log(config);
test.ok(/haraka\-config\/test\/config$/.test(config.root_path));
test.done();
},
'module_config' : function (test) {
test.expect(2);
var c = config.module_config('foo', 'bar');
test.ok(c.root_path);
test.equal(c.root_path, 'foo/config');
test.equal(c.overrides_path, 'bar/config');
test.done();
}
},
};

exports.arrange_args = {
Expand Down Expand Up @@ -157,7 +164,11 @@ var yamlRes = {

function _test_get(test, name, type, callback, options, expected) {
test.expect(1);
test.deepEqual(config.get(name, type, callback, options), expected);
var cfg = config.get(name, type, callback, options);
if (cfg && typeof cfg === 'object' && cfg.cached !== undefined) {
// delete cfg.cached;
}
test.deepEqual(cfg, expected);
test.done();
}

Expand All @@ -166,6 +177,12 @@ exports.get = {
'test (non-existing)' : function (test) {
_test_get(test, 'test', null, null, null, null);
},
'test (non-existing, cached)' : function (test) {
test.expect(1);
var cfg = config.get('test', null, null);
test.deepEqual(cfg, null);
test.done();
},

// config.get('test.ini');
'test.ini, no opts' : function (test) {
Expand Down

0 comments on commit 57c0df2

Please sign in to comment.