Skip to content

Commit 9062c5d

Browse files
committed
✨ feat(config.ts): add caching mechanism to reduce file I/O operations
A caching mechanism has been added to the getConfig function to improve performance by reducing the number of file I/O operations. The function now checks if the configCache variable is enabled and returns the cached value if it is. If the cache is not enabled, the function reads the configuration file and returns the value. After reading the configuration file, the function stores the value in the cache for future use.
1 parent 32f3e17 commit 9062c5d

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/commands/config.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { getI18nLocal } from '../i18n';
1111
import * as dotenv from 'dotenv';
1212

1313
dotenv.config();
14+
let configCache:ConfigType | null = null;
1415

1516
export enum CONFIG_KEYS {
1617
OCO_OPENAI_API_KEY = 'OCO_OPENAI_API_KEY',
@@ -134,6 +135,9 @@ export type ConfigType = {
134135
const configPath = pathJoin(homedir(), '.opencommit');
135136

136137
export const getConfig = (): ConfigType | null => {
138+
// If it is enable, use configCache
139+
if (configCache) return configCache;
140+
137141
const configFromEnv = {
138142
OCO_OPENAI_API_KEY: process.env.OCO_OPENAI_API_KEY,
139143
OCO_OPENAI_MAX_TOKENS: process.env.OCO_OPENAI_MAX_TOKENS ? Number(process.env.OCO_OPENAI_MAX_TOKENS) : undefined,
@@ -145,7 +149,10 @@ export const getConfig = (): ConfigType | null => {
145149
};
146150

147151
const configExists = existsSync(configPath);
148-
if (!configExists) return configFromEnv;
152+
if (!configExists) {
153+
configCache = configFromEnv;
154+
return configFromEnv;
155+
}
149156

150157
const configFile = readFileSync(configPath, 'utf8');
151158
const config = iniParse(configFile);
@@ -173,6 +180,8 @@ export const getConfig = (): ConfigType | null => {
173180
}
174181
}
175182

183+
// Store configCache
184+
configCache = config
176185
return config;
177186
};
178187

0 commit comments

Comments
 (0)