import { RedisService } from "@liaoliaots/nestjs-redis"; import { Injectable } from "@nestjs/common"; import Redis from "ioredis"; @Injectable() export class RedisClient { private readonly redis: Redis | null; constructor(private readonly redisService: RedisService) { this.redis = this.redisService.getOrThrow(); } async set( key: string, value: any, expireInSeconds?: number ): Promise<"OK" | null> { if (!this.redis) { return null; } if (expireInSeconds) { return await this.redis.set( key, JSON.stringify(value), "EX", expireInSeconds ); } return await this.redis.set(key, JSON.stringify(value)); } async get(key: string): Promise { if (!this.redis) { return null; } return JSON.parse(await this.redis.get(key)); } async del(key: string): Promise { if (!this.redis) { return null; } return await this.redis.del(key); } getClient(): Redis | null { return this.redis; } }