feat(cache): add TTL-based entry expiry to invoice cache (#606) - #631
Merged
Kingsman-99 merged 1 commit intoAug 26, 2026
Merged
Conversation
…it#606) - Add Cache<V> class to src/cache.ts with optional ttlMs constructor arg - set() records writtenAt timestamp alongside the value - get() returns undefined and deletes the entry when Date.now() - writtenAt > ttlMs - has() also respects TTL — returns false for expired entries - purgeExpired() sweeps all expired entries in one pass - No-expiry behaviour preserved when ttlMs is omitted (backward compatible) - Export Cache from src/index.ts - Unit tests cover all acceptance criteria: entry expires after TTL, valid before TTL, purgeExpired removes only expired, has() returns false for expired, no-expiry compat, delete/clear/size
7 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Title:
feat(cache): add TTL-based entry expiry to invoice cache
───────────────────────────────────────────────────────────────────────────────────────────
Description:
Closes #606
Summary
Adds a new
Cache<V>class tosrc/cache.tswith opt-in TTL-based entryexpiry. When
ttlMsis omitted the class behaves identically to a plainMap, preserving full backward compatibility. When a TTL is supplied,stale entries are evicted lazily on access and/or eagerly via
purgeExpired().The existing
SimpleCache(LRU + per-method TTL config) is untouched.Changes
src/cache.tsCache<V>class with:constructor(ttlMs?: number)— optional TTL; no arg = no expiryset(key, value)— recordswrittenAt: Date.now()alongside the valueget(key)— returnsundefinedand deletes the entry when `Date.now() - writtenAtsrc/index.tsCacheexported from./cache.jsalongsideSimpleCachetest/cache.test.tsvi.useFakeTimers()(no real-time delays):has()returnsfalsefor expired entriespurgeExpired()removes only expired entries, leaves fresh onesdelete,clear,sizecorrectnessTest results
npm test(CI — client + retryPolicy)vitest run test/cache.test.tsNotes
Date.now()only as required by the issue