{"version":3,"file":"cache.cjs","sources":["../../../src/core/cache.ts"],"sourcesContent":["/**\n * LRU Cache for Search Results\n * Provides 10-100x speedup for repeated queries (e.g., autocomplete)\n */\n\nimport type { SuggestionResult } from \"./types.js\";\n\n/**\n * LRU (Least Recently Used) Cache\n * Automatically evicts oldest entries when capacity is reached\n */\nexport class LRUCache<K, V> {\n  private cache: Map<K, V>;\n  private capacity: number;\n\n  constructor(capacity: number = 100) {\n    this.cache = new Map();\n    this.capacity = capacity;\n  }\n\n  /**\n   * Get value from cache\n   * Moves item to end (most recently used)\n   */\n  get(key: K): V | undefined {\n    if (!this.cache.has(key)) {\n      return undefined;\n    }\n\n    // Move to end (most recently used)\n    const value = this.cache.get(key)!;\n    this.cache.delete(key);\n    this.cache.set(key, value);\n\n    return value;\n  }\n\n  /**\n   * Set value in cache\n   * Evicts oldest entry if capacity exceeded\n   */\n  set(\n    //\n    key: K,\n    value: V\n  ): void {\n    // Remove if exists (to update position)\n    if (this.cache.has(key)) {\n      this.cache.delete(key);\n    }\n\n    // Add to end (most recently used)\n    this.cache.set(key, value);\n\n    // Evict oldest if over capacity\n    if (this.cache.size > this.capacity) {\n      const firstKey = this.cache.keys().next().value as K;\n      if (firstKey !== undefined) {\n        this.cache.delete(firstKey);\n      }\n    }\n  }\n\n  /**\n   * Check if key exists in cache\n   */\n  has(key: K): boolean {\n    return this.cache.has(key);\n  }\n\n  /**\n   * Clear all cached entries\n   */\n  clear(): void {\n    this.cache.clear();\n  }\n\n  /**\n   * Get current cache size\n   */\n  get size(): number {\n    return this.cache.size;\n  }\n\n  /**\n   * Get cache statistics\n   */\n  getStats(): {\n    //\n    size: number;\n    capacity: number;\n    utilization: number;\n  } {\n    return {\n      size: this.cache.size,\n      capacity: this.capacity,\n      utilization: this.cache.size / this.capacity,\n    };\n  }\n}\n\n/**\n * Search Result Cache\n * Caches search results with automatic invalidation\n */\nexport class SearchCache {\n  private cache: LRUCache<string, SuggestionResult[]>;\n  private hits: number = 0;\n  private misses: number = 0;\n\n  constructor(capacity: number = 100) {\n    this.cache = new LRUCache(capacity);\n  }\n\n  /**\n   * Generate cache key from query and options\n   */\n  private getCacheKey(query: string, maxResults?: number, options?: any): string {\n    const optionsKey = options ? JSON.stringify(options) : \"\";\n    return `${query}|${maxResults || \"default\"}|${optionsKey}`;\n  }\n\n  /**\n   * Get cached results\n   */\n  get(query: string, maxResults?: number, options?: any): SuggestionResult[] | undefined {\n    const key = this.getCacheKey(query, maxResults, options);\n    const result = this.cache.get(key);\n\n    if (result) {\n      this.hits++;\n    } else {\n      this.misses++;\n    }\n\n    return result;\n  }\n\n  /**\n   * Set cached results\n   */\n  set(query: string, results: SuggestionResult[], maxResults?: number, options?: any): void {\n    const key = this.getCacheKey(query, maxResults, options);\n    this.cache.set(key, results);\n  }\n\n  /**\n   * Clear cache\n   */\n  clear(): void {\n    this.cache.clear();\n    this.hits = 0;\n    this.misses = 0;\n  }\n\n  /**\n   * Get cache statistics\n   */\n  getStats(): {\n    //\n    size: number;\n    capacity: number;\n    hits: number;\n    misses: number;\n    hitRate: number;\n  } {\n    const cacheStats = this.cache.getStats();\n    const total = this.hits + this.misses;\n    const hitRate = total > 0 ? this.hits / total : 0;\n\n    return {\n      ...cacheStats,\n      hits: this.hits,\n      misses: this.misses,\n      hitRate,\n    };\n  }\n}\n"],"names":[],"mappings":";;AAWO,MAAM,SAAe;AAAA,EAClB;AAAA,EACA;AAAA,EAER,YAAY,WAAmB,KAAK;AAClC,SAAK,4BAAY,IAAA;AACjB,SAAK,WAAW;AAAA,EAClB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,IAAI,KAAuB;AACzB,QAAI,CAAC,KAAK,MAAM,IAAI,GAAG,GAAG;AACxB,aAAO;AAAA,IACT;AAGA,UAAM,QAAQ,KAAK,MAAM,IAAI,GAAG;AAChC,SAAK,MAAM,OAAO,GAAG;AACrB,SAAK,MAAM,IAAI,KAAK,KAAK;AAEzB,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;A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