UNPKG

multibridge

Version:

A multi-database connection framework with centralized configuration

86 lines (85 loc) 2.31 kB
"use strict"; /** * Simple LRU (Least Recently Used) Cache implementation * Used for connection and config caching with size limits */ Object.defineProperty(exports, "__esModule", { value: true }); exports.LRUCache = void 0; class LRUCache { cache; maxSize; ttl; // Time to live in milliseconds constructor(maxSize = 100, ttl) { this.cache = new Map(); this.maxSize = maxSize; this.ttl = ttl; } get(key) { const entry = this.cache.get(key); if (!entry) { return undefined; } // Check if entry has expired if (this.ttl && Date.now() - entry.lastAccessed > this.ttl) { this.cache.delete(key); return undefined; } // Update last accessed time (LRU) entry.lastAccessed = Date.now(); return entry.value; } set(key, value) { // If key already exists, update it if (this.cache.has(key)) { const entry = this.cache.get(key); entry.value = value; entry.lastAccessed = Date.now(); return; } // If cache is full, remove least recently used item if (this.cache.size >= this.maxSize) { const firstKey = this.cache.keys().next().value; if (firstKey !== undefined) { this.cache.delete(firstKey); } } this.cache.set(key, { value, lastAccessed: Date.now(), }); } has(key) { const entry = this.cache.get(key); if (!entry) { return false; } // Check if entry has expired if (this.ttl && Date.now() - entry.lastAccessed > this.ttl) { this.cache.delete(key); return false; } return true; } delete(key) { return this.cache.delete(key); } clear() { this.cache.clear(); } size() { return this.cache.size; } // Get all keys (for iteration) keys() { return this.cache.keys(); } // Get all entries (for cleanup) entries() { return this.cache.entries(); } // Get entry by key (for internal use) getEntry(key) { return this.cache.get(key); } } exports.LRUCache = LRUCache;