Skip to content

fix: evict expired entries from cursorIdentityCache#5

Open
iRonin wants to merge 1 commit into
standardagents:mainfrom
iRonin:fix/cache-eviction
Open

fix: evict expired entries from cursorIdentityCache#5
iRonin wants to merge 1 commit into
standardagents:mainfrom
iRonin:fix/cache-eviction

Conversation

@iRonin
Copy link
Copy Markdown

@iRonin iRonin commented May 23, 2026

Problem

cursorIdentityCache is a plain Map with a 1-hour TTL but never evicts expired entries. Keys that are looked up once and never again accumulate indefinitely. In long-lived Worker isolates (high-traffic deployments with rotating API keys), this causes unbounded memory growth.

Fix

  • Lazy eviction: when getCursorAccountIdentity finds an expired entry, it deletes it immediately
  • Periodic sweep: after each cache set(), evictExpiredCacheEntries() scans and removes all other expired entries — handles keys that are never looked up again
  • Both operations are O(n) in cache size, but n is expected to be very small (one entry per active API key)

Files changed

  • worker/cursor.ts — eviction logic in getCursorAccountIdentity + new evictExpiredCacheEntries()
  • worker/cursor.test.ts — 2 new tests for eviction behavior

The cache sets a 1-hour TTL but never removes expired entries, causing
unbounded growth in long-lived Worker isolates when API keys are rotated.

- Add lazy eviction on cache miss (delete expired entry when found)
- Add periodic sweep on each set() to evict all other expired entries
- Export evictExpiredCacheEntries and cursorIdentityCache for testing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant