Skip to main content

Overview

Kweri’s EvictionEngine periodically sweeps the cache and removes entries that are no longer needed. An entry is eligible for eviction when:
  1. It has no active observers (no components are subscribed to it), AND
  2. Its cache lifetime has expired (now > updatedAt + cacheTime) — or it was never populated (updatedAt === 0)
Entries that still have subscribers are never evicted, regardless of age.

Automatic GC (default)

In the browser, GC runs automatically — you don’t need to configure anything for cacheTime to be honored. The automatic sweeper is designed to cost nothing when idle:
  • Self-stopping — it only runs a timer while there are entries to collect, and stops once the cache empties (re-arming on the next write).
  • Visibility-aware — it pauses while the tab is hidden and resumes on focus.
  • SSR-safe — on the server (no document) the automatic sweeper is not started. A server-side instance is request-scoped and collected wholesale, so no per-request timer leaks. (Still call kweri.destroy() when a request ends if you set an explicit gcInterval.)

Explicit fixed-interval GC

Pass gcInterval to run a sweep on a fixed cadence instead of the automatic sweeper:

Manual control

isEligibleForEviction

The core eviction predicate is exported if you need to implement custom eviction logic:
An entry is eligible when:

Custom timer (testing)

For unit tests, inject a TimerAdapter to control time:

Memory model

If your app navigates frequently between views, set a generous cacheTime (5–10 minutes). The automatic sweeper keeps recently-visited data warm for instant navigation while still reclaiming memory for old queries — no gcInterval required.