/** * SMI-644: Unified Cache Manager * Provides high-level cache operations with: * - Popular query detection (dynamic TTL) * - Background refresh for hot entries * - Cache invalidation coordination * - Statistics and monitoring */ import type { SearchResult } from './lru.js'; import { TTLTier, POPULARITY_THRESHOLDS, getTTLTierName } from './CacheEntry.js'; import { type TieredCacheConfig, type TieredCacheStats } from './TieredCache.js'; /** * Search options for cache key generation */ export interface SearchOptions { query: string; filters?: Record; limit?: number; offset?: number; } /** * Refresh callback type */ export type RefreshCallback = (options: SearchOptions) => Promise<{ results: SearchResult[]; totalCount: number; }>; /** * Cache manager configuration */ export interface CacheManagerConfig extends TieredCacheConfig { /** Enable background refresh for hot entries (default: true) */ enableBackgroundRefresh?: boolean; /** Background refresh interval in ms (default: 30 seconds) */ refreshIntervalMs?: number; /** Maximum concurrent refreshes (default: 3) */ maxConcurrentRefreshes?: number; /** Callback to refresh cache entries */ refreshCallback?: RefreshCallback; } /** * Unified Cache Manager * Coordinates L1/L2 caching with intelligent TTL management */ export declare class CacheManager { private cache; private config; private queryFrequencies; private refreshTimer; /** SMI-683: Use Map for proper deduplication of concurrent refreshes */ private activeRefreshes; private refreshCallback; private lastInvalidation; private invalidationCallbacks; /** * @deprecated Use CacheManager.create(config) — async factory with WASM fallback. * This constructor always throws to prevent silent data loss. */ constructor(_config?: CacheManagerConfig); /** * Async factory — supports both native and WASM SQLite. * * @param config - Cache manager configuration * @returns Fully initialised CacheManager instance */ static create(config?: CacheManagerConfig): Promise; /** * Generate cache key from search options */ static generateKey(options: SearchOptions): string; /** * Parse search options from cache key * SMI-683: Fixed regex to handle empty filters (was: .+? requires at least 1 char) */ static parseKey(key: string): SearchOptions | null; /** * Get cached results for search options */ get(options: SearchOptions): { results: SearchResult[]; totalCount: number; } | undefined; /** * Get or compute cached results * @param options Search options * @param compute Function to compute results if not cached */ getOrCompute(options: SearchOptions, compute: () => Promise<{ results: SearchResult[]; totalCount: number; }>): Promise<{ results: SearchResult[]; totalCount: number; }>; /** * Store results in cache with automatic TTL detection */ set(options: SearchOptions, results: SearchResult[], totalCount: number): void; /** * Check if results are cached */ has(options: SearchOptions): boolean; /** * Delete specific cached result */ delete(options: SearchOptions): boolean; /** * Invalidate all cached results * Should be called when the skill index is updated */ invalidateAll(): void; /** * Register callback for invalidation events */ onInvalidate(callback: () => void): () => void; /** * Get time since last invalidation */ getTimeSinceInvalidation(): number; /** * Prune expired entries */ prune(): number; /** * Get comprehensive cache statistics */ getStats(): TieredCacheStats & { queryFrequencies: { popular: number; standard: number; rare: number; }; backgroundRefresh: { active: number; lastRun: number; }; }; /** * Get detailed hit rate by TTL tier */ getHitRateByTier(): Record; /** * Set the refresh callback for background refresh */ setRefreshCallback(callback: RefreshCallback): void; /** * Close cache manager and cleanup resources */ close(): void; /** * Record a hit for query frequency tracking */ private recordQueryHit; /** * Determine TTL tier based on query frequency */ private determineTTLTier; /** * Prune old query frequency entries */ private pruneQueryFrequencies; /** * Start background refresh loop */ private startBackgroundRefresh; /** * Perform background refresh for entries approaching expiration */ private performBackgroundRefresh; /** * Refresh a single cache entry * SMI-683: Fixed race condition by using Map> for proper deduplication. * Concurrent calls for the same key now return the same promise instance. */ private refreshEntry; } export { TTLTier, getTTLTierName, POPULARITY_THRESHOLDS }; //# sourceMappingURL=CacheManager.d.ts.map