mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-22 20:12:02 -06:00
add: hGet and hSet on LocalCache
This commit is contained in:
parent
0b40583fa8
commit
9a6d4a8e44
37
src/cache/localcache.ts
vendored
37
src/cache/localcache.ts
vendored
@ -1,8 +1,11 @@
|
||||
import { ICache } from '@api/abstract/abstract.cache';
|
||||
import { CacheConf, CacheConfLocal, ConfigService } from '@config/env.config';
|
||||
import NodeCache from 'node-cache';
|
||||
import { BufferJSON } from 'baileys';
|
||||
import { Logger } from '@config/logger.config';
|
||||
|
||||
export class LocalCache implements ICache {
|
||||
private readonly logger = new Logger('LocalCache');
|
||||
private conf: CacheConfLocal;
|
||||
static localCache = new NodeCache();
|
||||
|
||||
@ -45,14 +48,36 @@ export class LocalCache implements ICache {
|
||||
return `${this.module}:${key}`;
|
||||
}
|
||||
|
||||
async hGet() {
|
||||
console.log('hGet not implemented');
|
||||
return null
|
||||
async hGet(key: string, field: string) {
|
||||
try {
|
||||
const data = LocalCache.localCache.get(this.buildKey(key)) as Object;
|
||||
|
||||
if (data && field in data) {
|
||||
return JSON.parse(data[field], BufferJSON.reviver);
|
||||
}
|
||||
|
||||
return null;
|
||||
} catch (error) {
|
||||
this.logger.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
async hSet() {
|
||||
console.log('hSet not implemented');
|
||||
return null
|
||||
async hSet(key: string, field: string, value: any) {
|
||||
try {
|
||||
const json = JSON.stringify(value, BufferJSON.replacer);
|
||||
|
||||
let hash = LocalCache.localCache.get(this.buildKey(key));
|
||||
|
||||
if (!hash) {
|
||||
hash = {};
|
||||
}
|
||||
|
||||
hash[field] = json;
|
||||
LocalCache.localCache.set(key, hash);
|
||||
|
||||
} catch (error) {
|
||||
this.logger.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
async hDelete() {
|
||||
|
Loading…
Reference in New Issue
Block a user