patchCache.ts
| 1 | import { MAX_PATCH_CACHE, PATCH_CACHE_TTL_MS } from "../constants.ts"; |
| 2 | |
| 3 | export type ApplyResult = { status: "clean" | "conflict"; output: string }; |
| 4 | type Entry = { result: ApplyResult; expiresAt: number }; |
| 5 | const cache = new Map<number, Entry>(); |
| 6 | |
| 7 | export const patchCache = { |
| 8 | get: (id: number): ApplyResult | undefined => { |
| 9 | const entry = cache.get(id); |
| 10 | if (!entry) return undefined; |
| 11 | if (entry.expiresAt <= Date.now()) { |
| 12 | cache.delete(id); |
| 13 | return undefined; |
| 14 | } |
| 15 | return entry.result; |
| 16 | }, |
| 17 | set: (id: number, result: ApplyResult) => { |
| 18 | if (cache.size >= MAX_PATCH_CACHE && !cache.has(id)) { |
| 19 | cache.delete(cache.keys().next().value!); |
| 20 | } |
| 21 | cache.set(id, { result, expiresAt: Date.now() + PATCH_CACHE_TTL_MS }); |
| 22 | }, |
| 23 | invalidate: (id: number) => cache.delete(id), |
| 24 | }; |
| 25 |