-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcache.ts
85 lines (76 loc) · 2.34 KB
/
cache.ts
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
import {Heap} from "heap-js";
import {checkNotUndefined} from "./js/undefined";
import {Logger, ConsoleLogger} from "./logger";
class Value<T> {
constructor(
readonly key: string,
readonly value: T,
public ttl: number,
readonly weight: number,
) {}
}
/**
* A cache that expires the least recently used items in order to maintain a maximum size.
*/
export class WeightBasedLruCache<T> {
private readonly startTime: number;
private currentSize = 0;
private readonly ttls: Record<string, Value<T>> = {};
private lruHeap = new Heap<Value<T>>((first: Value<T>, second: Value<T>) => {
return first.ttl - second.ttl;
});
constructor(
private readonly maxSize: number,
private readonly weighingFunction: (t: T) => number,
private readonly getTime = () => Date.now(),
private readonly logger: Logger = new ConsoleLogger(),
) {
if (this.maxSize <= 0) {
throw new Error(`maxSize must be positive: ${this.maxSize}`);
}
this.startTime = this.getTime();
}
put(key: string, t: T): void {
const weight = this.weighingFunction(t);
if (weight > this.maxSize) {
throw new Error(
`${t} is larger than the maximum size of this cache: ${weight} > ${this.maxSize}`);
}
while (this.currentSize + weight > this.maxSize) {
this.expireOldest();
}
this.currentSize += weight;
const value = new Value(key, t, this.currentTime(), weight);
this.ttls[key] = value;
this.lruHeap.push(value);
}
get(key: string): T | undefined {
if (!(key in this.ttls)) {
return undefined;
}
const value = this.ttls[key];
this.lruHeap.remove(value);
value.ttl = this.currentTime();
this.lruHeap.push(value);
return value.value;
}
private currentTime(): number {
// Subtracting the start time helps keep values simpler for debugging
return this.getTime() - this.startTime;
}
private expireOldest() {
const oldest = checkNotUndefined(this.lruHeap.pop(), "oldest");
// eslint-disable-next-line no-console
this.logger.debug(`Expiring ${oldest.key} from the cache`);
delete this.ttls[oldest.key];
this.currentSize -= oldest.weight;
}
remove(key: string): void {
const value = this.ttls[key];
if (value) {
delete this.ttls[key];
this.currentSize -= value.weight;
this.lruHeap.remove(value);
}
}
}