Repository navigation
fix(memory): proactively flush expired entries after TTL - #759
Conversation
Schedule a setTimeout per TTL entry to delete it from the internal Map when it expires, preventing unbounded memory growth from entries that are never read again (e.g. unique URLs with varying query params). Closes nitrojs/nitro#2138 Ref unjs/ocache#10
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughAdded per-key TTL support to the in-memory cache driver using a timers map. When setting items with optional TTL, existing timers are cleared and new expiry timers are scheduled to auto-delete keys. A regression test validates automatic expiration after TTL expires without explicit reads. Changes
Sequence DiagramsequenceDiagram
actor User
participant Driver as Memory Driver
participant Storage as Storage Map
participant Timers as Timers Map
participant Timer as Node Timer
User->>Driver: setItem(key, value, {ttl: 0.01})
Driver->>Timers: Check & clear existing timer
alt Timer exists
Timers->>Timer: clearTimeout()
end
Driver->>Storage: Store value
Driver->>Timer: setTimeout(callback, 10ms)
Timer->>Timers: Store timer reference
Note over Driver: ~10ms delay
Timer->>Driver: Timer fires (expiry callback)
Driver->>Storage: Delete key
Driver->>Timers: Remove timer reference
Driver->>User: Confirm deletion
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
📝 Coding Plan
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #759 +/- ##
==========================================
+ Coverage 64.52% 64.81% +0.29%
==========================================
Files 43 43
Lines 2207 2234 +27
Branches 520 523 +3
==========================================
+ Hits 1424 1448 +24
- Misses 675 678 +3
Partials 108 108 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Summary
setTimeoutper TTL entry to proactively delete it from the internalMapwhen it expires.unref()so they don't prevent process exitRef unjs/ocache#10 nitrojs/nitro#2138
Test plan
Summary by CodeRabbit
New Features
Tests