Cache API Reference
This document provides a detailed API reference for the LRUMemoryCache
class — an in-memory Least Recently Used (LRU) cache implementation of the Cache
interface.
Interfaces
Cache
Interface defining the contract for a cache implementation.
-
get<CacheValue>(key: string): Promise<CacheValue | null>
-
set<CacheValue>(key: string, value: CacheValue, expiresInSeconds?: number): Promise<void>
-
remove(key: string): Promise<void>
-
clear(): Promise<void>
-
cleanupExpired(): Promise<void>
-
getAll<CacheValue>(): Promise<KeyValue<CacheValue>[]>
Class: LRUMemoryCache
Implements an in-memory LRU cache with a configurable maximum size. Least recently used items are evicted when the cache reaches its limit.
constructor
constructor(maxSize: number = 10000)
Creates a new instance of LRUMemoryCache
.
-
maxSize
(optional, number): Maximum number of entries the cache can hold. Defaults to 10000.
get
get<CacheValue>(key: string): Promise<CacheValue | null>
Retrieves a value from the cache by its key. If the item is found, it is refreshed to be the most recently used. Returns null
if the item does not exist or is expired.
-
key
(string, required): The cache key to retrieve.
-
A promise resolving to the cached value or
null
if absent or expired.
set
set<CacheValue>(key: string, value: CacheValue, expiresInSeconds?: number): Promise<void>
Adds a value to the cache associated with the given key. If the cache is full, the least recently used item is evicted. Optionally, an expiration time can be set.
-
key
(string, required): The cache key. -
value
(CacheValue
, required): The value to cache. -
expiresInSeconds
(number, optional): Number of seconds before the cache entry expires.
-
A promise that resolves when the value has been cached.
remove
remove(key: string): Promise<void>
Removes the cache entry for the specified key.
-
key
(string, required): The cache key to remove.
-
A promise resolving when the entry is removed.
clear
clear(): Promise<void>
Clears all entries from the cache.
-
A promise resolving when the cache has been cleared.