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.

Methods
  • 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>[]>

KeyValue<CacheValue>

Represents a cache entry key and its associated value.

Parameters
  • key (string): The cache key.

  • value (CacheValue): The cached value.

CacheEntry<CacheValue>

Represents an internal cache entry with its value and optional expiration timestamp.

Parameters
  • value (CacheValue): The cached value.

  • expiresAt (optional, number): Timestamp in milliseconds when the cache entry expires.

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.

Parameters
  • 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.

Parameters
  • key (string, required): The cache key to retrieve.

Returns
  • 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.

Parameters
  • key (string, required): The cache key.

  • value (CacheValue, required): The value to cache.

  • expiresInSeconds (number, optional): Number of seconds before the cache entry expires.

Returns
  • A promise that resolves when the value has been cached.

remove

remove(key: string): Promise<void>

Removes the cache entry for the specified key.

Parameters
  • key (string, required): The cache key to remove.

Returns
  • A promise resolving when the entry is removed.

clear

clear(): Promise<void>

Clears all entries from the cache.

Returns
  • A promise resolving when the cache has been cleared.

cleanupExpired

cleanupExpired(): Promise<void>

Removes all expired entries from the cache.

Returns
  • A promise resolving when expired entries have been cleaned up.

getAll

getAll<CacheValue>(): Promise<KeyValue<CacheValue>[]>

Retrieves all cache entries as an array of key-value pairs.

Returns
  • A promise resolving to an array of KeyValue objects representing all cached entries.