forked from screepers/screeps-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEs6LRUMapWithSizeAndTtl.js
188 lines (169 loc) · 3.71 KB
/
Es6LRUMapWithSizeAndTtl.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
/**
* posted 3 April 2018 by @warinternal
*
* A cache that can exhibit both least recently used (LRU) and max time to live (TTL) eviction policies.
*
* Internally the cache is backed by a `Map` but also maintains a linked list of entries to support the eviction policies.
* Source: https://github.com/ianp/es6-lru-cache
*/
'use strict';
class LRU {
// cache entries are objects with
// key - duplicated here to make iterator based methods more efficient
// value
// prev - a pointer
// next - a pointer
// expires - time of death in Date.now
/**
*
* @param {number} ttl - the max. time to live, in ticks
* @param {number} max - the max. number of entries in the cache
* @param {Object|Iterable} data - the data to initialize the cache with
*/
constructor({ ttl, max, data = {} }) {
this.data = new Map();
if (max) { this.max = max; }
if (ttl) { this.ttl = ttl; }
// this.head = undefined
// this.tail = undefined
if (data) {
if (data[Symbol.iterator]) {
for (const [key, value] in data) {
this.set(key, value);
}
} else {
Object.keys(data).forEach(key => this.set(key, data[key]));
}
}
}
clear() {
this.data.clear();
this.head = undefined;
this.tail = undefined;
}
delete(key) {
const curr = this.data.get(key);
if (this.data.delete(key)) {
this._remove(curr);
return true;
}
return false;
}
entries() {
return this.it(entry => [entry.key, entry.value]);
}
evict() {
let curr,count = 0;
const {max} = this;
const now = this.ttl ? Game.time : false;
for (curr = this.head; curr; curr = curr.next) {
++count;
if ((max && max < count) || (now && now > curr.expires)) {
this.data.delete(curr.key);
this._remove(curr);
}
}
return this.data.size;
}
forEach(callback) {
const iterator = this.it(entry => {
callback(entry.key, entry.value); // todo: support thisArg parameter
return true;
});
while (iterator.next()) { /* no-op */ }
}
get(key) {
const entry = this.data.get(key);
if (entry) {
if (entry.expires && entry.expires < Game.time) {
this.delete(key);
} else {
this._remove(entry);
this._insert(entry);
return entry.value;
}
}
return null;
}
has(key) {
const entry = this.data.get(key);
if (entry) {
if (entry.expires && entry.expires < Game.time) {
this.delete(key);
} else {
return true;
}
}
return false;
}
keys() {
return this.it(entry => entry.key);
}
set(key, value) {
let curr = this.data.get(key);
if (curr) {
this._remove(curr);
} else {
this.data.set(key, curr = {});
}
curr.key = key;
curr.value = value;
if (this.ttl) { curr.expires = Game.time + this.ttl; }
this._insert(curr);
this.evict();
return this;
}
get size() {
// run an eviction then we will report the correct size
return this.evict();
}
values() {
return this.it(entry => entry.value);
}
[Symbol.iterator]() {
return this.it(entry => [entry.key, entry.value]);
}
*it(accessFn) {
this.evict();
let curr;
for (curr = this.head; curr; curr = curr.next)
yield accessFn(curr);
}
/**
* Remove entry `curr` from the linked list.
* @private
*/
_remove(curr) {
if (!curr.prev) {
this.head = curr.next;
} else {
curr.prev.next = curr.next;
}
if (!curr.next) {
this.tail = curr.prev;
} else {
curr.next.prev = curr.prev;
}
}
/**
* Insert entry `curr` into the head of the linked list.
* @private
*/
_insert(curr) {
if (!this.head) {
this.head = curr;
this.tail = curr;
} else {
const node = this.head;
curr.prev = node.prev;
curr.next = node;
if (!node.prev) {
this.head = curr;
} else {
node.prev.next = curr;
}
node.prev = curr;
}
}
}
module.exports = LRU;