完成 MysqlConnection、RedisConnection

This commit is contained in:
tone
2024-09-25 02:02:12 +08:00
parent 96d212a973
commit 64ab40e566
5 changed files with 147 additions and 1 deletions

View File

@@ -0,0 +1,43 @@
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() {
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 <Redis>this.pool;
}
}
const redisConnection = new RedisConnection();
export default redisConnection.getPool();