feat(cacheable, memory): add maxTtl option to cap maximum time-to-live#1645
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a new maxTtl option to both Cacheable and CacheableMemory to enforce an upper bound on cache entry lifetimes. The feedback highlights a performance optimization opportunity: rather than parsing shorthand TTL strings (using regex and date instantiations) on every set or setMany operation, the maxTtl value should be pre-parsed into milliseconds and stored as a private field (_maxTtlMs) when initialized or updated. This avoids overhead in the hot path of the cache operations.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1645 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 26 26
Lines 2644 2688 +44
Branches 581 594 +13
=========================================
+ Hits 2644 2688 +44 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: c087656249
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Code Review ResponseAn internal staff-engineer-grade code review was run against this branch. Here's each finding and the action taken:
|
Adds a `maxTtl` option to both `Cacheable` and `CacheableMemory` that enforces an upper bound on any TTL in the cache. When set, per-entry TTLs exceeding maxTtl are capped, and entries with no TTL are also bounded. Default is `undefined` (no maximum). Includes full test coverage and documentation updates. https://claude.ai/code/session_01Gdiqk2rKNc1Vji61zm3qmy
The BEFORE_SET hook can mutate item.ttl, which could bypass the maxTtl enforcement. Now the TTL is re-capped after the hook runs, ensuring maxTtl is always enforced regardless of hook modifications. https://claude.ai/code/session_01Gdiqk2rKNc1Vji61zm3qmy
3e14a6f to
d777d3c
Compare
Summary
Adds a
maxTtloption to bothCacheableandCacheableMemorythat enforces an upper bound on any TTL in the cache. When set, per-entry TTLs exceedingmaxTtlare capped, and entries with no TTL (that would otherwise live indefinitely) are also bounded. This is useful for guaranteeing that no cache entry outlives a maximum duration regardless of what TTL is passed to individualset()calls.Changes
maxTtloption toCacheableMemoryOptionstype andCacheableMemoryclass with getter/setter, constructor handling, and enforcement inset()maxTtloption toCacheableOptionstype andCacheableclass with getter/setter, constructor handling, and enforcement inset()andsetMany()(both primary and secondary stores)@cacheable/memoryandcacheablepackages with usage examples and API referenceVerification
pnpm buildpassesbiome check --error-on-warnings)Test plan
Key scenarios tested:
maxTtldefaults toundefinedundefined,0, or negativemaxTtlmaxTtlmaxTtl'1s','1h')maxTtlboundarysetMany()SetOptionsobject (bothttlandexpirefields)https://claude.ai/code/session_01Gdiqk2rKNc1Vji61zm3qmy
Generated by Claude Code