Class: TypedCache<K, V>
Defined in: src/utils/cache.ts:141
Generic in-memory cache with optional TTL and LRU eviction.
Remarksโ
The implementation uses a Map to store entries and maintains
lastAccessed timestamps for LRU eviction. When maxSize is reached a
single entry is evicted (the one with the oldest lastAccessed). This
selection is O(n) over the number of entries.
Type Parametersโ
Kโ
K
Type of cache keys.
Vโ
V
Type of cached values.
Constructorsโ
Constructorโ
new TypedCache<
K,V>(options?:CacheOptions):TypedCache<K,V>
Defined in: src/utils/cache.ts:195
Create a new cache instance.
Parametersโ
options?โ
CacheOptions = {}
Cache configuration. Accepts either CacheOptions or CACHE_CONFIG format.
Returnsโ
TypedCache<K, V>
Default Valueโ
An instance will use maxSize 100 and no default TTL when omitted.
Propertiesโ
cacheโ
privatereadonlycache:Map<K,CacheEntry<V>>
Defined in: src/utils/cache.ts:151
Internal
Internal Map storing cache entries with metadata.
Remarksโ
This map contains the actual cached data along with timing metadata used for TTL expiration and LRU eviction calculations.
generationโ
privategeneration:number=0
Defined in: src/utils/cache.ts:161
Cache generation counter.
Remarksโ
Incremented whenever the cache is cleared. Used by higher-level helpers (for example getCachedOrFetch) to avoid caching values fetched before an invalidation/clear event.
defaultTtlโ
privatereadonlydefaultTtl:number|undefined
Defined in: src/utils/cache.ts:173
Internal
Default time-to-live for cache entries in milliseconds.
Remarksโ
When set, all entries without an explicit TTL will expire after this duration. If undefined, entries persist until manually evicted or removed by LRU eviction.
maxSizeโ
privatereadonlymaxSize:number
Defined in: src/utils/cache.ts:185
Internal
Maximum number of entries allowed in the cache.
Remarksโ
When this limit is reached, the least recently used entry is evicted to make room for new entries. Defaults to 100 if not specified in constructor options.
Accessorsโ
sizeโ
Get Signatureโ
get size():
number
Defined in: src/utils/cache.ts:219
Current number of entries in the cache.
Returnsโ
number
Number of stored entries.
Methodsโ
cleanup()โ
cleanup():
void
Defined in: src/utils/cache.ts:234
Remove entries that have exceeded their TTL from this cache instance.
Returnsโ
void
Remarksโ
Iterates the underlying Map and deletes expired items. This is a best-effort maintenance operation and safe to call periodically (for example from a scheduled timer). It does not return which keys were removed.
clear()โ
clear():
void
Defined in: src/utils/cache.ts:252
Remove all entries from the cache.
Returnsโ
void
Remarksโ
Clears the internal Map and resets the cache to empty.
getGeneration()โ
getGeneration():
number
Defined in: src/utils/cache.ts:264
Returns the current cache generation.
Returnsโ
number
Remarksโ
Consumers can capture this value before starting a long-running fetch and ensure the cache has not been invalidated before storing results.
delete()โ
delete(
key:K):boolean
Defined in: src/utils/cache.ts:277
Delete a specific key from the cache.
Parametersโ
keyโ
K
The cache key to remove.
Returnsโ
boolean
True if the key existed and was removed; false otherwise.
get()โ
get(
key:K):V|undefined
Defined in: src/utils/cache.ts:291
Retrieve a value if present and not expired.
Parametersโ
keyโ
K
The cache key to look up.
Returnsโ
V | undefined
The cached value, or undefined when the key does not exist or
the entry has expired.
has()โ
has(
key:K):boolean
Defined in: src/utils/cache.ts:321
Check whether a non-expired entry exists for the given key.
Parametersโ
keyโ
K
The cache key to test.
Returnsโ
boolean
true when a valid (non-expired) value exists; false
otherwise.
set()โ
set(
key:K,value:V,ttl?:number):void
Defined in: src/utils/cache.ts:341
Insert or update an entry in the cache and optionally specify a per-entry TTL that overrides the cache's default TTL.
Parametersโ
keyโ
K
The cache key to set.
valueโ
V
The value to store under the provided key.
ttl?โ
number
Optional per-entry TTL in milliseconds; when omitted the
cache's defaultTtl (if any) will apply.
Returnsโ
void
Remarksโ
If the cache size would exceed CacheOptions.maxSize a single least-recently-used entry is evicted. LRU selection scans all entries and has O(n) cost.