import { Inject, Injectable } from '@nestjs/common'; import { Redis } from 'ioredis'; import { IRedisClient } from './IRedisClient'; @Injectable() export class RedisClientAdapter implements IRedisClient { constructor( @Inject('REDIS_CLIENT') private readonly redis: Redis, ) {} async get(key: string): Promise { const data = await this.redis.get(key); if (!data) return null; try { return JSON.parse(data); } catch (_error) { // If it's not valid JSON, return the raw string value return data as T; } } async set(key: string, value: T, ttlSeconds = 300): Promise { await this.redis.set(key, JSON.stringify(value), 'EX', ttlSeconds); } async del(key: string): Promise; async del(...keys: string[]): Promise; async del(keyOrKeys: string | string[]): Promise { if (Array.isArray(keyOrKeys)) { await this.redis.del(...keyOrKeys); } else { await this.redis.del(keyOrKeys); } } async keys(pattern: string): Promise { return this.redis.keys(pattern); } async ttl(key: string): Promise { return this.redis.ttl(key); } async eval( script: string, numKeys: number, ...keysAndArgs: (string | number)[] ): Promise { return this.redis.eval(script, numKeys, ...keysAndArgs); } }