-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
279 lines (257 loc) · 6.79 KB
/
index.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// A simple TTL cache with max capacity option, ms resolution,
// autopurge, and reasonably optimized performance
// Relies on the fact that integer Object keys are kept sorted,
// and managed very efficiently by V8.
/* istanbul ignore next */
const perf =
typeof performance === 'object' &&
performance &&
typeof performance.now === 'function'
? performance
: Date
const now = () => perf.now()
const isPosInt = n => n && n === Math.floor(n) && n > 0 && isFinite(n)
const isPosIntOrInf = n => n === Infinity || isPosInt(n)
class TTLCache {
constructor({
max = Infinity,
ttl,
updateAgeOnGet = false,
noUpdateTTL = false,
dispose,
noDisposeOnSet = false,
} = {}) {
// {[expirationTime]: [keys]}
this.expirations = Object.create(null)
// {key=>val}
this.data = new Map()
// {key=>expiration}
this.expirationMap = new Map()
if (ttl !== undefined && !isPosIntOrInf(ttl)) {
throw new TypeError(
'ttl must be positive integer or Infinity if set'
)
}
if (!isPosIntOrInf(max)) {
throw new TypeError('max must be positive integer or Infinity')
}
this.ttl = ttl
this.max = max
this.updateAgeOnGet = updateAgeOnGet
this.noUpdateTTL = noUpdateTTL
this.noDisposeOnSet = noDisposeOnSet
if (dispose !== undefined) {
if (typeof dispose !== 'function') {
throw new TypeError('dispose must be function if set')
}
this.dispose = dispose
}
this.timers = new Set()
}
// hang onto the timer so we can clearTimeout if all items
// are deleted. Deno doesn't have Timer.unref(), so it
// hangs otherwise.
cancelTimers() {
for (const t of this.timers) {
clearTimeout(t)
this.timers.delete(t)
}
}
clear() {
const entries =
this.dispose !== TTLCache.prototype.dispose ? [...this] : []
this.data.clear()
this.expirationMap.clear()
// no need for any purging now
this.cancelTimers()
this.expirations = Object.create(null)
for (const [key, val] of entries) {
this.dispose(val, key, 'delete')
}
}
setTTL(key, ttl = this.ttl) {
const current = this.expirationMap.get(key)
if (current !== undefined) {
// remove from the expirations list, so it isn't purged
const exp = this.expirations[current]
if (!exp || exp.length <= 1) {
delete this.expirations[current]
} else {
this.expirations[current] = exp.filter(k => k !== key)
}
}
if (ttl !== Infinity) {
const expiration = Math.floor(now() + ttl)
this.expirationMap.set(key, expiration)
if (!this.expirations[expiration]) {
const t = setTimeout(() => {
this.timers.delete(t)
this.purgeStale()
}, ttl)
/* istanbul ignore else - affordance for non-node envs */
if (t.unref) t.unref()
this.timers.add(t)
this.expirations[expiration] = []
}
this.expirations[expiration].push(key)
} else {
this.expirationMap.set(key, Infinity)
}
}
set(
key,
val,
{
ttl = this.ttl,
noUpdateTTL = this.noUpdateTTL,
noDisposeOnSet = this.noDisposeOnSet,
} = {}
) {
if (!isPosIntOrInf(ttl)) {
throw new TypeError('ttl must be positive integer or Infinity')
}
if (this.expirationMap.has(key)) {
if (!noUpdateTTL) {
this.setTTL(key, ttl)
}
// has old value
const oldValue = this.data.get(key)
if (oldValue !== val) {
this.data.set(key, val)
if (!noDisposeOnSet) {
this.dispose(oldValue, key, 'set')
}
}
} else {
this.setTTL(key, ttl)
this.data.set(key, val)
}
while (this.size > this.max) {
this.purgeToCapacity()
}
return this
}
has(key) {
return this.data.has(key)
}
getRemainingTTL(key) {
const expiration = this.expirationMap.get(key)
return expiration === Infinity
? expiration
: expiration !== undefined
? Math.max(0, Math.ceil(expiration - now()))
: 0
}
get(
key,
{ updateAgeOnGet = this.updateAgeOnGet, ttl = this.ttl } = {}
) {
const val = this.data.get(key)
if (updateAgeOnGet) {
this.setTTL(key, ttl)
}
return val
}
dispose(_, __) {}
delete(key) {
const current = this.expirationMap.get(key)
if (current !== undefined) {
const value = this.data.get(key)
this.data.delete(key)
this.expirationMap.delete(key)
const exp = this.expirations[current]
if (exp) {
if (exp.length <= 1) {
delete this.expirations[current]
} else {
this.expirations[current] = exp.filter(k => k !== key)
}
}
this.dispose(value, key, 'delete')
if (this.size === 0) {
this.cancelTimers()
}
return true
}
return false
}
purgeToCapacity() {
for (const exp in this.expirations) {
const keys = this.expirations[exp]
if (this.size - keys.length >= this.max) {
delete this.expirations[exp]
const entries = []
for (const key of keys) {
entries.push([key, this.data.get(key)])
this.data.delete(key)
this.expirationMap.delete(key)
}
for (const [key, val] of entries) {
this.dispose(val, key, 'evict')
}
} else {
const s = this.size - this.max
const entries = []
for (const key of keys.splice(0, s)) {
entries.push([key, this.data.get(key)])
this.data.delete(key)
this.expirationMap.delete(key)
}
for (const [key, val] of entries) {
this.dispose(val, key, 'evict')
}
return
}
}
}
get size() {
return this.data.size
}
purgeStale() {
const n = Math.ceil(now())
for (const exp in this.expirations) {
if (exp === 'Infinity' || exp > n) {
return
}
const keys = [...this.expirations[exp]]
const entries = []
delete this.expirations[exp]
for (const key of keys) {
entries.push([key, this.data.get(key)])
this.data.delete(key)
this.expirationMap.delete(key)
}
for (const [key, val] of entries) {
this.dispose(val, key, 'stale')
}
}
if (this.size === 0) {
this.cancelTimers()
}
}
*entries() {
for (const exp in this.expirations) {
for (const key of this.expirations[exp]) {
yield [key, this.data.get(key)]
}
}
}
*keys() {
for (const exp in this.expirations) {
for (const key of this.expirations[exp]) {
yield key
}
}
}
*values() {
for (const exp in this.expirations) {
for (const key of this.expirations[exp]) {
yield this.data.get(key)
}
}
}
[Symbol.iterator]() {
return this.entries()
}
}
module.exports = TTLCache