Documentation Index
Fetch the complete documentation index at: https://bytekit.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
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
| Property | Type | Default | Description |
|---|---|---|---|
defaultTTL | number | 60000 | Default time-to-live in ms. |
maxSize | number | 1000 | Maximum number of entries before LRU eviction. |
maxMemorySize | number | — | Optional memory budget in bytes (approximate). |
enableLocalStorage | boolean | false | Persist cache entries to localStorage. |
storagePrefix | string | — | Key prefix when enableLocalStorage is true. |
Factory
CacheManager instance.
Methods
get<T>(key)
undefined if the key is missing or expired.
set<T>(key, value, options?)
ttl overrides defaultTTL.
| Parameter | Type | Description |
|---|---|---|
key | string | Cache key. |
value | T | Value to store. |
options | { ttl?: number } | Optional per-entry TTL in ms. |
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()
| Property | Type | Description |
|---|---|---|
hits | number | Total cache hits. |
misses | number | Total cache misses. |
size | number | Current entry count. |
hitRate | number | Ratio of hits to total lookups (0–1). |
memoryUsage | number | Approximate memory usage in bytes. |
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.