perf(cache): prune expired entries before eviction#166
Conversation
Reclaim expired entries before applying maxSize LRU eviction so valid entries are not evicted while stale entries still occupy capacity. - Preserve LRU eviction when the cache remains full after pruning - Add regression coverage for TTL and LRU order diverging - Document the TTL plus maxSize behavior in README, API docs, and examples Closes #161
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
✅ Files skipped from review due to trivial changes (2)
📝 WalkthroughWalkthroughThis PR makes MemoryCache.set reclaim TTL-expired entries before performing LRU eviction when a new key is inserted into a full cache. It updates the implementation, adds deterministic tests using fake timers, and revises README, API docs, and example pages to document the behavior. ChangesTTL+MaxSize Pruning Order
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested labels
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@README.md`:
- Around line 266-278: The README example is ambiguous about timing for ttl
expiry; update the MemoryCache example (referencing MemoryCache, ttl, maxSize
and the set calls for 'stale', 'fresh', 'next') to explicitly stagger the writes
or add a comment/note so only 'stale' expires before cache.set('next', ...). For
example, indicate a delay between cache.set('stale', ...) and cache.set('fresh',
...) (or insert an explicit wait/timeout comment) so readers know 'stale' is
allowed to expire while 'fresh' remains valid before calling cache.set('next',
...).
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: ef5e4986-a875-4203-a875-d0646c28e083
📒 Files selected for processing (6)
README.mddocs/src/routes/docs/api/memory-cache/+page.svxdocs/src/routes/examples/+page.tsdocs/src/routes/examples/lru-eviction/+page.sveltesrc/cache.lru.test.tssrc/cache.ts
Stagger the TTL plus maxSize examples so readers can see that only the first entry expires before the next write prunes it.
|
❌ Release workflow failed. Please check the workflow logs |
Summary
Prunes expired entries before applying
maxSizeLRU eviction on new writes. This prevents valid LRU entries from being evicted while expired entries still occupy capacity, and documents howttlandmaxSizeinteract.Closes #161.
Changes
🔧 Refactoring/improvements
prune()before LRU eviction when a newset()would hitmaxSize.🧪 Testing
📚 Documentation
Verification
trunk checkpnpm vitest run src/cache.lru.test.ts src/cache.statistics.test.ts --coverage=falsepnpm exec tsc --noEmit --pretty falsepnpm --filter docs checkpnpm test:only --coverage=falsepnpm --filter docs build/examples/lru-evictionCommits
a3ee694perf(cache): prune expired entries before eviction