Files
portalweb-api/src/core/configs/cache/redis-client.adapter.ts
2025-03-28 14:42:53 -03:00

25 lines
661 B
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);
return data ? JSON.parse(data) : null;
}
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> {
await this.redis.del(key);
}
}