| 3 | import { BufferJSON } from 'baileys'; |
| 4 | |
| 5 | export class CacheService { |
| 6 | private readonly logger = new Logger('CacheService'); |
| 7 | |
| 8 | constructor(private readonly cache: ICache) { |
| 9 | if (cache) { |
| 10 | this.logger.verbose(`cacheservice created using cache engine: ${cache.constructor?.name}`); |
| 11 | } else { |
| 12 | this.logger.verbose(`cacheservice disabled`); |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | async get(key: string): Promise<any> { |
| 17 | if (!this.cache) { |
| 18 | return; |
| 19 | } |
| 20 | return this.cache.get(key); |
| 21 | } |
| 22 | |
| 23 | public async hGet(key: string, field: string) { |
| 24 | if (!this.cache) { |
| 25 | return null; |
| 26 | } |
| 27 | try { |
| 28 | const data = await this.cache.hGet(key, field); |
| 29 | |
| 30 | if (data) { |
| 31 | return JSON.parse(data, BufferJSON.reviver); |
| 32 | } |
| 33 | |
| 34 | return null; |
| 35 | } catch (error) { |
| 36 | this.logger.error(error); |
| 37 | return null; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | async set(key: string, value: any, ttl?: number) { |
| 42 | if (!this.cache) { |
| 43 | return; |
| 44 | } |
| 45 | this.cache.set(key, value, ttl); |
| 46 | } |
| 47 | |
| 48 | public async hSet(key: string, field: string, value: any) { |
| 49 | if (!this.cache) { |
| 50 | return; |
| 51 | } |
| 52 | try { |
| 53 | const json = JSON.stringify(value, BufferJSON.replacer); |
| 54 | |
| 55 | await this.cache.hSet(key, field, json); |
| 56 | } catch (error) { |
| 57 | this.logger.error(error); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | async has(key: string) { |
| 62 | if (!this.cache) { |
nothing calls this directly
no outgoing calls
no test coverage detected