UNPKG

@quantumai/quantum-cli-core

Version:

Quantum CLI Core - Multi-LLM Collaboration System

45 lines 1.23 kB
import { LRUCache } from 'lru-cache'; import { createHash } from 'crypto'; export class QueryCache { cache; stats = { hits: 0, misses: 0, hitRate: 0 }; constructor(maxSize = 100) { this.cache = new LRUCache({ max: maxSize }); } generateKey(query) { return createHash('sha256').update(query).digest('hex'); } get(query) { const key = this.generateKey(query); const entry = this.cache.get(key); if (entry) { this.stats.hits++; } else { this.stats.misses++; } this.updateHitRate(); return entry; } set(query, response, providerId) { const key = this.generateKey(query); const entry = { response, providerId, timestamp: Date.now(), }; this.cache.set(key, entry); } invalidate(query) { const key = this.generateKey(query); this.cache.delete(key); } getStats() { return this.stats; } updateHitRate() { const total = this.stats.hits + this.stats.misses; this.stats.hitRate = total > 0 ? this.stats.hits / total : 0; } } //# sourceMappingURL=query-cache.js.map