import { StorageBackend } from './storage'; import { Logger } from '../utils/logger'; export interface CacheEntry { value: T; expires: number; staleExpires: number; } export interface CacheStats { hits: number; misses: number; staleHits: number; size: number; } export interface CacheOptions { storage?: StorageBackend; staleTTL?: number; onRevalidate?: (key: string) => Promise; logger?: Logger; /** * Called when cache operations fail * Allows custom error handling without throwing */ onError?: (error: Error, operation: string, context?: Record) => void; /** * Enable automatic pruning of expired entries * Default: true */ autoPrune?: boolean; /** * Interval for automatic pruning in milliseconds * Default: DEFAULT_STALE_TTL (1 minute) */ pruneInterval?: number; /** * Maximum cache size before forcing eviction * Default: undefined (no limit) */ maxSize?: number; } /** * CacheManager provides TTL-based caching with stale-while-revalidate strategy * * Features: * - TTL-based expiration * - Stale-while-revalidate pattern for better performance * - Multiple storage backends (memory, localStorage) * - Cache statistics tracking * - Browser and Node.js compatible */ export declare class CacheManager { private storage; private staleTTL; private stats; private revalidationCallbacks; private pendingRevalidations; private revalidationControllers; private logger; private onError?; private disposed; private options; private pruneTimer?; constructor(options?: CacheOptions); /** * Handle errors consistently across all cache operations */ private handleError; /** * Get cached value * Returns fresh data if available, or stale data while triggering background revalidation */ get(key: string): Promise; /** * Set cached value with TTL * @param key - Cache key * @param value - Value to cache * @param ttl - Time to live in milliseconds */ set(key: string, value: T, ttl: number): Promise; /** * Invalidate specific cache entry */ invalidate(key: string): Promise; /** * Clear all cache entries */ clear(): Promise; /** * Get cache statistics */ getStats(): CacheStats; /** * Register a revalidation callback for a specific key pattern * @param keyPattern - Key or pattern (* for all keys) * @param callback - Async function to fetch fresh data */ onRevalidate(keyPattern: string, callback: (key: string) => Promise): void; /** * Remove revalidation callback */ offRevalidate(keyPattern: string): void; /** * Trigger background revalidation for a key * @private */ private revalidate; /** * Update cache size stat * @private */ private updateSize; /** * Check if a key exists in cache (including stale entries) */ has(key: string): Promise; /** * Get all cache keys */ keys(): Promise; /** * Prune expired entries from cache */ prune(): Promise; /** * Get or set pattern: fetch from cache or execute callback and cache result */ getOrSet(key: string, fetcher: () => Promise, ttl: number): Promise; /** * Gets current cache statistics including size */ getStatsWithSize(): Promise; /** * Gets cache size information */ getSizeInfo(): Promise<{ entryCount: number; maxSize?: number; utilizationPercent?: number; }>; /** * Starts automatic pruning */ private startAutoPrune; /** * Stops automatic pruning */ private stopAutoPrune; /** * Evicts one entry (oldest expiration) */ private evictOne; /** * Ensures cache is not disposed */ private ensureNotDisposed; /** * Disposes the cache manager and releases all resources * * This method should be called when the cache is no longer needed, * typically when shutting down the application or cleaning up a component. * * After calling dispose(): * - All pending operations are cancelled * - All callbacks are cleared * - All cached data is removed * - Further operations will throw an error * * @example * ```typescript * const cache = new CacheManager() * // ... use cache * await cache.dispose() // Clean up when done * ``` */ dispose(): Promise; } //# sourceMappingURL=manager.d.ts.map