import { Injectable, OnModuleDestroy } from '@nestjs/common'; import Redis from 'ioredis'; @Injectable() export class RedisService implements OnModuleDestroy { private readonly client: Redis; constructor() { this.client = new Redis({ host: process.env.REDIS_HOST || '127.0.0.1', port: Number(process.env.REDIS_PORT || 6379), password: process.env.REDIS_PASSWORD || undefined, db: Number(process.env.REDIS_DB || 0), keyPrefix: '', lazyConnect: false, maxRetriesPerRequest: 2, }); } getClient() { return this.client; } async get(key: string) { return this.client.get(key); } async setex(key: string, seconds: number, value: string) { await this.client.set(key, value, 'EX', seconds); } async del(key: string) { await this.client.del(key); } async onModuleDestroy() { await this.client.quit(); } }