-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlru.js
95 lines (81 loc) · 1.92 KB
/
lru.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
// Simple doubly linked list with just what we need
// Add
// | first last
// | __ __ __ __
// V | | --next-> | | --next-> | | --next-> | |
// |__| <-prev-- |__| <-prev-- |__| <-prev-- |__|
function LL() {
this.first = null;
this.last = null;
this.size = 0;
}
LL.prototype.add = function(node) {
node.next = this.first;
node.prev = null;
if (this.first === null) {
this.last = node;
} else {
this.first.prev = node;
}
this.first = node;
this.size++;
return node;
}
LL.prototype.remove = function(node) {
if (node.next !== null) {
node.next.prev = node.prev;
} else {
this.last = node.prev;
}
if (node.prev !== null) {
node.prev.next = node.next;
} else {
this.first = node.next;
}
this.size--;
}
LL.prototype.moveToFirst = function(node) {
this.remove(node);
this.add(node);
}
LL.prototype.removeLast = function() {
if (this.size === 0) {
return;
}
this.remove(this.last);
}
function LRU(maxSize) {
this.data = {};
this.ll = new LL();
this.maxSize = maxSize;
}
LRU.prototype.set = function(key, value) {
if (key === undefined || value === undefined) {
return;
}
if (this.data[key]) {
this.data[key].value = value;
// Not sure if we should move to first when updating an existing cache value
this.ll.moveToFirst(this.data[key]);
} else {
let node = {value, key};
this.data[key] = node;
if (this.ll.size >= this.maxSize) {
delete this.data[this.ll.last.key];
this.ll.removeLast();
}
this.ll.add(node);
}
};
LRU.prototype.get = function(key, getDataFn) {
if (this.data[key]) {
const node = this.data[key];
this.ll.moveToFirst(node);
return node.value;
} else if(typeof getDataFn === "function") {
const value = getDataFn();
this.set(key, value);
return value;
}
};
module.exports = LRU;