-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
cache.js
206 lines (193 loc) · 3.88 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
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
/* eslint-env browser */
/**
* An implementation of a map which has keys that expire.
*
* @module cache
*/
import * as list from './list.js'
import * as map from './map.js'
import * as time from './time.js'
/**
* @template K, V
*
* @implements {list.ListNode}
*/
class Entry {
/**
* @param {K} key
* @param {V | Promise<V>} val
*/
constructor (key, val) {
/**
* @type {this | null}
*/
this.prev = null
/**
* @type {this | null}
*/
this.next = null
this.created = time.getUnixTime()
this.val = val
this.key = key
}
}
/**
* @template K, V
*/
export class Cache {
/**
* @param {number} timeout
*/
constructor (timeout) {
this.timeout = timeout
/**
* @type list.List<Entry<K, V>>
*/
this._q = list.create()
/**
* @type {Map<K, Entry<K, V>>}
*/
this._map = map.create()
}
}
/**
* @template K, V
*
* @param {Cache<K, V>} cache
* @return {number} Returns the current timestamp
*/
export const removeStale = cache => {
const now = time.getUnixTime()
const q = cache._q
while (q.start && now - q.start.created > cache.timeout) {
cache._map.delete(q.start.key)
list.popFront(q)
}
return now
}
/**
* @template K, V
*
* @param {Cache<K, V>} cache
* @param {K} key
* @param {V} value
*/
export const set = (cache, key, value) => {
const now = removeStale(cache)
const q = cache._q
const n = cache._map.get(key)
if (n) {
list.removeNode(q, n)
list.pushEnd(q, n)
n.created = now
n.val = value
} else {
const node = new Entry(key, value)
list.pushEnd(q, node)
cache._map.set(key, node)
}
}
/**
* @template K, V
*
* @param {Cache<K, V>} cache
* @param {K} key
* @return {Entry<K, V> | undefined}
*/
const getNode = (cache, key) => {
removeStale(cache)
const n = cache._map.get(key)
if (n) {
return n
}
}
/**
* @template K, V
*
* @param {Cache<K, V>} cache
* @param {K} key
* @return {V | undefined}
*/
export const get = (cache, key) => {
const n = getNode(cache, key)
return n && !(n.val instanceof Promise) ? n.val : undefined
}
/**
* @template K, V
*
* @param {Cache<K, V>} cache
* @param {K} key
*/
export const refreshTimeout = (cache, key) => {
const now = time.getUnixTime()
const q = cache._q
const n = cache._map.get(key)
if (n) {
list.removeNode(q, n)
list.pushEnd(q, n)
n.created = now
}
}
/**
* Works well in conjunktion with setIfUndefined which has an async init function.
* Using getAsync & setIfUndefined ensures that the init function is only called once.
*
* @template K, V
*
* @param {Cache<K, V>} cache
* @param {K} key
* @return {V | Promise<V> | undefined}
*/
export const getAsync = (cache, key) => {
const n = getNode(cache, key)
return n ? n.val : undefined
}
/**
* @template K, V
*
* @param {Cache<K, V>} cache
* @param {K} key
*/
export const remove = (cache, key) => {
const n = cache._map.get(key)
if (n) {
list.removeNode(cache._q, n)
cache._map.delete(key)
return n.val && !(n.val instanceof Promise) ? n.val : undefined
}
}
/**
* @template K, V
*
* @param {Cache<K, V>} cache
* @param {K} key
* @param {function():Promise<V>} init
* @param {boolean} removeNull Optional argument that automatically removes values that resolve to null/undefined from the cache.
* @return {Promise<V> | V}
*/
export const setIfUndefined = (cache, key, init, removeNull = false) => {
removeStale(cache)
const q = cache._q
const n = cache._map.get(key)
if (n) {
return n.val
} else {
const p = init()
const node = new Entry(key, p)
list.pushEnd(q, node)
cache._map.set(key, node)
p.then(v => {
if (p === node.val) {
node.val = v
}
if (removeNull && v == null) {
remove(cache, key)
}
})
return p
}
}
/**
* @param {number} timeout
*/
export const create = timeout => new Cache(timeout)