42 lines
896 B
TypeScript
42 lines
896 B
TypeScript
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
|
/* eslint-disable @typescript-eslint/no-unsafe-call */
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
import { REDIS } from '../constants/redis.constants';
|
|
import Redis from 'ioredis';
|
|
|
|
@Injectable()
|
|
export class RedisService {
|
|
constructor(
|
|
/**
|
|
* Inject Redis
|
|
*/
|
|
@Inject(REDIS)
|
|
private readonly redis: Redis,
|
|
) {}
|
|
|
|
get client() {
|
|
return this.redis;
|
|
}
|
|
|
|
async set(key: string, val: string, ttl?: number) {
|
|
if (ttl) {
|
|
await this.redis.set(key, val, 'EX', ttl);
|
|
} else {
|
|
await this.redis.set(key, val);
|
|
}
|
|
}
|
|
|
|
async get(key: string) {
|
|
return await this.redis.get(key);
|
|
}
|
|
|
|
async del(key: string) {
|
|
return await this.redis.del(key);
|
|
}
|
|
|
|
async onModuleDestroy() {
|
|
await this.redis.quit();
|
|
}
|
|
}
|