chore(backend): Laat productie zonder caching toe
This commit is contained in:
parent
15a6a7524f
commit
d15d91a144
3 changed files with 10 additions and 6 deletions
|
@ -15,6 +15,10 @@ async function initializeClient(): Promise<CacheClient> {
|
|||
const cachingPort = getEnvVar(envVars.CachePort);
|
||||
const cachingUrl = `${cachingHost}:${cachingPort}`;
|
||||
|
||||
if (cachingHost === '') {
|
||||
return cacheClient;
|
||||
}
|
||||
|
||||
cacheClient = Client.create(cachingUrl);
|
||||
|
||||
getLogger().info(`Memcached client initialized at ${cachingUrl}`);
|
||||
|
|
|
@ -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<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);
|
||||
}
|
||||
|
||||
|
@ -41,9 +42,8 @@ async function fetchWithCache<T>(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<T>(url: string, description: string, options?: Opt
|
|||
const response = await fetchWithLogging<T>(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;
|
||||
|
|
|
@ -41,7 +41,7 @@ export const envVars: Record<string, EnvVar> = {
|
|||
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) || '' : '';
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue