-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcache.ts
More file actions
41 lines (34 loc) · 1.06 KB
/
cache.ts
File metadata and controls
41 lines (34 loc) · 1.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
export interface CacheProvider {
get<T = any>(key: string): Promise<T | undefined>;
getTimestamp(key: string): Promise<number>;
set<T = any>(key: string, result: T): Promise<boolean>;
has(key: string): Promise<boolean>;
remove(key: string): Promise<boolean>;
}
interface MemoryCacheItem {
timestamp: number;
value: any;
}
export class MemoryCacheProvider implements CacheProvider {
private cache: { [key: string]: MemoryCacheItem } = {};
async get<T = object>(key: string): Promise<T | undefined> {
return this.cache[key].value;
}
async getTimestamp(key: string): Promise<number> {
return this.cache[key].timestamp;
}
async set(key: string, result: any): Promise<boolean> {
this.cache[key] = {
timestamp: new Date().getTime(),
value: result
};
return true;
}
async has(key: string): Promise<boolean> {
return !!this.cache[key];
}
async remove(key: string): Promise<boolean> {
delete this.cache[key];
return true;
}
}