(key: string, results: string[], ttlMs: number)
| 39 | * Writes data to the in-memory cache and sets a timer to evict it after the TTL. |
| 40 | */ |
| 41 | export const write = (key: string, results: string[], ttlMs: number): void => { |
| 42 | // Clear any existing timer for this key to prevent premature deletion |
| 43 | if (cacheTimers.has(key)) { |
| 44 | clearTimeout(cacheTimers.get(key)!); |
| 45 | } |
| 46 | |
| 47 | // Store the new data |
| 48 | crawlCache.set(key, results); |
| 49 | |
| 50 | // Set a timer to automatically delete the cache entry after the TTL |
| 51 | const timerId = setTimeout(() => { |
| 52 | crawlCache.delete(key); |
| 53 | cacheTimers.delete(key); |
| 54 | }, ttlMs); |
| 55 | |
| 56 | // Store the timer handle so we can clear it if the entry is updated |
| 57 | cacheTimers.set(key, timerId); |
| 58 | }; |
| 59 | |
| 60 | /** |
| 61 | * Clears the entire cache and all active timers. |
no test coverage detected