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