UNPKG

@catbee/utils

Version:

A modular, production-grade utility toolkit for Node.js and TypeScript, designed for robust, scalable applications (including Express-based services). All utilities are tree-shakable and can be imported independently.

153 lines 4.67 kB
/** * Options for configuring a TTLCache instance. */ export interface TTLCacheOptions { /** Default time-to-live in milliseconds for cache entries */ ttlMs?: number; /** Maximum number of entries to keep in cache (uses LRU eviction policy) */ maxSize?: number; /** Auto-cleanup interval in milliseconds (disabled if 0 or negative) */ autoCleanupMs?: number; } /** * An in-memory cache with time-to-live (TTL) support for each entry. * * @typeParam K - Type of the key. * @typeParam V - Type of the value. * * @example * const cache = new TTLCache<string, number>({ ttlMs: 1000 }); * cache.set("x", 123); * const value = cache.get("x"); // 123 */ export declare class TTLCache<K, V> { private cache; private ttlMs; private maxSize?; private cleanupInterval?; /** * @param options - Configuration options for the cache */ constructor(options?: TTLCacheOptions); /** * Sets a key-value pair in the cache with the default TTL. * * @param key - The key to set. * @param value - The value to associate with the key. */ set(key: K, value: V): void; /** * Sets a key-value pair in the cache with a custom TTL. * * @param key - The key to set. * @param value - The value to associate with the key. * @param ttlMs - Time-to-live in milliseconds. */ setWithTTL(key: K, value: V, ttlMs: number): void; /** * Retrieves the value for a given key if it hasn't expired. * * @param key - The key to retrieve. * @returns The cached value, or undefined if not found or expired. */ get(key: K): V | undefined; /** * Retrieves or computes a value if it's not in the cache or has expired. * * @param key - The key to retrieve * @param producer - Function to generate the value if not cached * @param ttlMs - Optional custom TTL for the computed value * @returns The cached or computed value */ getOrCompute(key: K, producer: () => Promise<V>, ttlMs?: number): Promise<V>; /** * Checks if the key exists and hasn't expired. * * @param key - The key to check. * @returns `true` if key exists and is valid, otherwise `false`. */ has(key: K): boolean; /** * Deletes a key from the cache. * * @param key - The key to delete. * @returns `true` if the key existed and was removed, `false` otherwise. */ delete(key: K): boolean; /** * Clears all entries from the cache. */ clear(): void; /** * Returns the number of entries currently in the cache (includes expired entries until next access/cleanup). * * @returns Number of items in the cache (may include expired keys). */ size(): number; /** * Set multiple key-value pairs at once with the default TTL. * * @param entries - Array of [key, value] tuples to set */ setMany(entries: [K, V][]): void; /** * Get multiple values at once. * * @param keys - Array of keys to retrieve * @returns Array of values (undefined for keys that don't exist or expired) */ getMany(keys: K[]): (V | undefined)[]; /** * Removes all expired entries from the cache. * * @returns Number of entries removed. */ cleanup(): number; /** * Returns an iterator of all current valid [key, value] pairs. * * @returns IterableIterator<[K, V]> */ entries(): IterableIterator<[K, V]>; /** * Returns an iterator of all current valid keys. * * @returns IterableIterator<K> */ keys(): IterableIterator<K>; /** * Returns an iterator of all current valid values. * * @returns IterableIterator<V> */ values(): IterableIterator<V>; /** * Extends the expiration of a key by the specified time or default TTL. * * @param key - The key to refresh * @param ttlMs - Optional new TTL in milliseconds (uses default if not specified) * @returns true if the key was found and refreshed, false otherwise */ refresh(key: K, ttlMs?: number): boolean; /** * Returns a snapshot of cache stats. * * @returns Object containing cache statistics */ stats(): { size: number; validEntries: number; expiredEntries: number; maxSize: number | undefined; }; /** * Stop the auto-cleanup interval if it's running. */ destroy(): void; /** * Evict the least recently used entry from the cache. * @private */ private evictLRU; } //# sourceMappingURL=cache.utils.d.ts.map