Skip to content

getAllOrElse

ts
function Cache.getAllOrElse(
    cache: Cache<K, V>,
    keys: Iterable<K>,
    orElse: (cache: Cache<K, V>, key: K) => T
): (V | T)[]

Gets multiple values from the cache by keys, calling the fallback function for each missing key. Useful for lazy computation or setting default values in the cache.

Example

ts
import { Cache } from "@monstermann/cache";

const cache = Cache.Map<string, number>();

Cache.set(cache, "foo", 42);
Cache.set(cache, "bar", 100);

Cache.getAllOrElse(cache, ["foo", "bar", "baz"], () => 0); // [42, 100, 0]

// Set default values for missing keys
Cache.getAllOrElse(cache, ["foo", "qux", "quux"], (c, key) => {
    return Cache.set(c, key, key.length);
}); // [42, 3, 4]