/
lru.ts
48 lines (40 loc) · 1.34 KB
/
lru.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
import LRUCache from 'mnemonist/lru-cache.js';
import {IArrayLikeConstructor} from 'mnemonist/utils/types.d';
import {TemplateFunction} from 'eta/dist/types/compile.d';
export class LRU {
private cache: LRUCache<string, TemplateFunction>;
constructor(capacity = 1000) {
// https://yomguithereal.github.io/posts/lru-cache
this.cache = new LRUCache(
String,
// @ts-ignore: No need to align types here.
Function as TemplateFunction as IArrayLikeConstructor,
capacity,
);
}
get(key: string): TemplateFunction | undefined {
return this.cache.get(key);
}
set(key: string, value: TemplateFunction): void {
this.cache.set(key, value);
}
// ETA storage compatibility
// https://github.com/eta-dev/eta/blob/master/src/storage.ts
define(key: string, value: TemplateFunction): void {
this.cache.set(key, value);
}
remove(key: string): void {
// https://github.com/Yomguithereal/mnemonist/issues/143
// @ts-ignore: Eta cache declares this for some reason (never called in the code actually).
this.cache.set(key, undefined);
}
reset(): void {
this.cache.clear();
}
// Comes from ETA cache implementation (Cacher<TemplateFunction>)
// In `Cacher` it duplicates cache instance from another instance.
// Not going to re-implement that here.
load(): void {
return void null;
}
}