Import
What it does
CacheManager provides a generic in-memory cache with time-to-live (TTL) expiration, LRU eviction when maxSize is reached, optional localStorage persistence, and built-in hit/miss statistics. Use it to cache expensive computations, API responses, or any data that benefits from short-lived memoization.
Constructor
CacheManagerConfig
Factory
CacheManager instance.
Methods
get<T>(key)
undefined if the key is missing or expired.
set<T>(key, value, options?)
ttl overrides defaultTTL.
getOrCompute<T>(key, fn, options?)
fn, caches the result, and returns it. Useful for stale-while-revalidate patterns.
delete(key)
true if the key was found.
has(key)
true if the key exists and has not expired.
clear()
size()
keys()
stats()
Examples
Basic TTL cache
Compute-on-miss pattern
Monitoring cache performance
When
maxSize is reached, the least-recently-used entry is evicted to make room for new ones. This ensures the cache stays within its configured bounds.