Files
portalweb-api/src/core/configs/cache/redis-client.adapter.ts
JuruSysadmin 233734fdea fix: corrige erros do ESLint e configura variáveis globais
- Adiciona variáveis globais do Node.js (process, console, __dirname, require, module, exports)
- Adiciona variáveis globais do Jest (describe, it, beforeEach, fail, etc.)
- Configura ESLint para arquivos JavaScript de configuração
- Remove diretivas eslint-disable não utilizadas
- Corrige variáveis não usadas prefixando com _
- Ajusta regras do ESLint para ignorar variáveis que começam com _
- Formata código com Prettier
2025-11-21 17:05:07 -03:00

54 lines
1.3 KiB
TypeScript

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<T>(key: string): Promise<T | null> {
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<T>(key: string, value: T, ttlSeconds = 300): Promise<void> {
await this.redis.set(key, JSON.stringify(value), 'EX', ttlSeconds);
}
async del(key: string): Promise<void>;
async del(...keys: string[]): Promise<void>;
async del(keyOrKeys: string | string[]): Promise<void> {
if (Array.isArray(keyOrKeys)) {
await this.redis.del(...keyOrKeys);
} else {
await this.redis.del(keyOrKeys);
}
}
async keys(pattern: string): Promise<string[]> {
return this.redis.keys(pattern);
}
async ttl(key: string): Promise<number> {
return this.redis.ttl(key);
}
async eval(
script: string,
numKeys: number,
...keysAndArgs: (string | number)[]
): Promise<any> {
return this.redis.eval(script, numKeys, ...keysAndArgs);
}
}