25 lines
661 B
TypeScript
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);
|
|
}
|
|
}
|