Repository navigation
feat(cache): support dynamic per-entry TTL via getMaxAge hook - #39
Conversation
|
Warning Review limit reached
Next review available in: 17 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review detailsβοΈ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: π Files selected for processing (5)
β¨ Finishing Touchesπ§ͺ Generate unit tests (beta)
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 |
Adds a `getMaxAge` option that runs after the resolver and returns
either a number (seconds, shorthand for `maxAge`) or
`{ maxAge?, staleMaxAge? }` to override the static options for that
specific entry. Resolved values are persisted on the entry and drive
both the read freshness check and the storage TTL, letting the cache
honor per-entry lifetimes (OAuth `expires_in`, upstream
`Cache-Control`, content-derived freshness).
Additive and backwards compatible β absent `getMaxAge` leaves behavior
unchanged. Flows through to `defineCachedHandler` automatically.
Closes #36
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
ttl hookgetMaxAge hook
A negative or non-finite `getMaxAge` result (e.g. an already-expired token's `expires_in - margin`) previously left `ttl <= 0`, which the freshness check treated as "never expires" β pinning the entry as fresh forever instead of not caching it. Clamp negatives to 0 (re-resolve every access) and fall back to static options for non-finite values. Fix the README example so it can't derive a negative TTL. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Closes #36.
Summary
TTL was fixed at creation time (
maxAge/staleMaxAge). This adds agetMaxAgehook that derives an entry's lifetime from the value it resolved to, so the cache can honor per-entry expiry instead of a single global constant.Motivating cases: OAuth/API tokens with
expires_in, upstream responses carrying their ownCache-Control: max-age, or content-derived freshness.How it works
getMaxAge?: (entry) => number | { maxAge?, staleMaxAge? } | undefinedruns after the resolver, before the entry is persisted (async-supported).{ maxAge }; return an object to also override the stale window.CacheEntry.maxAge/CacheEntry.staleMaxAge) and used for both the read-side freshness check (including the SWR stale window) and the storage TTL on write.undefinedreturn β falls back to the static option. If the hook throws, the entry is still cached with static options and the error is reported viaonError.expireCache()prefers the per-entry TTL when computing the remaining storage window.defineCachedHandlerautomatically (entry value is theResponseCacheEntry).Compatibility
Additive and fully backwards compatible β when
getMaxAgeis absent, behavior is unchanged.Tests
New
getMaxAgeblock covering: number-shorthand freshness derived from the value, persistence on the stored entry, fallback when the hook returnsundefined, per-entry SWR stale window, hook-throws resilience, andexpireCacherespecting per-entry TTL. All 105 tests pass; typecheck clean.π€ Generated with Claude Code