-
Notifications
You must be signed in to change notification settings - Fork 3
/
cache.js
123 lines (103 loc) · 2.34 KB
/
cache.js
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
114
115
116
117
118
119
120
121
122
123
import { getByteLength} from './util.js'
const voidCache = {
add() {},
get() {},
has() {return false},
del() {},
size() {return 0},
byteLength() {return 0}
}
const hits = new Map()
let cache
export const getCache = (opts) => {
if (cache) {
return cache
} else if (!opts?.ttl) {
cache = voidCache
} else if (typeof opts.get === 'function') { // Custom cache implementation
cache = opts
} else {
cache = createCache(opts)
}
return cache
}
export const stopCache = () => {
clearInterval(getCache().timer)
cache = voidCache
}
export const hasHit = (cache, name) => hits.has(name)
export const hasKey = (name) => getCache().has(name)
export const isNoCache = () => getCache() === voidCache
export const withCache = (name, cb, ttl) => {
if (!hits.has(name)) {
(() => {
let p
hits.set(name, async () => {
if (p) {
return p
}
p = (async () => {
const cache = getCache()
if (await cache.has(name)) {
return cache.get(name)
}
const value = await cb()
await cache.add(name, value, ttl)
p = null
return value
})().finally(() => hits.delete(name))
return p
})
})()
}
return hits.get(name)()
}
export const createCache = ({ttl, evictionTimeout = ttl, warmup, limit = Infinity}) => {
const store = new Map()
let totalByteLength = 0
const timer = setInterval(() => {
const now = Date.now()
store.forEach(({validTill, key}) => {
if (now > validTill) {
cache.del(key)
}
})
}, evictionTimeout)
const cache = {
add(key, value, _ttl) {
const byteLength = getByteLength(value)
if (totalByteLength + byteLength <= limit) {
totalByteLength += byteLength
store.set(key, {
key,
value,
validTill: Date.now() + (_ttl || ttl),
byteLength
})
}
return value
},
has(key) {
return store.has(key)
},
get(key) {
return store.get(key)?.value || null
},
del(key) {
totalByteLength -= store.get(key)?.byteLength || 0
store.delete(key)
},
size() {
return store.size
},
byteLength () {
return totalByteLength
},
store,
timer,
ttl,
warmup,
evictionTimeout
}
return cache
}