File size: 952 Bytes
aa89cb5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
type LocalCacheEntry = {
  value: unknown;
  expiresAt?: number;
};

/** Process-local cache with optional TTL in seconds. */
export class LocalCache {
  private readonly entries = new Map<string, LocalCacheEntry>();

  put(key: string, value: unknown, timeToLiveInSeconds?: number): void {
    const expiresAt = timeToLiveInSeconds !== undefined && timeToLiveInSeconds > 0
      ? Date.now() + timeToLiveInSeconds * 1000
      : undefined;
    this.entries.set(key, { value, expiresAt });
  }

  get<T>(key: string): T | undefined {
    const entry = this.entries.get(key);
    if (entry === undefined) return undefined;
    if (entry.expiresAt !== undefined && Date.now() >= entry.expiresAt) {
      this.entries.delete(key);
      return undefined;
    }
    return entry.value as T;
  }

  remove(key: string): void {
    this.entries.delete(key);
  }

  clear(): void {
    this.entries.clear();
  }
}

export const localCache = new LocalCache();