Class: TypedCache<K, V>
Defined in: src/utils/cache.ts:131
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:174
Create a new cache instance.
Parametersโ
optionsโ
CacheOptions
= {}
Optional cache configuration.
Returnsโ
TypedCache
<K
, V
>
Default Valueโ
An instance will use maxSize
100 and no default TTL when omitted.
Propertiesโ
cacheโ
private
readonly
cache:Map
<K
,CacheEntry
<V
>>
Defined in: src/utils/cache.ts:141
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.
defaultTtlโ
private
readonly
defaultTtl:undefined
|number
Defined in: src/utils/cache.ts:153
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โ
private
readonly
maxSize:number
Defined in: src/utils/cache.ts:165
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:184
Current number of entries in the cache.
Returnsโ
number
Number of stored entries.
Methodsโ
cleanup()โ
cleanup():
void
Defined in: src/utils/cache.ts:199
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:215
Remove all entries from the cache.
Returnsโ
void
Remarksโ
Clears the internal Map and resets the cache to empty.
delete()โ
delete(
key
:K
):boolean
Defined in: src/utils/cache.ts:226
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
):undefined
|V
Defined in: src/utils/cache.ts:238
Retrieve a value if present and not expired.
Parametersโ
keyโ
K
The cache key to look up.
Returnsโ
undefined
| V
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:266
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:284
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.