Skip to content
This repository has been archived by the owner on Nov 4, 2022. It is now read-only.

Commit

Permalink
Adding caching for the config objects.
Browse files Browse the repository at this point in the history
Close #331.
  • Loading branch information
henvic committed Jan 14, 2015
1 parent b5d627a commit cd4beb8
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lib/configs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 -------------------------------------------------------------------

Expand All @@ -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 = {};
Expand All @@ -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) {
Expand All @@ -58,6 +69,7 @@ exports.getGlobalConfig = function(opt_plugin) {

if (!fs.existsSync(configPath)) {
fs.writeFileSync(configPath, '{}');
cache = {};
}

try {
Expand Down Expand Up @@ -99,6 +111,7 @@ exports.removeGlobalConfig = function(key) {
exports.getUserHomePath(),
JSON.stringify(config, null, 4)
);
cache = {};
};

exports.writeGlobalConfig = function(jsonPath, value) {
Expand All @@ -120,6 +133,7 @@ exports.writeGlobalConfig = function(jsonPath, value) {
exports.getUserHomePath(),
JSON.stringify(config, null, 4)
);
cache = {};
};

exports.writeGlobalConfigCredentials = function(user, token) {
Expand Down

0 comments on commit cd4beb8

Please sign in to comment.