Team Ai
Apppublic

oex2003/evolution-api

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
rediscache.ts119 linesDownload Raw Back to cache
1import { ICache } from '@api/abstract/abstract.cache';2import { CacheConf, CacheConfRedis, ConfigService } from '@config/env.config';3import { Logger } from '@config/logger.config';4import { BufferJSON } from 'baileys';5import { RedisClientType } from 'redis';6 7import { redisClient } from './rediscache.client';8 9export class RedisCache implements ICache {10  private readonly logger = new Logger('RedisCache');11  private client: RedisClientType;12  private conf: CacheConfRedis;13 14  constructor(15    private readonly configService: ConfigService,16    private readonly module: string,17  ) {18    this.conf = this.configService.get<CacheConf>('CACHE')?.REDIS;19    this.client = redisClient.getConnection();20  }21  async get(key: string): Promise<any> {22    try {23      return JSON.parse(await this.client.get(this.buildKey(key)));24    } catch (error) {25      this.logger.error(error);26    }27  }28 29  async hGet(key: string, field: string) {30    try {31      const data = await this.client.hGet(this.buildKey(key), field);32 33      if (data) {34        return JSON.parse(data, BufferJSON.reviver);35      }36 37      return null;38    } catch (error) {39      this.logger.error(error);40    }41  }42 43  async set(key: string, value: any, ttl?: number) {44    try {45      await this.client.setEx(this.buildKey(key), ttl || this.conf?.TTL, JSON.stringify(value));46    } catch (error) {47      this.logger.error(error);48    }49  }50 51  async hSet(key: string, field: string, value: any) {52    try {53      const json = JSON.stringify(value, BufferJSON.replacer);54 55      await this.client.hSet(this.buildKey(key), field, json);56    } catch (error) {57      this.logger.error(error);58    }59  }60 61  async has(key: string) {62    try {63      return (await this.client.exists(this.buildKey(key))) > 0;64    } catch (error) {65      this.logger.error(error);66    }67  }68 69  async delete(key: string) {70    try {71      return await this.client.del(this.buildKey(key));72    } catch (error) {73      this.logger.error(error);74    }75  }76 77  async hDelete(key: string, field: string) {78    try {79      return await this.client.hDel(this.buildKey(key), field);80    } catch (error) {81      this.logger.error(error);82    }83  }84 85  async deleteAll(appendCriteria?: string) {86    try {87      const keys = await this.keys(appendCriteria);88      if (!keys?.length) {89        return 0;90      }91 92      return await this.client.del(keys);93    } catch (error) {94      this.logger.error(error);95    }96  }97 98  async keys(appendCriteria?: string) {99    try {100      const match = `${this.buildKey('')}${appendCriteria ? `${appendCriteria}:` : ''}*`;101      const keys = [];102      for await (const key of this.client.scanIterator({103        MATCH: match,104        COUNT: 100,105      })) {106        keys.push(key);107      }108 109      return [...new Set(keys)];110    } catch (error) {111      this.logger.error(error);112    }113  }114 115  buildKey(key: string) {116    return `${this.conf?.PREFIX_KEY}:${this.module}:${key}`;117  }118}119