Skip to content

sieveCache<K, V>(options): Cache<K, V>

Defined in: packages/cache/src/sieve-cache.ts:91

Create a Cache that evicts with SIEVE (Zhang et al., NSDI ’24).

Each entry carries a visited bit that get sets. A hand walks from the oldest entry toward the newest, clearing set bits, and evicts the first entry whose bit is clear; it stays where it stopped for the next eviction. An entry read since the hand last passed it therefore survives one more pass, and new entries never move older ones.

Choose it as the default for skewed traffic, where a minority of keys takes most reads. During a one-pass scan of keys never read again, the hand clears each read entry once and then chases the scanned keys toward the head, evicting them rather than the read entries behind it; but when read entries fill all but one slot, the hand walks off the head, wraps to the tail, and the scan pushes out every entry not read during it. get and a set of a key already held count as uses; peek and has do not.

get, peek, has, and delete run in O(1) average time. set runs in O(1) amortized time per entry it evicts: the hand clears each visited bit at most once per set. Iteration is O(n) over every slot allocated since the last clear.

K

V

CacheOptions<K, V>

Cache<K, V>

With INVALID_CAPACITY when capacity is not a positive safe integer (or, with weigh, not a positive finite number), MISSING_CLOCK when timeToLive comes without clock, or INVALID_TIME_TO_LIVE when timeToLive is not a positive finite number.

const cache = sieveCache<string, number>({ capacity: 2 })
cache.set('a', 1)
cache.set('b', 2)
cache.get('a')
cache.set('c', 3) // [{ key: 'b', value: 2 }]