mcp-booster
Version:
Servidor MCP com CoConuT (Continuous Chain of Thought) para uso com Cursor IDE - Pacote Global NPM
140 lines (139 loc) • 3.62 kB
JavaScript
"use strict";
/**
* Sistema de cache para resultados de similaridade
* Melhora o desempenho evitando recálculos de similaridade entre textos
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.SimilarityCache = exports.LRUCache = void 0;
/**
* Classe de cache genérico com limite de tamanho e expiração
*/
class LRUCache {
constructor(maxSize = 1000, ttl = 0) {
this.cache = new Map();
this.maxSize = maxSize;
this.ttl = ttl;
}
/**
* Gera uma chave de cache a partir de um objeto
*/
generateKey(obj) {
return JSON.stringify(obj);
}
/**
* Verifica se uma entrada expirou
*/
isExpired(entry) {
if (this.ttl === 0)
return false;
const now = Date.now();
return now - entry.timestamp > this.ttl;
}
/**
* Obtém um valor do cache
*/
get(key) {
const cacheKey = this.generateKey(key);
const entry = this.cache.get(cacheKey);
if (!entry)
return undefined;
// Verificar expiração
if (this.isExpired(entry)) {
this.cache.delete(cacheKey);
return undefined;
}
// Atualizar timestamp para LRU
entry.timestamp = Date.now();
return entry.value;
}
/**
* Define um valor no cache
*/
set(key, value) {
const cacheKey = this.generateKey(key);
// Verificar se já existe para atualizar
if (this.cache.has(cacheKey)) {
this.cache.set(cacheKey, {
key: cacheKey,
value,
timestamp: Date.now()
});
return;
}
// Verificar tamanho do cache e remover o item mais antigo se necessário
if (this.cache.size >= this.maxSize) {
let oldest = null;
let oldestKey = '';
for (const [k, entry] of this.cache.entries()) {
if (!oldest || entry.timestamp < oldest.timestamp) {
oldest = entry;
oldestKey = k;
}
}
if (oldestKey) {
this.cache.delete(oldestKey);
}
}
// Adicionar nova entrada
this.cache.set(cacheKey, {
key: cacheKey,
value,
timestamp: Date.now()
});
}
/**
* Remove um valor do cache
*/
delete(key) {
const cacheKey = this.generateKey(key);
return this.cache.delete(cacheKey);
}
/**
* Limpa o cache
*/
clear() {
this.cache.clear();
}
/**
* Retorna o tamanho atual do cache
*/
size() {
return this.cache.size;
}
}
exports.LRUCache = LRUCache;
/**
* Cache específico para resultados de similaridade
*/
class SimilarityCache {
constructor(maxSize = 1000) {
this.cache = new LRUCache(maxSize);
}
/**
* Obtém um valor de similaridade do cache
*/
getSimilarity(text1, text2, algorithm) {
const key = { text1, text2, algorithm };
return this.cache.get(key);
}
/**
* Armazena um valor de similaridade no cache
*/
setSimilarity(text1, text2, algorithm, similarity) {
const key = { text1, text2, algorithm };
this.cache.set(key, similarity);
}
/**
* Limpa o cache de similaridade
*/
clear() {
this.cache.clear();
}
/**
* Retorna o tamanho atual do cache
*/
size() {
return this.cache.size();
}
}
exports.SimilarityCache = SimilarityCache;