From cd4beb81b0089c80899eca614f8401f1ab296800 Mon Sep 17 00:00:00 2001 From: Henrique Vicente Date: Thu, 1 Jan 2015 01:15:35 -0300 Subject: [PATCH] Adding caching for the config objects. Close #331. --- lib/configs.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/configs.js b/lib/configs.js index d961b489..9abe264e 100644 --- a/lib/configs.js +++ b/lib/configs.js @@ -14,7 +14,8 @@ var fs = require('fs'), npm = require('npm'), path = require('path'), userhome = require('userhome'), - which = require('which'); + which = require('which'), + cache = {}; // -- Config ------------------------------------------------------------------- @@ -26,7 +27,7 @@ exports.getUserHomePath = function() { return userhome('.gh.json'); }; -exports.getConfig = function(opt_plugin) { +function getConfig(opt_plugin) { var globalConfig = exports.getGlobalConfig(opt_plugin), projectConfig, result = {}; @@ -47,6 +48,16 @@ exports.getConfig = function(opt_plugin) { catch (err) { return globalConfig; } +} + +exports.getConfig = function(opt_plugin) { + if (cache[opt_plugin]) { + return cache[opt_plugin]; + } + + cache[opt_plugin] = getConfig(opt_plugin); + + return cache[opt_plugin]; }; exports.getGlobalConfig = function(opt_plugin) { @@ -58,6 +69,7 @@ exports.getGlobalConfig = function(opt_plugin) { if (!fs.existsSync(configPath)) { fs.writeFileSync(configPath, '{}'); + cache = {}; } try { @@ -99,6 +111,7 @@ exports.removeGlobalConfig = function(key) { exports.getUserHomePath(), JSON.stringify(config, null, 4) ); + cache = {}; }; exports.writeGlobalConfig = function(jsonPath, value) { @@ -120,6 +133,7 @@ exports.writeGlobalConfig = function(jsonPath, value) { exports.getUserHomePath(), JSON.stringify(config, null, 4) ); + cache = {}; }; exports.writeGlobalConfigCredentials = function(user, token) {