MCPcopy Create free account
hub / github.com/evolution-foundation/evolution-api / LocalCache

Class LocalCache

src/cache/localcache.ts:7–100  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5import NodeCache from 'node-cache';
6
7export class LocalCache implements ICache {
8 private readonly logger = new Logger('LocalCache');
9 private conf: CacheConfLocal;
10 static localCache = new NodeCache();
11
12 constructor(
13 private readonly configService: ConfigService,
14 private readonly module: string,
15 ) {
16 this.conf = this.configService.get<CacheConf>('CACHE')?.LOCAL;
17 }
18
19 async get(key: string): Promise<any> {
20 return LocalCache.localCache.get(this.buildKey(key));
21 }
22
23 async set(key: string, value: any, ttl?: number) {
24 return LocalCache.localCache.set(this.buildKey(key), value, ttl || this.conf.TTL);
25 }
26
27 async has(key: string) {
28 return LocalCache.localCache.has(this.buildKey(key));
29 }
30
31 async delete(key: string) {
32 return LocalCache.localCache.del(this.buildKey(key));
33 }
34
35 async deleteAll(appendCriteria?: string) {
36 const keys = await this.keys(appendCriteria);
37 if (!keys?.length) {
38 return 0;
39 }
40
41 return LocalCache.localCache.del(keys);
42 }
43
44 async keys(appendCriteria?: string) {
45 const filter = `${this.buildKey('')}${appendCriteria ? `${appendCriteria}:` : ''}`;
46
47 return LocalCache.localCache.keys().filter((key) => key.substring(0, filter.length) === filter);
48 }
49
50 buildKey(key: string) {
51 return `${this.module}:${key}`;
52 }
53
54 async hGet(key: string, field: string) {
55 try {
56 const data = LocalCache.localCache.get(this.buildKey(key)) as object;
57
58 if (data && field in data) {
59 return JSON.parse(data[field], BufferJSON.reviver);
60 }
61
62 return null;
63 } catch (error) {
64 this.logger.error(error);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected