/** * SMI-1303: Parse Result Cache * SMI-1337: Added metrics integration * * LRU cache for parse results with content hash validation. * Provides memory-based eviction to prevent memory exhaustion. * * @see docs/internal/architecture/multi-language-analysis.md * @module analysis/cache */ import type { ParseResult, CacheStats } from './types.js'; import { type AnalysisMetrics } from './metrics.js'; /** * Options for ParseCache */ export interface ParseCacheOptions { /** Maximum memory in MB (default: 200) */ maxMemoryMB?: number; /** TTL in milliseconds (default: no TTL) */ ttlMs?: number; /** Custom metrics instance (uses default if not provided) */ metrics?: AnalysisMetrics; } /** * LRU cache for parse results with memory-based eviction * * Caches parse results keyed by file path. Uses content hashing * to detect when cached results are stale. * * @example * ```typescript * const cache = new ParseCache({ maxMemoryMB: 100 }) * * // Check cache first * const cached = cache.get('src/main.py', fileContent) * if (cached) { * return cached * } * * // Parse and cache * const result = adapter.parseFile(fileContent, 'src/main.py') * cache.set('src/main.py', fileContent, result) * ``` */ export declare class ParseCache { private cache; private readonly maxMemory; private readonly metrics; private hits; private misses; constructor(options?: ParseCacheOptions); /** * Get cached result if content unchanged * * Returns null if: * - No entry exists for the path * - Content hash doesn't match (file was modified) * - Entry was evicted due to memory pressure * * SMI-1337: Records cache hit/miss metrics. * * @param filePath - Path to the file * @param content - Current file content for hash comparison * @returns Cached parse result or null * * @example * ```typescript * const cached = cache.get('src/main.py', fileContent) * if (cached) { * console.log('Cache hit!') * return cached * } * ``` */ get(filePath: string, content: string): ParseResult | null; /** * Store parse result in cache * * The result is stored with a content hash for future validation. * If the cache is at capacity, least recently used entries * are evicted to make room. * * SMI-1337: Updates cache size metrics. * * @param filePath - Path to the file * @param content - File content (used for hash) * @param result - Parse result to cache * * @example * ```typescript * const result = adapter.parseFile(content, 'src/main.py') * cache.set('src/main.py', content, result) * ``` */ set(filePath: string, content: string, result: ParseResult): void; /** * Check if a file is cached (without counting as hit/miss) * * @param filePath - Path to check * @returns True if entry exists (may be stale) */ has(filePath: string): boolean; /** * Invalidate cache entries for changed files * * Call this when files are known to have changed * to prevent stale cache hits. * * @param filePaths - Paths to invalidate * * @example * ```typescript * // On file system change event * cache.invalidate(['src/modified.py', 'src/deleted.py']) * ``` */ invalidate(filePaths: string[]): void; /** * Invalidate entries matching a pattern * * @param pattern - Glob-like pattern to match * * @example * ```typescript * // Invalidate all Python files * cache.invalidatePattern('*.py') * ``` */ invalidatePattern(pattern: string): void; /** * Clear entire cache * * Removes all entries and resets statistics. */ clear(): void; /** * Get cache statistics * * @returns Current cache statistics * * @example * ```typescript * const stats = cache.getStats() * console.log(`Hit rate: ${(stats.hitRate * 100).toFixed(1)}%`) * console.log(`Size: ${(stats.size / 1024 / 1024).toFixed(1)} MB`) * ``` */ getStats(): CacheStats; /** * Get number of cached entries */ get size(): number; /** * Reset hit/miss counters */ resetStats(): void; /** * Hash file content for change detection * * Uses SHA-256 truncated to 16 characters for efficiency. */ private hashContent; /** * Get language from file path extension * SMI-1337: Helper for metrics labeling */ private getLanguageFromPath; /** * Estimate memory size of a parse result * * Rough estimate based on array sizes and average item sizes. * SMI-1335: Named constants for magic numbers */ private estimateSize; /** * Convert glob pattern to regex */ private patternToRegex; } //# sourceMappingURL=cache.d.ts.map