API reference › @evolu/common › Cache › createLruCache
function createLruCache<K, V>(
capacity: number &
Brand<"NonNaN"> &
Brand<"Finite"> &
Brand<"Int"> &
Brand<"NonNegative"> &
Brand<"Positive">,
): Cache<K, V>;
Defined in: packages/common/src/Cache.ts:63
Creates an LRU (least recently used) cache with a maximum capacity.
When the cache reaches capacity, the least recently used entry is evicted.
Both get and set operations update the access order.
Example
import {
assertEqual,
assertFalse,
createLruCache,
PositiveInt,
} from "@evolu/common";
const cache = createLruCache<string, number>(PositiveInt.orThrow(2));
cache.set("a", 1);
cache.set("b", 2);
// Reading "a" makes "b" the least recently used entry.
assertEqual(cache.get("a"), 1);
cache.set("c", 3);
assertFalse(cache.has("b"));