/
memory.ts
113 lines (100 loc) · 2.88 KB
/
memory.ts
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
102
103
104
105
106
107
108
109
110
111
112
113
import {LRUCache} from 'lru-cache';
import cloneDeep from 'lodash.clonedeep';
import {type Config, type Cache, type Store} from '../caching.js';
function clone<T>(object: T): T {
if (typeof object === 'object' && object !== null) {
return cloneDeep(object);
}
return object;
}
// eslint-disable-next-line @typescript-eslint/naming-convention
type LRU = LRUCache<string, any>;
type Pre = LRUCache.OptionsTTLLimit<string, any, unknown>;
type Options = Omit<Pre, 'ttlAutopurge'> & Partial<Pick<Pre, 'ttlAutopurge'>>;
export type MemoryConfig = {
max?: number;
sizeCalculation?: (value: unknown, key: string) => number;
shouldCloneBeforeSet?: boolean;
} & Options &
Config;
export type MemoryStore = Store & {
dump: LRU['dump'];
load: LRU['load'];
calculatedSize: LRU['calculatedSize'];
get size(): number;
};
export type MemoryCache = Cache<MemoryStore>;
/**
* Wrapper for lru-cache.
*/
export function memoryStore(arguments_?: MemoryConfig): MemoryStore {
const shouldCloneBeforeSet = arguments_?.shouldCloneBeforeSet !== false; // Clone by default
const isCacheable = arguments_?.isCacheable ?? (value => value !== undefined);
const lruOptions = {
ttlAutopurge: true,
...arguments_,
max: arguments_?.max ?? 500,
ttl: arguments_?.ttl === undefined ? 0 : arguments_.ttl,
};
const lruCache = new LRUCache(lruOptions);
return {
async del(key) {
lruCache.delete(key);
},
get: async <T>(key: string) => lruCache.get(key) as T,
keys: async () => [...lruCache.keys()],
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
mget: async (...arguments_) => arguments_.map(x => lruCache.get(x)),
async mset(arguments_, ttl?) {
const opt = {ttl: ttl ?? lruOptions.ttl} as const;
for (const [key, value] of arguments_) {
if (!isCacheable(value)) {
throw new Error(`no cacheable value ${JSON.stringify(value)}`);
}
if (shouldCloneBeforeSet) {
lruCache.set(key, clone(value), opt);
} else {
lruCache.set(key, value, opt);
}
}
},
async mdel(...arguments_) {
for (const key of arguments_) {
lruCache.delete(key);
}
},
async reset() {
lruCache.clear();
},
ttl: async key => lruCache.getRemainingTTL(key),
async set(key, value, opt) {
if (!isCacheable(value)) {
throw new Error(`no cacheable value ${JSON.stringify(value)}`);
}
if (shouldCloneBeforeSet) {
value = clone(value);
}
const ttl = opt ?? lruOptions.ttl;
lruCache.set(key, value, {ttl});
},
get calculatedSize() {
return lruCache.calculatedSize;
},
/**
* This method is not available in the caching modules.
*/
get size() {
return lruCache.size;
},
/**
* This method is not available in the caching modules.
*/
dump: () => lruCache.dump(),
/**
* This method is not available in the caching modules.
*/
load(...arguments_: Parameters<LRU['load']>) {
lruCache.load(...arguments_);
},
};
}