1 |
|
2 |
|
3 |
|
4 | export function lruQueue(limit) {
|
5 | var size = 0;
|
6 | var base = 1;
|
7 | var queue = Object.create(null);
|
8 | var map = Object.create(null);
|
9 | var index = 0;
|
10 |
|
11 | var del = function del(id) {
|
12 | var oldIndex = map[id];
|
13 | if (!oldIndex) return;
|
14 | delete queue[oldIndex];
|
15 | delete map[id];
|
16 | --size;
|
17 | if (base !== oldIndex) return;
|
18 |
|
19 | if (!size) {
|
20 | index = 0;
|
21 | base = 1;
|
22 | return;
|
23 | }
|
24 |
|
25 | while (!hasOwnProperty.call(queue, ++base)) {
|
26 | continue;
|
27 | }
|
28 | };
|
29 |
|
30 | limit = Math.abs(limit);
|
31 | return {
|
32 | hit: function hit(id) {
|
33 | var oldIndex = map[id];
|
34 | var nuIndex = ++index;
|
35 | queue[nuIndex] = id;
|
36 | map[id] = nuIndex;
|
37 |
|
38 | if (!oldIndex) {
|
39 | ++size;
|
40 | if (size <= limit) return undefined;
|
41 | id = queue[base];
|
42 | del(id);
|
43 | return id;
|
44 | }
|
45 |
|
46 | delete queue[oldIndex];
|
47 | if (base !== oldIndex) return undefined;
|
48 |
|
49 | while (!hasOwnProperty.call(queue, ++base)) {
|
50 | continue;
|
51 | }
|
52 |
|
53 | return undefined;
|
54 | },
|
55 | delete: del,
|
56 | clear: function clear() {
|
57 | size = index = 0;
|
58 | base = 1;
|
59 | queue = Object.create(null);
|
60 | map = Object.create(null);
|
61 | }
|
62 | };
|
63 | }
|
64 | ; |
\ | No newline at end of file |