File size: 1,748 Bytes
05c5ed5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { Cache } from "./cache.interface";

type Entry<V> = { value: V; expiresAt: number };
interface MemoryCacheOptions {
  defaultTtlMs?: number;
  cleanupIntervalMs?: number;
}

export class MemoryCache implements Cache {
  private store = new Map<string, Entry<JsonValue>>();
  private defaultTtlMs: number;
  constructor(opts: MemoryCacheOptions = {}) {
    this.defaultTtlMs = opts.defaultTtlMs ?? Infinity;
    const interval = opts.cleanupIntervalMs ?? 60_000;
    if (isFinite(interval) && interval > 0) {
      setInterval(() => this.sweep(), interval).unref();
    }
  }

  async get<T>(key: string): Promise<T | undefined> {
    const e = this.store.get(key);
    if (!e) return undefined;
    if (Date.now() > e.expiresAt) {
      this.store.delete(key);
      return undefined;
    }

    return e.value as T;
  }

  async set(key: string, value: any, ttlMs = this.defaultTtlMs) {
    const expiresAt = isFinite(ttlMs) ? Date.now() + ttlMs : Infinity;
    this.store.set(key, { value, expiresAt });
  }

  async has(key: string) {
    return (await this.get(key)) !== undefined;
  }
  async delete(key: string) {
    this.store.delete(key);
  }
  async clear() {
    this.store.clear();
  }

  async getAll(): Promise<Map<string, unknown>> {
    const result = new Map<string, unknown>();
    const now = Date.now();

    for (const [key, entry] of this.store) {
      if (now <= entry.expiresAt) {
        result.set(key, entry.value);
      } else {
        // Clean up expired entries while we're iterating
        this.store.delete(key);
      }
    }

    return result;
  }

  private sweep() {
    const now = Date.now();
    for (const [k, { expiresAt }] of this.store)
      if (now > expiresAt) this.store.delete(k);
  }
}