Skip to content

Implement stale-while-revalidate strategy in OptimisticCache #776

Description

@Kingsman-99

Description

OptimisticCache returns cached values until TTL expiry and then blocks on a fresh fetch. Under stale-while-revalidate, the stale value is returned immediately while a background refresh is triggered, eliminating latency spikes at TTL boundaries.

Acceptance Criteria

  • OptimisticCacheOptions gains staleWhileRevalidateMs (default 0, meaning disabled)
  • When a cached value is within staleWhileRevalidateMs of its TTL, it is returned immediately and a background revalidation is started
  • Background errors do not propagate to the caller; they are emitted as a revalidateError event
  • Unit tests pass

Context

  • Target file: src/cache/OptimisticCache.ts
  • Background revalidation should not run more than once concurrently per cache key

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions