import Redis from 'ioredis'; import config from '../../config'; import Logger from '@lib/Logger/Logger'; class _RedisConnection { private pool?: Redis private logger = new Logger('Redis') constructor() { if (!config.redis.enable) { this.logger.info('Database is disabled, initialization terminated'); return; } try { this.pool = new Redis({ port: config.redis.port, host: config.redis.host, password: config.redis.password, maxRetriesPerRequest: 10, }); this.logger.info('Database connection pool created') } catch (error) { this.logger.error('Failed to create database connection pool: ' + error) } setTimeout(async () => { if (this.pool == undefined) return; try { let res = await this.pool.set('redis_test', '1'); if (res) this.logger.info('Database test successful') else throw new Error('Unexpected return value') } catch (error) { this.logger.error('Database test failed: ' + error) } }, 10); } public getPool(): Redis { return this.pool; } } const _redisConnection = new _RedisConnection(); const RedisConnection = _redisConnection.getPool(); export default RedisConnection; export type { Redis };