Skip to content

Commit

Permalink
✨ feat(config.ts): add caching mechanism to reduce file I/O operations
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
takuya-o committed May 6, 2023
1 parent ad70a90 commit 92d892f
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/commands/config.ts
Original file line number Diff line number Diff line change
@@ -8,6 +8,8 @@ import chalk from 'chalk';
import { COMMANDS } from '../CommandsEnum';
import { getI18nLocal } from '../i18n';

let configCache:ConfigType | null = null

export enum CONFIG_KEYS {
OPENAI_API_KEY = 'OPENAI_API_KEY',
OPENAI_MAX_TOKENS = 'OPENAI_MAX_TOKENS',
@@ -119,6 +121,9 @@ export type ConfigType = {
const configPath = pathJoin(homedir(), '.opencommit');

export const getConfig = (): ConfigType | null => {
// If it is enable, use configCache
if (configCache) return configCache

const configExists = existsSync(configPath);
if (!configExists) return null;

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

// Store configCache
configCache = config
return config;
};

0 comments on commit 92d892f

Please sign in to comment.