chore(backend): Laat productie zonder caching toe

This commit is contained in:
Tibo De Peuter 2025-05-16 23:21:33 +02:00
parent 15a6a7524f
commit d15d91a144
Signed by: tdpeuter
GPG key ID: 38297DE43F75FFE2
3 changed files with 10 additions and 6 deletions

View file

@ -15,6 +15,10 @@ async function initializeClient(): Promise<CacheClient> {
const cachingPort = getEnvVar(envVars.CachePort); const cachingPort = getEnvVar(envVars.CachePort);
const cachingUrl = `${cachingHost}:${cachingPort}`; const cachingUrl = `${cachingHost}:${cachingPort}`;
if (cachingHost === '') {
return cacheClient;
}
cacheClient = Client.create(cachingUrl); cacheClient = Client.create(cachingUrl);
getLogger().info(`Memcached client initialized at ${cachingUrl}`); getLogger().info(`Memcached client initialized at ${cachingUrl}`);

View file

@ -5,6 +5,7 @@ import { getCacheClient } from '../caching.js';
import { envVars, getEnvVar, getNumericEnvVar } from './envVars.js'; import { envVars, getEnvVar, getNumericEnvVar } from './envVars.js';
import { createHash } from 'crypto'; import { createHash } from 'crypto';
const cacheClient = await getCacheClient();
const logger: Logger = getLogger(); const logger: Logger = getLogger();
const runMode: string = getEnvVar(envVars.RunMode); const runMode: string = getEnvVar(envVars.RunMode);
const prefix: string = getEnvVar(envVars.CacheKeyPrefix); const prefix: string = getEnvVar(envVars.CacheKeyPrefix);
@ -27,7 +28,7 @@ interface Options {
* @returns The response data if successful, or null if an error occurs. * @returns The response data if successful, or null if an error occurs.
*/ */
export async function fetchRemote<T>(url: string, description: string, options?: Options, cacheTTL?: number): Promise<T | null> { export async function fetchRemote<T>(url: string, description: string, options?: Options, cacheTTL?: number): Promise<T | null> {
if (runMode !== 'dev' && !runMode.includes('test')) { if (runMode !== 'dev' && !runMode.includes('test') && cacheClient !== undefined) {
return fetchWithCache<T>(url, description, options, cacheTTL); return fetchWithCache<T>(url, description, options, cacheTTL);
} }
@ -41,9 +42,8 @@ async function fetchWithCache<T>(url: string, description: string, options?: Opt
const urlWithParams = `${url}${options?.params ? JSON.stringify(options.params) : ''}`; const urlWithParams = `${url}${options?.params ? JSON.stringify(options.params) : ''}`;
const hashedUrl = createHash('sha256').update(urlWithParams).digest('hex'); const hashedUrl = createHash('sha256').update(urlWithParams).digest('hex');
const key = `${prefix}:${hashedUrl}`; const key = `${prefix}:${hashedUrl}`;
const client = await getCacheClient();
const cachedData = await client.get(key); const cachedData = await cacheClient.get(key);
if (cachedData?.value) { if (cachedData?.value) {
logger.debug(`✅ INFO: Cache hit for ${description} at "${url}" (key: "${key}")`); logger.debug(`✅ INFO: Cache hit for ${description} at "${url}" (key: "${key}")`);
@ -54,7 +54,7 @@ async function fetchWithCache<T>(url: string, description: string, options?: Opt
const response = await fetchWithLogging<T>(url, description, options); const response = await fetchWithLogging<T>(url, description, options);
const ttl = cacheTTL || getNumericEnvVar(envVars.CacheTTL); 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}")`); logger.debug(`✅ INFO: Cached response for ${description} at "${url}" for ${ttl} seconds. (key: "${key}")`);
return response; return response;

View file

@ -41,7 +41,7 @@ export const envVars: Record<string, EnvVar> = {
LogLevel: { key: LOGGING_PREFIX + 'LEVEL', defaultValue: 'info' }, LogLevel: { key: LOGGING_PREFIX + 'LEVEL', defaultValue: 'info' },
LokiHost: { key: LOGGING_PREFIX + 'LOKI_HOST', defaultValue: 'http://localhost:3102' }, 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 }, CachePort: { key: CACHE_PREFIX + 'PORT', defaultValue: 11211 },
CacheTTL: { key: CACHE_PREFIX + 'TTL', defaultValue: 60 * 60 * 24 }, // 24 hours CacheTTL: { key: CACHE_PREFIX + 'TTL', defaultValue: 60 * 60 * 24 }, // 24 hours
CacheKeyPrefix: { key: CACHE_PREFIX + 'KEY_PREFIX', defaultValue: 'dwengo' }, CacheKeyPrefix: { key: CACHE_PREFIX + 'KEY_PREFIX', defaultValue: 'dwengo' },
@ -62,7 +62,7 @@ export function getEnvVar(envVar: EnvVar): string {
} else if (envVar.required) { } else if (envVar.required) {
throw new Error(`Missing environment variable: ${envVar.key}`); throw new Error(`Missing environment variable: ${envVar.key}`);
} else { } else {
return String(envVar.defaultValue) || ''; return (envVar.defaultValue !== undefined) ? String(envVar.defaultValue) || '' : '';
} }
} }