Skip to content

Commit 9a6d4a8

Browse files
committed
add: hGet and hSet on LocalCache
1 parent 0b40583 commit 9a6d4a8

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

src/cache/localcache.ts

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { ICache } from '@api/abstract/abstract.cache';
22
import { CacheConf, CacheConfLocal, ConfigService } from '@config/env.config';
33
import NodeCache from 'node-cache';
4+
import { BufferJSON } from 'baileys';
5+
import { Logger } from '@config/logger.config';
46

57
export class LocalCache implements ICache {
8+
private readonly logger = new Logger('LocalCache');
69
private conf: CacheConfLocal;
710
static localCache = new NodeCache();
811

@@ -45,14 +48,36 @@ export class LocalCache implements ICache {
4548
return `${this.module}:${key}`;
4649
}
4750

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+
}
5163
}
5264

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+
}
5681
}
5782

5883
async hDelete() {

0 commit comments

Comments
 (0)