-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathlru-cache.ts
More file actions
36 lines (31 loc) · 1.05 KB
/
Copy pathlru-cache.ts
File metadata and controls
36 lines (31 loc) · 1.05 KB
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
// A tiny bounded LRU cache.
//
// measureText() and wrapText() keep module-level caches that previously had no
// eviction, so every distinct string an app ever rendered was retained for the
// lifetime of the process — a streaming/typing indicator, a growing log or a
// clock leaked monotonically until OOM (#986). Capping the caches bounds memory
// while keeping recently-used entries hot.
export class LruCache<K, V> {
private readonly cache = new Map<K, V>();
private readonly maxSize: number;
constructor(maxSize: number) {
this.maxSize = maxSize;
}
get(key: K): V | undefined {
if (!this.cache.has(key)) {
return undefined;
}
const value = this.cache.get(key) as V;
// Re-insert so the entry becomes the most recently used.
this.cache.delete(key);
this.cache.set(key, value);
return value;
}
set(key: K, value: V): void {
if (this.cache.size >= this.maxSize) {
// Map iterates in insertion order; the first key is least recently used.
this.cache.delete(this.cache.keys().next().value as K);
}
this.cache.set(key, value);
}
}