UNPKG

prompt-plus-plus-mcp

Version:

Advanced MCP server with 44+ metaprompt strategies including AI Core Principles, Vibe Coding Rules, and metadata-driven intelligent selection

76 lines 2.11 kB
import { logger } from './logger.js'; export class Cache { cache = new Map(); defaultTtl; constructor(defaultTtl = 300000) { this.defaultTtl = defaultTtl; } set(key, data, ttl) { const entry = { data, timestamp: Date.now(), ttl: ttl || this.defaultTtl }; this.cache.set(key, entry); logger.debug(`Cache set: ${key}`, { ttl: entry.ttl }); } get(key) { const entry = this.cache.get(key); if (!entry) { logger.debug(`Cache miss: ${key}`); return null; } const now = Date.now(); const age = now - entry.timestamp; if (entry.ttl && age > entry.ttl) { this.cache.delete(key); logger.debug(`Cache expired: ${key}`, { age, ttl: entry.ttl }); return null; } logger.debug(`Cache hit: ${key}`, { age }); return entry.data; } has(key) { const entry = this.cache.get(key); if (!entry) return false; const now = Date.now(); const age = now - entry.timestamp; if (entry.ttl && age > entry.ttl) { this.cache.delete(key); return false; } return true; } delete(key) { const result = this.cache.delete(key); if (result) { logger.debug(`Cache deleted: ${key}`); } return result; } clear() { this.cache.clear(); logger.debug('Cache cleared'); } size() { return this.cache.size; } // Clean up expired entries cleanup() { const now = Date.now(); let cleaned = 0; for (const [key, entry] of this.cache.entries()) { const age = now - entry.timestamp; if (entry.ttl && age > entry.ttl) { this.cache.delete(key); cleaned++; } } if (cleaned > 0) { logger.debug(`Cache cleanup: ${cleaned} entries removed`); } return cleaned; } } //# sourceMappingURL=cache.js.map