Skip to content

feat(cache): implement stale-while-revalidate in OptimisticCache (#776) - #819

Open
ZacLou wants to merge 1 commit into
Stellar-split:mainfrom
ZacLou:feat/optimistic-cache-swr-776
Open

feat(cache): implement stale-while-revalidate in OptimisticCache (#776)#819
ZacLou wants to merge 1 commit into
Stellar-split:mainfrom
ZacLou:feat/optimistic-cache-swr-776

Conversation

@ZacLou

@ZacLou ZacLou commented Sep 5, 2026

Copy link
Copy Markdown

Adds stale-while-revalidate (SWR) support to OptimisticCache so UIs never block on a TTL boundary.

Changes

  • New options on OptimisticCacheOptions:
    • staleWhileRevalidateMs — time window before expiry where a value is considered stale but still served (default 0, i.e. disabled).
    • revalidate — async callback invoked in the background to fetch a fresh value.
  • SWR behaviour:
    • When a get() hits a stale entry, the stale value is returned immediately.
    • A background revalidation is started (debounced to one per key).
    • On success the fresh value is written into the base cache.
    • On failure a revalidateError event is emitted; the caller is unaffected.
  • SimpleCache additions (src/cache.ts):
    • peek(key) — read raw entry metadata without evicting or counting hits.
    • resolveTtl(method) — return the configured TTL for a method prefix.
    • enabledState getter.
  • Tests: test/optimisticCacheSwr.test.ts covers stale detection, non-stale skipping, disabled SWR, concurrency guard, error emission, and post-error staleness.

Closes #776

…llar-split#776)

- Add OptimisticCacheOptions with staleWhileRevalidateMs (default 0)
- Add optional revalidate callback for background refresh
- Return stale values immediately while triggering background revalidation
- Prevent concurrent revalidations per key via in-flight tracking
- Emit revalidateError events when background refresh fails
- Add peek(), resolveTtl(), and enabledState to SimpleCache for SWR support
- Add unit tests for stale detection, concurrency guard, and error emission

Closes Stellar-split#776
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement stale-while-revalidate strategy in OptimisticCache

1 participant