fix: Adjusts in redis for save instances

This commit is contained in:
Davidson Gomes
2024-04-18 10:39:24 -03:00
parent 4ed1335f89
commit 61bd5b3484
18 changed files with 178 additions and 208 deletions

View File

@@ -1,3 +1,4 @@
import { BufferJSON } from '@whiskeysockets/baileys';
import { RedisClientType } from 'redis';
import { ICache } from '../api/abstract/abstract.cache';
@@ -14,7 +15,6 @@ export class RedisCache implements ICache {
this.conf = this.configService.get<CacheConf>('CACHE')?.REDIS;
this.client = redisClient.getConnection();
}
async get(key: string): Promise<any> {
try {
return JSON.parse(await this.client.get(this.buildKey(key)));
@@ -23,6 +23,20 @@ export class RedisCache implements ICache {
}
}
async hGet(key: string, field: string) {
try {
const data = await this.client.hGet(this.buildKey(key), field);
if (data) {
return JSON.parse(data, BufferJSON.reviver);
}
return null;
} catch (error) {
this.logger.error(error);
}
}
async set(key: string, value: any, ttl?: number) {
try {
await this.client.setEx(this.buildKey(key), ttl || this.conf?.TTL, JSON.stringify(value));
@@ -31,6 +45,16 @@ export class RedisCache implements ICache {
}
}
async hSet(key: string, field: string, value: any) {
try {
const json = JSON.stringify(value, BufferJSON.replacer);
await this.client.hSet(this.buildKey(key), field, json);
} catch (error) {
this.logger.error(error);
}
}
async has(key: string) {
try {
return (await this.client.exists(this.buildKey(key))) > 0;
@@ -47,6 +71,14 @@ export class RedisCache implements ICache {
}
}
async hDelete(key: string, field: string) {
try {
return await this.client.hDel(this.buildKey(key), field);
} catch (error) {
this.logger.error(error);
}
}
async deleteAll(appendCriteria?: string) {
try {
const keys = await this.keys(appendCriteria);