UNPKG

knowledge-base-mcp

Version:

知识库MCP服务,基于Dify MCP协议的两步式知识库检索系统

69 lines (68 loc) 1.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CacheManager = void 0; class CacheManager { cache = new Map(); defaultTTL; constructor(defaultTTL = 3600000) { this.defaultTTL = defaultTTL; // 启动定期清理任务 setInterval(() => this.cleanup(), 300000); // 每5分钟清理一次 } /** * 设置缓存 * @param key 缓存键 * @param data 缓存数据 * @param ttl 缓存有效期(毫秒) */ set(key, data, ttl = this.defaultTTL) { const now = Date.now(); this.cache.set(key, { data, timestamp: now, expiresAt: now + ttl }); } /** * 获取缓存 * @param key 缓存键 * @returns 缓存数据,如不存在或已过期返回null */ get(key) { const item = this.cache.get(key); if (!item) { return null; } const now = Date.now(); if (now > item.expiresAt) { this.cache.delete(key); return null; } return item.data; } /** * 删除缓存 * @param key 缓存键 */ delete(key) { return this.cache.delete(key); } /** * 清空所有缓存 */ clear() { this.cache.clear(); } /** * 清理过期缓存 */ cleanup() { const now = Date.now(); for (const [key, item] of this.cache.entries()) { if (now > item.expiresAt) { this.cache.delete(key); } } } } exports.CacheManager = CacheManager;