Skip to content

Commit 92d892f

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 ad70a90 commit 92d892f

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/commands/config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import chalk from 'chalk';
88
import { COMMANDS } from '../CommandsEnum';
99
import { getI18nLocal } from '../i18n';
1010

11+
let configCache:ConfigType | null = null
12+
1113
export enum CONFIG_KEYS {
1214
OPENAI_API_KEY = 'OPENAI_API_KEY',
1315
OPENAI_MAX_TOKENS = 'OPENAI_MAX_TOKENS',
@@ -119,6 +121,9 @@ export type ConfigType = {
119121
const configPath = pathJoin(homedir(), '.opencommit');
120122

121123
export const getConfig = (): ConfigType | null => {
124+
// If it is enable, use configCache
125+
if (configCache) return configCache
126+
122127
const configExists = existsSync(configPath);
123128
if (!configExists) return null;
124129

@@ -133,6 +138,8 @@ export const getConfig = (): ConfigType | null => {
133138
config[configKey] = validValue;
134139
}
135140

141+
// Store configCache
142+
configCache = config
136143
return config;
137144
};
138145

0 commit comments

Comments
 (0)