Skip to content

Cap a cache by entry count

A SIEVE cache keeps at most capacity entries and returns what it evicted from every set.

import {
function sieveCache<K, V>(options: CacheOptions<K, V>): Cache<K, V>

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.

@throws ― {CacheInvalidInputError} 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.

@example

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 }]

sieveCache
} from 'knotkit/cache'
const
const cache: Cache<string, number>
cache
=
sieveCache<string, number>(options: CacheOptions<string, number>): Cache<string, number>

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.

@throws ― {CacheInvalidInputError} 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.

@example

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 }]

sieveCache
<string, number>({
CacheOptions<string, number>.capacity: number

The bound the cache keeps itself within. Without

weigh

, it counts entries and must be a positive safe integer. With

weigh

, it bounds the exact sum of the supplied binary floating-point weights and may be any positive finite number. There is no rounding tolerance at the bound.

capacity
: 2 })
const cache: Cache<string, number>
cache
.
Cache<string, number>.set: (key: string, value: number) => readonly CacheEntry<string, number>[]

Store the value under the key, replacing any value already there, after evicting what the policy chooses until the entry fits. The key just set is never evicted by its own call.

@returns ― The entries evicted to make room, in eviction order. Expired entries removed along the way are not reported. Empty when nothing was evicted.

@throws ― {CacheInvalidInputError} With code INVALID_VALUE when the value is undefined, INVALID_WEIGHT when weigh returns anything but a positive finite number, ENTRY_TOO_HEAVY when the entry alone outweighs capacity, or INVALID_CLOCK_READING when the injected clock returns a non-finite number. The cache is unchanged after any of these. A customCache also throws INVALID_VICTIM when its policy's chooseVictim names a key the cache does not hold or the key being set.

set
('a', 1) // []
const cache: Cache<string, number>
cache
.
Cache<string, number>.set: (key: string, value: number) => readonly CacheEntry<string, number>[]

Store the value under the key, replacing any value already there, after evicting what the policy chooses until the entry fits. The key just set is never evicted by its own call.

@returns ― The entries evicted to make room, in eviction order. Expired entries removed along the way are not reported. Empty when nothing was evicted.

@throws ― {CacheInvalidInputError} With code INVALID_VALUE when the value is undefined, INVALID_WEIGHT when weigh returns anything but a positive finite number, ENTRY_TOO_HEAVY when the entry alone outweighs capacity, or INVALID_CLOCK_READING when the injected clock returns a non-finite number. The cache is unchanged after any of these. A customCache also throws INVALID_VICTIM when its policy's chooseVictim names a key the cache does not hold or the key being set.

set
('b', 2) // []
const cache: Cache<string, number>
cache
.
Cache<string, number>.get: (key: string) => number | undefined

Return the value stored under the key, counting the read as a use for the eviction policy, or undefined when the key is absent or expired.

@throws ― {CacheInvalidInputError} With code INVALID_CLOCK_READING when the injected clock returns a non-finite number.

get
('a') // 1
const cache: Cache<string, number>
cache
.
Cache<string, number>.set: (key: string, value: number) => readonly CacheEntry<string, number>[]

Store the value under the key, replacing any value already there, after evicting what the policy chooses until the entry fits. The key just set is never evicted by its own call.

@returns ― The entries evicted to make room, in eviction order. Expired entries removed along the way are not reported. Empty when nothing was evicted.

@throws ― {CacheInvalidInputError} With code INVALID_VALUE when the value is undefined, INVALID_WEIGHT when weigh returns anything but a positive finite number, ENTRY_TOO_HEAVY when the entry alone outweighs capacity, or INVALID_CLOCK_READING when the injected clock returns a non-finite number. The cache is unchanged after any of these. A customCache also throws INVALID_VICTIM when its policy's chooseVictim names a key the cache does not hold or the key being set.

set
('c', 3) // [{ key: 'b', value: 2 }], what was evicted to make room
const cache: Cache<string, number>
cache
.
Cache<string, number>.peek: (key: string) => number | undefined

Return the value stored under the key without counting the read as a use, or undefined when the key is absent or expired.

@throws ― {CacheInvalidInputError} With code INVALID_CLOCK_READING when the injected clock returns a non-finite number.

peek
('b') // undefined
const cache: Cache<string, number>
cache
.
Cache<string, number>.size: number

Number of entries held, including expired entries nothing has touched yet. O(1).

size
// 2

The same options also bound total weight with weigh, and expire entries with timeToLive and a clock you pass in. The module page compares SIEVE, LRU, and S3-FIFO on a scan-heavy trace.

Modules: knotkit/cache · API reference