mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-12 04:18:54 +00:00
50 lines
1.1 KiB
TypeScript
50 lines
1.1 KiB
TypeScript
import {
|
|
Injectable,
|
|
Logger,
|
|
OnModuleDestroy,
|
|
OnModuleInit,
|
|
} from '@nestjs/common';
|
|
import { Redis as IORedis, RedisOptions } from 'ioredis';
|
|
|
|
import { Config } from '../../fundamentals/config';
|
|
|
|
class Redis extends IORedis implements OnModuleDestroy, OnModuleInit {
|
|
logger = new Logger(Redis.name);
|
|
constructor(opts: RedisOptions) {
|
|
super({
|
|
...opts,
|
|
lazyConnect: true,
|
|
});
|
|
}
|
|
|
|
async onModuleInit() {
|
|
await this.connect().catch(() => {
|
|
this.logger.error('Failed to connect to Redis server.');
|
|
});
|
|
}
|
|
onModuleDestroy() {
|
|
this.disconnect();
|
|
}
|
|
}
|
|
|
|
@Injectable()
|
|
export class CacheRedis extends Redis {
|
|
constructor(config: Config) {
|
|
super(config.plugins.redis ?? {});
|
|
}
|
|
}
|
|
|
|
@Injectable()
|
|
export class SessionRedis extends Redis {
|
|
constructor(config: Config) {
|
|
super({ ...config.plugins.redis, db: (config.plugins.redis?.db ?? 0) + 2 });
|
|
}
|
|
}
|
|
|
|
@Injectable()
|
|
export class SocketIoRedis extends Redis {
|
|
constructor(config: Config) {
|
|
super({ ...config.plugins.redis, db: (config.plugins.redis?.db ?? 0) + 3 });
|
|
}
|
|
}
|