chore(backend): Caching envvars
This commit is contained in:
parent
a59417b8f5
commit
207df530b9
5 changed files with 66 additions and 1 deletions
42
backend/src/caching.ts
Normal file
42
backend/src/caching.ts
Normal file
|
@ -0,0 +1,42 @@
|
|||
import { createClient, RedisClientType } from 'redis';
|
||||
import { getLogger } from './logging/initalize.js';
|
||||
import { envVars, getEnvVar } from './util/envVars.js';
|
||||
|
||||
export type CacheClient = RedisClientType;
|
||||
|
||||
let redisClient: CacheClient;
|
||||
|
||||
async function initializeClient(): Promise<CacheClient> {
|
||||
if (redisClient !== undefined) {
|
||||
return redisClient;
|
||||
}
|
||||
|
||||
const redisHost = getEnvVar(envVars.CacheHost);
|
||||
const redisPort = getEnvVar(envVars.CachePort);
|
||||
const redisUrl = `redis://${redisHost}:${redisPort}`;
|
||||
|
||||
redisClient = createClient({
|
||||
url: redisUrl
|
||||
});
|
||||
|
||||
redisClient.on('error', (err) => getLogger().error('Redis error:', err));
|
||||
await redisClient.connect();
|
||||
|
||||
return redisClient;
|
||||
}
|
||||
|
||||
export async function getCacheClient(): Promise<CacheClient> {
|
||||
redisClient ||= await initializeClient();
|
||||
return redisClient;
|
||||
}
|
||||
|
||||
export async function checkRedisHealth(): Promise<boolean> {
|
||||
try {
|
||||
await redisClient.set('health', 'ok');
|
||||
const reply = await redisClient.get('health');
|
||||
return reply === 'ok';
|
||||
} catch (error) {
|
||||
getLogger().error('Redis Health Check Failed:', error);
|
||||
return false;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue