forked from jaredwray/cacheable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.ts
99 lines (89 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
import { LRUCache } from 'lru-cache';
import cloneDeep from 'lodash.clonedeep';
import { Config, Cache, Store } from '../caching';
function clone<T>(object: T): T {
if (typeof object === 'object' && object !== null) {
return cloneDeep(object);
}
return object;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type LRU = LRUCache<string, any, unknown>;
// eslint-disable-next-line @typescript-eslint/no-explicit-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 & {
get size(): number;
dump: LRU['dump'];
load: LRU['load'];
calculatedSize: LRU['calculatedSize'];
};
export type MemoryCache = Cache<MemoryStore>;
/**
* Wrapper for lru-cache.
*/
export function memoryStore(args?: MemoryConfig): MemoryStore {
const shouldCloneBeforeSet = args?.shouldCloneBeforeSet !== false; // clone by default
const isCacheable = args?.isCacheable ?? ((val) => val !== undefined);
const lruOpts = {
ttlAutopurge: true,
...args,
max: args?.max || 500,
ttl: args?.ttl !== undefined ? args.ttl : 0,
};
const lruCache = new LRUCache(lruOpts);
return {
async del(key) {
lruCache.delete(key);
},
get: async <T>(key: string) => lruCache.get(key) as T,
keys: async () => [...lruCache.keys()],
mget: async (...args) => args.map((x) => lruCache.get(x)),
async mset(args, ttl?) {
const opt = { ttl: ttl !== undefined ? ttl : lruOpts.ttl } as const;
for (const [key, value] of args) {
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(...args) {
for (const key of args) 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 !== undefined ? opt : lruOpts.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: (...args: Parameters<LRU['load']>) => lruCache.load(...args),
};
}