1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | import * as list from './list.js'
|
11 | import * as map from './map.js'
|
12 | import * as time from './time.js'
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | class Entry {
|
20 | |
21 |
|
22 |
|
23 |
|
24 | constructor (key, val) {
|
25 | |
26 |
|
27 |
|
28 | this.prev = null
|
29 | |
30 |
|
31 |
|
32 | this.next = null
|
33 | this.created = time.getUnixTime()
|
34 | this.val = val
|
35 | this.key = key
|
36 | }
|
37 | }
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | export class Cache {
|
43 | |
44 |
|
45 |
|
46 | constructor (timeout) {
|
47 | this.timeout = timeout
|
48 | |
49 |
|
50 |
|
51 | this._q = list.create()
|
52 | |
53 |
|
54 |
|
55 | this._map = map.create()
|
56 | }
|
57 | }
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | export const removeStale = cache => {
|
66 | const now = time.getUnixTime()
|
67 | const q = cache._q
|
68 | while (q.start && now - q.start.created > cache.timeout) {
|
69 | cache._map.delete(q.start.key)
|
70 | list.popFront(q)
|
71 | }
|
72 | return now
|
73 | }
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | export const set = (cache, key, value) => {
|
83 | const now = removeStale(cache)
|
84 | const q = cache._q
|
85 | const n = cache._map.get(key)
|
86 | if (n) {
|
87 | list.removeNode(q, n)
|
88 | list.pushEnd(q, n)
|
89 | n.created = now
|
90 | n.val = value
|
91 | } else {
|
92 | const node = new Entry(key, value)
|
93 | list.pushEnd(q, node)
|
94 | cache._map.set(key, node)
|
95 | }
|
96 | }
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | const getNode = (cache, key) => {
|
106 | removeStale(cache)
|
107 | const n = cache._map.get(key)
|
108 | if (n) {
|
109 | return n
|
110 | }
|
111 | }
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 | export const get = (cache, key) => {
|
121 | const n = getNode(cache, key)
|
122 | return n && !(n.val instanceof Promise) ? n.val : undefined
|
123 | }
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 | export const refreshTimeout = (cache, key) => {
|
132 | const now = time.getUnixTime()
|
133 | const q = cache._q
|
134 | const n = cache._map.get(key)
|
135 | if (n) {
|
136 | list.removeNode(q, n)
|
137 | list.pushEnd(q, n)
|
138 | n.created = now
|
139 | }
|
140 | }
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 | export const getAsync = (cache, key) => {
|
153 | const n = getNode(cache, key)
|
154 | return n ? n.val : undefined
|
155 | }
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 | export const remove = (cache, key) => {
|
164 | const n = cache._map.get(key)
|
165 | if (n) {
|
166 | list.removeNode(cache._q, n)
|
167 | cache._map.delete(key)
|
168 | return n.val && !(n.val instanceof Promise) ? n.val : undefined
|
169 | }
|
170 | }
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 | export const setIfUndefined = (cache, key, init, removeNull = false) => {
|
182 | removeStale(cache)
|
183 | const q = cache._q
|
184 | const n = cache._map.get(key)
|
185 | if (n) {
|
186 | return n.val
|
187 | } else {
|
188 | const p = init()
|
189 | const node = new Entry(key, p)
|
190 | list.pushEnd(q, node)
|
191 | cache._map.set(key, node)
|
192 | p.then(v => {
|
193 | if (p === node.val) {
|
194 | node.val = v
|
195 | }
|
196 | if (removeNull && v == null) {
|
197 | remove(cache, key)
|
198 | }
|
199 | })
|
200 | return p
|
201 | }
|
202 | }
|
203 |
|
204 |
|
205 |
|
206 |
|
207 | export const create = timeout => new Cache(timeout)
|