import type { CacheOptions, CacheStats } from './interfaces'; import { CacheLayer } from './interfaces'; import { ClsCacheProvider, LRUCacheProvider, RedisCacheProvider } from './providers'; import { CacheSerializationService } from './cache-serialization.service'; /** * Unified cache service with three-tier architecture * * L1: CLS (request-level) * L2: LRU (process-level with automatic eviction) * L3: Redis (distributed) * * Supports automatic fallback, backfill, and dependency-based invalidation. */ export declare class CacheService { private readonly clsProvider; private readonly lruProvider; private readonly redisProvider; private readonly enableCompression; private readonly compressionThreshold; private readonly serializationService; private readonly logger; private readonly providers; private readonly stats; constructor(clsProvider: ClsCacheProvider, lruProvider: LRUCacheProvider, redisProvider: RedisCacheProvider, enableCompression?: boolean, compressionThreshold?: number, serializationService?: CacheSerializationService); /** * Get or set cache value with factory function * * @param key - Cache key * @param factory - Function to generate value if not cached * @param options - Cache options * @returns Cached or generated value */ getOrSet(key: string, factory: () => Promise, options?: CacheOptions): Promise; /** * Get value from cache with optional layer specification * * @param key - Cache key * @param options - Cache options including layer preference * @returns Cached value or null * * @example * ```typescript * // Try memory only * const data = await this.cacheService.get('user:123', { * layers: [CacheLayer.MEMORY] * }); * * // Try all layers in order (CLS -> MEMORY -> REDIS) * const data = await this.cacheService.get('config:app', { * layers: [CacheLayer.CLS, CacheLayer.MEMORY, CacheLayer.REDIS] * }); * ``` */ get(key: string, options?: CacheOptions): Promise; /** * Set value in cache with optional layer specification * * Supports both new API (CacheOptions) and legacy API (TTL number) for backward compatibility. * * @param key - Cache key * @param value - Value to cache * @param options - Cache options including layer selection OR TTL (number) for legacy compatibility * * @example * ```typescript * // New API - Cache in memory only * await this.cacheService.set('user:123', userData, { * layers: [CacheLayer.MEMORY], * ttl: 300000 * }); * * // Legacy API - TTL as third parameter (still works for backward compatibility) * await this.cacheService.set('user:123', userData, 300000); * * // Cache in all layers (default behavior) * await this.cacheService.set('config:app', configData, { * layers: [CacheLayer.CLS, CacheLayer.MEMORY, CacheLayer.REDIS], * ttl: 3600000 * }); * * // Cache in Redis only (distributed) * await this.cacheService.set('global:settings', settings, { * layers: [CacheLayer.REDIS], * ttl: 86400000 * }); * ``` */ set(key: string, value: T, options?: CacheOptions | number): Promise; /** * Delete value from cache * * @param key - Cache key or array of keys * @param layers - Specific layers to delete from (default: all) */ del(key: string | string[], layers?: CacheLayer[]): Promise; /** * Delete keys matching pattern * * @param pattern - Pattern to match (e.g., 'user:*') * @param layers - Specific layers to delete from (default: Memory and Redis only) */ deletePattern(pattern: string, layers?: CacheLayer[]): Promise; /** * Clear all cache * * @param layers - Specific layers to clear (default: all) */ clear(layers?: CacheLayer[]): Promise; /** * Invalidate tags (for TagDependency) * * @param tags - Tags to invalidate */ invalidateTags(tags: string[]): Promise; /** * Get multiple values * * @param keys - Array of cache keys * @param options - Cache options * @returns Array of values (null for missing keys) */ mget(keys: string[], options?: CacheOptions): Promise<(T | null)[]>; /** * Set multiple values * * @param items - Array of key-value pairs * @param options - Cache options */ mset(items: Array<{ key: string; value: any; }>, options?: CacheOptions): Promise; /** * Get cache statistics * * @returns Cache statistics */ getStats(): CacheStats; /** * Reset statistics */ resetStats(): void; /** * Internal method to set cache value with options */ private setWithOptions; /** * Resolve cache layers from options */ private resolveLayers; /** * Get default cache layers */ private getDefaultLayers; /** * Build full cache key with namespace */ private buildKey; /** * Backfill upper cache layers */ private backfillUpperLayers; /** * Record cache hit */ private recordHit; /** * Record cache miss */ private recordMiss; /** * Update hit rate for a layer */ private updateLayerHitRate; /** * Initialize statistics for all layers */ private initializeStats; }