forked from Khaaz/AxonCore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLRUCache.js
231 lines (206 loc) · 5.71 KB
/
LRUCache.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/* eslint-disable max-classes-per-file */
import Collection from '../Collection';
class Node {
constructor(key, value, next = null, prev = null) {
this.key = key;
this.value = value;
this.next = next;
this.prev = prev;
}
}
/**
* Least Recently Used cache implementation.
* Read and Write operations are in O(1)
*
* https://medium.com/dsinjs/implementing-lru-cache-in-javascript-94ba6755cda9
*
* @author KhaaZ
*
* @class LRUCache
*
* @prop {Number} limit - Maximum size of the LRU
* @prop {Number} size - Current size of the LRU
* @prop {Node} head
* @prop {Node} tail
* @prop {Collection<*>} _cache - The Collection holding the cache (private, handled by the LRU structure)
*/
class LRUCache {
/**
* Creates an instance of LRUCache.
*
* @param {Number} limit - Max number of element in the Collection
* @param {Object} options - Options used to construct the Collection
* @param {Class} [options.base=null]
* @param {Array|Object} [options.iterable=null]
*
* @memberof LRUCache
*/
constructor(limit, options = {} ) {
const { iterable = null } = options;
this.limit = limit;
this.size = 0;
this.head = null;
this.queue = null;
this._cache = new Collection( { base: options.base } );
if (iterable && iterable instanceof Array) {
for (const elem of iterable) {
this.set(elem[0], elem[1] );
}
} else if (iterable && iterable instanceof Object) {
for (const elem of Object.entries(iterable) ) {
this.set(elem[0], elem[1] );
}
}
}
/**
* Add a value in the LRU cache.
*
* @param {String} key
* @param {*} value
*
* @memberof LRUCache
*/
set(key, value) {
if (!this.head) {
this.head = this.tail = new Node(key, value);
} else {
const node = new Node(key, value, this.head);
this.head.prev = node;
this.head = node;
}
// Update the cache map
this._cache.set(key, this.head);
this.size++;
}
/**
* Retrieve a value from the LRU cache
*
* @param {String} key
* @returns {*} value
*
* @memberof LRUCache
*/
get(key) {
const node = this._cache.get(key);
if (node) {
const { value } = node;
// move to top
this.remove(key);
this.set(key, value);
return value;
}
return null;
}
/**
* Remove an element from the LRUCache
*
* @param {String} key
*
* @memberof LRUCache
*/
remove(key) {
const node = this._cache.get(key);
if (node.prev !== null) {
node.prev.next = node.next;
} else {
this.head = node.next;
}
if (node.next !== null) {
node.next.prev = node.prev;
} else {
this.tail = node.prev;
}
this._cache.delete(key);
this.size--;
}
/**
* Empty the LRUCache entirely
*
* @memberof LRUCache
*/
clear() {
this.head = null;
this.tail = null;
this.size = 0;
this._cache = new Collection();
}
_ensureLimit() {
if (this.size === this.limit) {
this.remove(this.tail.key);
}
}
/**
* Execute a function against every element of the Collection
*
* @param {Function} fn
*
* @memberof LRUCache
*/
forEach(fn) {
this._cache.forEach(fn);
}
/**
* Return the first object to make the function evaluate true
*
* @param {Function} func - A function that takes an object and returns true if it matches
* @returns {Class} The first matching object, or null if no match
*
* @memberof LRUCache
*/
find(func) {
return this._cache.find(func);
}
/**
* Return an array with the results of applying the given function to each element
*
* @param {Function} func - A function that takes an object and returns something
* @returns {Array} An array containing the results
*
* @memberof LRUCache
*/
map(func) {
return this._cache.map(func);
}
/**
* Return all the objects that make the function evaluate true
*
* @param {Function} func - A function that takes an object and returns true if it matches
* @returns {Array<Class>} An array containing all the objects that matched
*
* @memberof LRUCache
*/
filter(func) {
return this._cache.filter(func);
}
/**
* Test if at least one element passes the test implemented by the provided function. Returns true if yes, or false if not.
*
* @param {Function} func - A function that takes an object and returns true if it matches
* @returns {Boolean} An array containing all the objects that matched
*
* @memberof LRUCache
*/
some(func) {
return this._cache.some(func);
}
/**
* Test if all elements passe the test implemented by the provided function. Returns true if yes, or false if not.
*
* @param {Function} func - A function that takes an object and returns true if it matches
* @returns {Boolean} An array containing all the objects that matched
*
* @memberof LRUCache
*/
every(func) {
return this._cache.every(func);
}
// To iterate over LRU with a 'for...of' loop
*[Symbol.iterator]() {
let node = this.head;
while (node) {
yield node;
node = node.next;
}
}
}
export default LRUCache;