File size: 829 Bytes
7e3630c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { JimpImage } from "./image.js";

export class ImageCache {
  #cache = new Map<string, JimpImage>();
  #maxEntries: number;

  constructor(maxEntries: number) {
    this.#maxEntries = Math.max(1, maxEntries);
  }

  get(key: string): JimpImage | undefined {
    const entry = this.#cache.get(key);
    if (!entry) return undefined;

    this.#cache.delete(key);
    this.#cache.set(key, entry);

    return entry.clone();
  }

  set(key: string, image: JimpImage): void {
    this.#cache.delete(key);

    while (this.#cache.size >= this.#maxEntries) {
      const lru = this.#cache.keys().next().value;
      if (lru !== undefined) this.#cache.delete(lru);
    }

    this.#cache.set(key, image.clone());
  }

  clear(): void {
    this.#cache.clear();
  }

  get size(): number {
    return this.#cache.size;
  }
}