diff --git a/backend/src/caching.ts b/backend/src/caching.ts index 1349911f..b0591796 100644 --- a/backend/src/caching.ts +++ b/backend/src/caching.ts @@ -15,6 +15,10 @@ async function initializeClient(): Promise { const cachingPort = getEnvVar(envVars.CachePort); const cachingUrl = `${cachingHost}:${cachingPort}`; + if (cachingHost === '') { + return cacheClient; + } + cacheClient = Client.create(cachingUrl); getLogger().info(`Memcached client initialized at ${cachingUrl}`); diff --git a/backend/src/util/api-helper.ts b/backend/src/util/api-helper.ts index d1e87075..e0c42e3a 100644 --- a/backend/src/util/api-helper.ts +++ b/backend/src/util/api-helper.ts @@ -5,6 +5,7 @@ import { getCacheClient } from '../caching.js'; import { envVars, getEnvVar, getNumericEnvVar } from './envVars.js'; import { createHash } from 'crypto'; +const cacheClient = await getCacheClient(); const logger: Logger = getLogger(); const runMode: string = getEnvVar(envVars.RunMode); const prefix: string = getEnvVar(envVars.CacheKeyPrefix); @@ -27,7 +28,7 @@ interface Options { * @returns The response data if successful, or null if an error occurs. */ export async function fetchRemote(url: string, description: string, options?: Options, cacheTTL?: number): Promise { - if (runMode !== 'dev' && !runMode.includes('test')) { + if (runMode !== 'dev' && !runMode.includes('test') && cacheClient !== undefined) { return fetchWithCache(url, description, options, cacheTTL); } @@ -41,9 +42,8 @@ async function fetchWithCache(url: string, description: string, options?: Opt const urlWithParams = `${url}${options?.params ? JSON.stringify(options.params) : ''}`; const hashedUrl = createHash('sha256').update(urlWithParams).digest('hex'); const key = `${prefix}:${hashedUrl}`; - const client = await getCacheClient(); - const cachedData = await client.get(key); + const cachedData = await cacheClient.get(key); if (cachedData?.value) { logger.debug(`✅ INFO: Cache hit for ${description} at "${url}" (key: "${key}")`); @@ -54,7 +54,7 @@ async function fetchWithCache(url: string, description: string, options?: Opt const response = await fetchWithLogging(url, description, options); const ttl = cacheTTL || getNumericEnvVar(envVars.CacheTTL); - await client.set(key, JSON.stringify(response), { expires: ttl }); + await cacheClient.set(key, JSON.stringify(response), { expires: ttl }); logger.debug(`✅ INFO: Cached response for ${description} at "${url}" for ${ttl} seconds. (key: "${key}")`); return response; diff --git a/backend/src/util/envVars.ts b/backend/src/util/envVars.ts index dd936a10..b4d03c68 100644 --- a/backend/src/util/envVars.ts +++ b/backend/src/util/envVars.ts @@ -41,7 +41,7 @@ export const envVars: Record = { LogLevel: { key: LOGGING_PREFIX + 'LEVEL', defaultValue: 'info' }, LokiHost: { key: LOGGING_PREFIX + 'LOKI_HOST', defaultValue: 'http://localhost:3102' }, - CacheHost: { key: CACHE_PREFIX + 'HOST', defaultValue: 'localhost' }, + CacheHost: { key: CACHE_PREFIX + 'HOST' }, CachePort: { key: CACHE_PREFIX + 'PORT', defaultValue: 11211 }, CacheTTL: { key: CACHE_PREFIX + 'TTL', defaultValue: 60 * 60 * 24 }, // 24 hours CacheKeyPrefix: { key: CACHE_PREFIX + 'KEY_PREFIX', defaultValue: 'dwengo' }, @@ -62,7 +62,7 @@ export function getEnvVar(envVar: EnvVar): string { } else if (envVar.required) { throw new Error(`Missing environment variable: ${envVar.key}`); } else { - return String(envVar.defaultValue) || ''; + return (envVar.defaultValue !== undefined) ? String(envVar.defaultValue) || '' : ''; } }