-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Expand file tree
/
Copy pathcache.service.ts
More file actions
101 lines (87 loc) · 2.06 KB
/
cache.service.ts
File metadata and controls
101 lines (87 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { ICache } from '@api/abstract/abstract.cache';
import { Logger } from '@config/logger.config';
import { BufferJSON } from 'baileys';
export class CacheService {
private readonly logger = new Logger('CacheService');
constructor(private readonly cache: ICache) {
if (cache) {
this.logger.verbose(`cacheservice created using cache engine: ${cache.constructor?.name}`);
} else {
this.logger.verbose(`cacheservice disabled`);
}
}
async get(key: string): Promise<any> {
if (!this.cache) {
return;
}
return this.cache.get(key);
}
public async hGet(key: string, field: string) {
if (!this.cache) {
return null;
}
try {
const data = await this.cache.hGet(key, field);
if (data) {
return JSON.parse(data, BufferJSON.reviver);
}
return null;
} catch (error) {
this.logger.error(error);
return null;
}
}
async set(key: string, value: any, ttl?: number) {
if (!this.cache) {
return;
}
this.cache.set(key, value, ttl);
}
public async hSet(key: string, field: string, value: any) {
if (!this.cache) {
return;
}
try {
const json = JSON.stringify(value, BufferJSON.replacer);
await this.cache.hSet(key, field, json);
} catch (error) {
this.logger.error(error);
}
}
async has(key: string) {
if (!this.cache) {
return;
}
return this.cache.has(key);
}
async delete(key: string) {
if (!this.cache) {
return;
}
return this.cache.delete(key);
}
async hDelete(key: string, field: string) {
if (!this.cache) {
return false;
}
try {
await this.cache.hDelete(key, field);
return true;
} catch (error) {
this.logger.error(error);
return false;
}
}
async deleteAll(appendCriteria?: string) {
if (!this.cache) {
return;
}
return this.cache.deleteAll(appendCriteria);
}
async keys(appendCriteria?: string) {
if (!this.cache) {
return;
}
return this.cache.keys(appendCriteria);
}
}