forked from badges/shields
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lru-cache.js
55 lines (49 loc) · 1.62 KB
/
lru-cache.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
// Cache any data with a timestamp,
// remove only the oldest data.
function Cache(size) {
if (!this instanceof Cache) { return new Cache(size); }
this.size = size;
// `cache` contains {content, index}.
// - content: the actual data that is cached.
// - index: the position in `order` of the data.
this.cache = Object.create(null);
this.order = []; // list of cache keys from oldest to newest.
}
Cache.prototype.set =
function addToCache(cacheIndex, cached) {
if (this.cache[cacheIndex] !== undefined) {
this.order.splice(this.cache[cacheIndex].index, 1);
// Put the new element at the end of `order`.
this.cache[cacheIndex].index = this.order.length;
this.cache[cacheIndex].content = cached;
this.order.push(cacheIndex);
} else {
// If the cache is full, remove the oldest data
// (ie, the data requested longest ago.)
if (this.order.length >= this.size) {
// Remove `order`'s oldest element, the first.
delete this.cache[this.order[0]];
this.order.shift();
}
this.cache[cacheIndex] = {
index: this.order.length,
content: cached
}
this.order.push(cacheIndex);
}
}
Cache.prototype.get =
function getFromCache(cacheIndex) {
if (this.cache[cacheIndex] !== undefined) {
this.order.splice(this.cache[cacheIndex].index, 1);
// Put the new element at the end of `order`.
this.cache[cacheIndex].index = this.order.length;
this.order.push(cacheIndex);
return this.cache[cacheIndex].content;
} else { return; }
}
Cache.prototype.has =
function hasInCache(cacheIndex) {
return this.cache[cacheIndex] !== undefined;
}
module.exports = Cache;