/** * ThreadTS Universal - Cache Utilities * * Shared cache implementations for decorators. * Follows the DRY principle by extracting common cache logic. * * @module utils/cache * @author ThreadTS Universal Team */ /** * Cache entry containing a value and an optional expiration timestamp. * * @template T - Type of the cached value */ export interface CacheEntry { /** Cached value */ value: T; /** Expiration timestamp in milliseconds (optional) */ expiry?: number; } /** * Configuration for the LRU cache. */ export interface LRUCacheConfig { /** Maximum number of entries in the cache */ maxSize: number; /** Time-to-live in milliseconds (optional, 0 = no expiration) */ ttlMs?: number; } /** * LRU (Least Recently Used) Cache Implementierung. * * Features: * - Bounded size with automatic eviction * - Optional TTL (time-to-live) for entries * - Efficient O(1) operations via a Map-based implementation * * @template K - Key type * @template V - Value type * * @example * ```typescript * const cache = new LRUCache({ maxSize: 100, ttlMs: 60000 }); * cache.set('key1', 42); * const value = cache.get('key1'); // 42 * ``` */ export declare class LRUCache { /** Internal map for cache entries */ private readonly cache; /** Maximum cache size */ private readonly maxSize; /** Time-to-live in milliseconds (0 = no expiration) */ private readonly ttlMs; /** * Creates a new LRU cache. * * @param config - Cache configuration */ constructor(config: LRUCacheConfig); /** * Retrieves a value from the cache. * * Updates the LRU order (most recently used -> end of the Map). * Returns undefined if the key does not exist or has expired. * * @param key - The key * @returns The cached value or undefined */ get(key: K): V | undefined; /** * Stores a value in the cache. * * When reaching maximum capacity, the oldest entry is evicted. * * @param key - The key * @param value - The value to store */ set(key: K, value: V): void; /** * Checks whether a key exists in the cache and is still valid. * * @param key - The key * @returns true if the key exists and has not expired */ has(key: K): boolean; /** * Removes an entry from the cache. * * @param key - The key to remove * @returns true if the key existed and was removed */ delete(key: K): boolean; /** * Clears the entire cache. */ clear(): void; /** * Returns the current number of entries. * * @returns Number of entries in the cache */ get size(): number; /** * Removes all expired entries from the cache. * * This method is not called automatically to avoid impacting get/set * performance. Call it manually if needed (e.g. periodically). * * @returns Number of removed entries */ prune(): number; /** * Returns cache statistics. * * @returns Cache statistics object */ stats(): CacheStats; } /** * Cache statistics. */ export interface CacheStats { /** Current number of entries */ size: number; /** Maximum number of entries */ maxSize: number; /** Time-to-live in milliseconds */ ttlMs: number; } /** * Creates a string cache key from function arguments. * * Uses JSON.stringify for serializable arguments. * Warning: functions and circular references are not supported. * * @param args - Arguments to serialize * @returns A string key for the cache * * @example * ```typescript * const key = createCacheKey([1, 'test', { a: 1 }]); * // '[[1,"test",{"a":1}]]' or similar * ``` */ export declare function createCacheKey(args: unknown[]): string; /** * Lazy initialization state. * * Manages state for one-time initialization with support for concurrent * callers while initialization is in progress. * * @template T - Type of the initialized value */ export declare class LazyInitializer { /** Initialized value */ private value; /** Whether initialization has completed */ private initialized; /** Promise for ongoing initialization (prevents race conditions) */ private initializing; /** * Returns the value, initializing it if needed. * * If called concurrently while initialization is in progress, the same * Promise is returned to prevent duplicate initialization. * * @param initializer - Function that initializes the value * @returns Promise resolving to the initialized value */ get(initializer: () => Promise): Promise; /** * Resets the lazy state. * * After reset, the next call to get() will initialize again. */ reset(): void; /** * Checks whether the value has already been initialized. * * @returns true if initialized */ isInitialized(): boolean; } //# sourceMappingURL=cache.d.ts.map