(usage: CacheUsage)
| 52 | * Returns the updated in-memory state. |
| 53 | */ |
| 54 | export function onResponse(usage: CacheUsage): MemState { |
| 55 | const sig = tokenSignature(usage) |
| 56 | const hitRate = computeHitRate(usage) |
| 57 | |
| 58 | if (sig !== memState.signature) { |
| 59 | // New API response — reset the TTL clock |
| 60 | memState = { |
| 61 | signature: sig, |
| 62 | lastResetAt: Date.now(), |
| 63 | lastHitRate: hitRate, |
| 64 | } |
| 65 | // Persist asynchronously; intentionally fire-and-forget |
| 66 | if (sessionId !== null) { |
| 67 | const filePath = getStateFilePath(sessionId) |
| 68 | const toWrite: CacheStatsState = { |
| 69 | version: 1, |
| 70 | signature: sig, |
| 71 | lastResetAt: memState.lastResetAt, |
| 72 | lastHitRate: hitRate, |
| 73 | } |
| 74 | void writeStateAtomic(filePath, toWrite) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | return { ...memState } |
| 79 | } |
| 80 | |
| 81 | /** Read current in-memory state without triggering a response update. */ |
| 82 | export function getCacheStatsState(): MemState { |
no test coverage detected