|
1 | 1 | import { ICache } from '@api/abstract/abstract.cache'; |
2 | 2 | import { CacheConf, CacheConfLocal, ConfigService } from '@config/env.config'; |
3 | 3 | import NodeCache from 'node-cache'; |
| 4 | +import { BufferJSON } from 'baileys'; |
| 5 | +import { Logger } from '@config/logger.config'; |
4 | 6 |
|
5 | 7 | export class LocalCache implements ICache { |
| 8 | + private readonly logger = new Logger('LocalCache'); |
6 | 9 | private conf: CacheConfLocal; |
7 | 10 | static localCache = new NodeCache(); |
8 | 11 |
|
@@ -45,14 +48,36 @@ export class LocalCache implements ICache { |
45 | 48 | return `${this.module}:${key}`; |
46 | 49 | } |
47 | 50 |
|
48 | | - async hGet() { |
49 | | - console.log('hGet not implemented'); |
50 | | - return null |
| 51 | + async hGet(key: string, field: string) { |
| 52 | + try { |
| 53 | + const data = LocalCache.localCache.get(this.buildKey(key)) as Object; |
| 54 | + |
| 55 | + if (data && field in data) { |
| 56 | + return JSON.parse(data[field], BufferJSON.reviver); |
| 57 | + } |
| 58 | + |
| 59 | + return null; |
| 60 | + } catch (error) { |
| 61 | + this.logger.error(error); |
| 62 | + } |
51 | 63 | } |
52 | 64 |
|
53 | | - async hSet() { |
54 | | - console.log('hSet not implemented'); |
55 | | - return null |
| 65 | + async hSet(key: string, field: string, value: any) { |
| 66 | + try { |
| 67 | + const json = JSON.stringify(value, BufferJSON.replacer); |
| 68 | + |
| 69 | + let hash = LocalCache.localCache.get(this.buildKey(key)); |
| 70 | + |
| 71 | + if (!hash) { |
| 72 | + hash = {}; |
| 73 | + } |
| 74 | + |
| 75 | + hash[field] = json; |
| 76 | + LocalCache.localCache.set(key, hash); |
| 77 | + |
| 78 | + } catch (error) { |
| 79 | + this.logger.error(error); |
| 80 | + } |
56 | 81 | } |
57 | 82 |
|
58 | 83 | async hDelete() { |
|
0 commit comments