MCPcopy Create free account
hub / github.com/ReAPI-com/mcp-openapi / SimpleCache

Class SimpleCache

src/core/Cache.ts:12–87  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

10}
11
12export class SimpleCache<K, V> {
13 private cache: Map<K, CacheEntry<V>>;
14 private maxSize: number;
15 private ttl: number;
16 private cleanupTimer: NodeJS.Timeout | null;
17 private cleanupInterval: number;
18
19 constructor(options: CacheOptions = {}) {
20 this.cache = new Map();
21 this.maxSize = options.maxSize || 500;
22 this.ttl = options.ttl || 60 * 60 * 1000; // 1 hour default
23 this.cleanupInterval = options.cleanupInterval || 5 * 60 * 1000; // 5 minutes default
24 this.cleanupTimer = null;
25 this.startCleanupTimer();
26 }
27
28 private startCleanupTimer(): void {
29 if (this.cleanupTimer) {
30 return;
31 }
32 this.cleanupTimer = setInterval(() => {
33 this.cleanup();
34 }, this.cleanupInterval);
35 }
36
37 private stopCleanupTimer(): void {
38 if (this.cleanupTimer) {
39 clearInterval(this.cleanupTimer);
40 this.cleanupTimer = null;
41 }
42 }
43
44 private cleanup(): void {
45 const now = Date.now();
46 for (const [key, entry] of this.cache.entries()) {
47 if (now - entry.timestamp > this.ttl) {
48 this.cache.delete(key);
49 }
50 }
51 }
52
53 set(key: K, value: V): void {
54 // Remove oldest entry if we're at capacity
55 if (this.cache.size >= this.maxSize) {
56 const oldestKey = this.cache.keys().next().value;
57 this.cache.delete(oldestKey);
58 }
59
60 this.cache.set(key, {
61 value,
62 timestamp: Date.now(),
63 });
64 }
65
66 get(key: K): V | undefined {
67 const entry = this.cache.get(key);
68 if (!entry) return undefined;
69

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected