UNPKG

@dharshansr/gitgenius

Version:

AI-powered commit message generator with enhanced features

123 lines 3.8 kB
import { ConfigManager } from '../core/ConfigManager.js'; export class CacheManager { constructor() { this.cache = new Map(); this.configManager = new ConfigManager(); this.loadCache(); } static getInstance() { if (!CacheManager.instance) { CacheManager.instance = new CacheManager(); } return CacheManager.instance; } set(key, data, ttlMinutes = 60) { const entry = { data, timestamp: Date.now(), ttl: ttlMinutes * 60 * 1000 // Convert to milliseconds }; this.cache.set(key, entry); this.saveCache(); } get(key) { const entry = this.cache.get(key); if (!entry) { return null; } // Check if entry has expired if (Date.now() - entry.timestamp > entry.ttl) { this.cache.delete(key); this.saveCache(); return null; } return entry.data; } has(key) { const entry = this.cache.get(key); if (!entry) return false; // Check if expired if (Date.now() - entry.timestamp > entry.ttl) { this.cache.delete(key); this.saveCache(); return false; } return true; } delete(key) { const deleted = this.cache.delete(key); if (deleted) { this.saveCache(); } return deleted; } clear() { this.cache.clear(); this.saveCache(); } // Cache commit messages for similar diffs cacheCommitMessage(diffHash, message, provider) { const key = `commit:${provider}:${diffHash}`; this.set(key, message, 120); // Cache for 2 hours } getCachedCommitMessage(diffHash, provider) { const key = `commit:${provider}:${diffHash}`; return this.get(key); } // Cache AI responses for code reviews cacheReview(codeHash, review, provider) { const key = `review:${provider}:${codeHash}`; this.set(key, review, 240); // Cache for 4 hours } getCachedReview(codeHash, provider) { const key = `review:${provider}:${codeHash}`; return this.get(key); } loadCache() { try { const cacheData = this.configManager.getConfig('cache') || {}; this.cache = new Map(Object.entries(cacheData)); } catch (error) { // If cache loading fails, start with empty cache this.cache = new Map(); } } saveCache() { try { const cacheData = Object.fromEntries(this.cache.entries()); this.configManager.setConfigValue('cache', cacheData); } catch (error) { // Silently fail if cache saving fails } } // Utility method to create hash from content static createHash(content) { // Simple hash function for caching purposes let hash = 0; if (content.length === 0) return hash.toString(); for (let i = 0; i < content.length; i++) { const char = content.charCodeAt(i); hash = ((hash << 5) - hash) + char; hash = hash & hash; // Convert to 32-bit integer } return Math.abs(hash).toString(36); } // Get cache statistics getStats() { const entries = Array.from(this.cache.entries()).map(([key, entry]) => ({ key, age: Math.floor((Date.now() - entry.timestamp) / 1000), ttl: Math.floor(entry.ttl / 1000) })); return { size: this.cache.size, hitRate: 0, // Would need to track hits/misses for this entries }; } } //# sourceMappingURL=CacheManager.js.map