import { Inject, Injectable } from '@nestjs/common'; import { createClient } from 'redis'; import { REDIS_CONFIG } from './constants'; import { RedisModuleOptions } from './redis.module'; @Injectable() export class RedisService { private client?: ReturnType; constructor( @Inject(REDIS_CONFIG) private readonly conf: RedisModuleOptions, ) { // } async connect(opts?: RedisModuleOptions) { if (this.client && !opts) { return this.client; } const client = (opts && createClient(opts)) || createClient(this.conf); if (this.client) { await this.disconnect(); } this.client = client; await this.client.connect(); return this.client; } async disconnect() { if (!this.client) return; await this.client.quit(); this.client = undefined; } }