UNPKG

mcp-duckduckgo-search

Version:
43 lines (42 loc) 1.07 kB
import { CACHE_TTL } from './config.js'; class SearchCache { constructor() { this.cache = new Map(); } get(key) { const entry = this.cache.get(key); if (!entry) { return null; } // Check if cache entry has expired if (Date.now() - entry.timestamp > CACHE_TTL) { this.cache.delete(key); return null; } return entry.data; } set(key, data) { this.cache.set(key, { timestamp: Date.now(), data, }); } clear() { this.cache.clear(); } // Clean expired entries clean() { const now = Date.now(); for (const [key, entry] of this.cache.entries()) { if (now - entry.timestamp > CACHE_TTL) { this.cache.delete(key); } } } // Get cache key from search parameters static get_cache_key(params) { return JSON.stringify(params); } } // Export singleton instance export const search_cache = new SearchCache();