import { IDelOptions, ISetOptions } from '../interfaces/redis.interface'; export class RedisService { private readonly client; // eslint-disable-next-line @typescript-eslint/no-empty-function constructor(client: any) { this.client = client; } public async set( key: string, value: any, options?: ISetOptions, ): Promise { try { const newList = typeof value === 'string' ? value : JSON.stringify(value); const expire = options?.isExpire ? options.expire : 0; await this.client.setEx(key, expire, newList); if (options?.ListKey) { const listKey = options.ListKey; const redisParams = []; if (Array.isArray(listKey)) { const multiList = this.client.mget(listKey); for (let i = 0; i < multiList.length; i++) { redisParams.push(listKey[i]); const data = multiList[i] ?? []; data.push(key); redisParams.push(data); } } else { let data = await this.client.get(listKey); redisParams.push(listKey); if (data) { data.push(key); } else { data = [key]; } redisParams.push(data); } await this.client.mset(redisParams); } } catch (error) { console.error('RedisService.set() error', error); throw error; } } public async get(key: string): Promise { try { return await this.client.get(key); } catch (error) { console.error('RedisService.get() error', error); throw error; } } public async deleteCacheList( key: string, options?: IDelOptions, ): Promise { try { await this.client.del(key); if (options?.ListKey) { const listKey = options.ListKey; await this.client.del(listKey); } } catch (error) { console.error('RedisService.deleteCacheList() error', error); throw error; } } }