-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathLRUCache.js
169 lines (151 loc) · 3.81 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
/* eslint-disable max-classes-per-file */
import Store from '../Store';
/**
* @template T
* @author KhaaZ
* @class Node
*/
class Node {
/**
* Creates an instance of Node
* @param {String} key
* @param {T} [value]
* @param {Node<T>} [next]
* @param {Node<T>} [prev]
*/
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
*
* @template T
* @class LRUCache
* @extends Store<Node<T>>
* @prop {Number} limit - Maximum size of the LRU
* @prop {Node<T>} head
* @prop {Node<T>} tail
*/
class LRUCache extends Store {
/**
* Creates an instance of LRUCache.
*
* @param {Number} limit - Max number of elements in the cache
* @param {Object} options - Options used to construct the cache
* @param {Object.<string, T>|Array<[string, T]>} [options.iterable=null]
* @memberof LRUCache
*/
constructor(limit, options = {} ) {
super(new Map() );
const { iterable = null } = options;
this.limit = limit;
/**
* @type {Node<T>}
*/
this.head = null;
/**
* @type {Node<T>}
*/
this.tail = null;
if (iterable && Array.isArray(iterable) ) {
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] );
}
}
}
/**
* Retrieve a value from the LRU cache
*
* @param {String} key
* @returns {T} value
* @memberof LRUCache
*/
get(key) {
const node = this.cache.get(key);
if (node) {
const { value } = node;
// move to top
this.delete(key);
this.set(key, value);
return value;
}
return null;
}
/**
* Add a value in the LRU cache.
*
* @param {String} key
* @param {T} value
* @param {LRUCache<T>} - The current LRUCache
* @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);
return this;
}
/**
* Delete an element from the LRUCache
*
* @param {String} key
* @returns {Boolean} - Wether it deleted the item or not
* @memberof LRUCache
*/
delete(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;
}
return this.cache.delete(key);
}
/**
* Empty the LRUCache entirely
* @memberof LRUCache
*/
clear() {
this.head = null;
this.tail = null;
this.cache = new Map();
}
_ensureLimit() {
if (this.size === this.limit) {
this.delete(this.tail.key);
}
}
// 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;