Cache Utility API reference for the bounded Map-like cache utility database API Reference
Categories

Cache Utility

The createCache utility produces a bounded, Map-like cache with pluggable eviction. It mirrors the ES Map API and automatically removes entries when the configured maxSize is reached, using the eviction strategy you pick at creation time.

Reach for createCache when you want memoization-style storage but need an upper bound on memory — typical uses include caching compiled templates, parsed AST nodes, and hot function results.

createCache

createCache({ maxSize, eviction = 'lru', onEvict })

Creates a new bounded cache.

Parameters

NameTypeDescription
optionsobjectRequired options bag
Options
NameTypeDefaultDescription
maxSizenumberRequired. Non-negative integer. Maximum entries the cache holds before eviction. A maxSize of 0 accepts nothing.
evictionstring'lru'Eviction strategy: 'lru', 'fifo', or 'flush'.
onEvictfunctionCallback invoked as (key, value) for every evicted entry. Not fired on in-place updates to existing keys.

Eviction Strategies

StrategyBehavior
'lru'Evicts the least recently used entry. get() refreshes recency; so does re-set() on an existing key.
'fifo'Evicts the oldest inserted entry. Reads do not affect order. Updating an existing key does not change its position.
'flush'Clears the entire cache when full, then stores the new entry. Cheapest per-write; best when entries are quick to rebuild and bulk invalidation is acceptable.

Methods

MethodReturnsDescription
get(key)value | undefinedRetrieves a value. For LRU caches, refreshes recency.
set(key, value)cacheStores a value. Evicts per strategy if full. Chainable.
has(key)booleanChecks for key presence without affecting recency.
delete(key)booleanRemoves an entry. Returns true if present.
clear()voidRemoves every entry. Fires onEvict for each.
evict()voidManually evict one entry per strategy.
keys()iteratorKeys in insertion order.
values()iteratorValues in insertion order.
entries()iterator[key, value] pairs in insertion order.
forEach(fn, thisArg?)voidInvokes fn(value, key, cache) for each entry.
[Symbol.iterator]()iteratorEnables for..of and spread.

Properties

NameTypeDescription
sizenumberCurrent entry count.
maxSizenumberConfigured limit.
evictionstringConfigured strategy.
onEvictfunction | undefinedConfigured callback.

Example

import { createCache } from '@semantic-ui/utils';
// LRU (default) — reads protect entries from eviction
const lru = createCache({ maxSize: 500 });
lru.set('user:1', { name: 'Alice' });
lru.get('user:1'); // refreshes recency
// FIFO — insertion order, reads do not matter
const fifo = createCache({ maxSize: 100, eviction: 'fifo' });
// Flush — clear everything when full (fast bulk invalidation)
const templates = createCache({
maxSize: 5000,
eviction: 'flush',
onEvict: (key) => console.debug('evicted', key),
});
// Map-like iteration
for (const [key, value] of lru) {
console.log(key, value);
}
Previous
Browser
Next
Cloning