完成 MysqlConnection、RedisConnection
This commit is contained in:
43
src/lib/Database/RedisConnection.ts
Normal file
43
src/lib/Database/RedisConnection.ts
Normal 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();
|
||||
Reference in New Issue
Block a user