UNPKG

maplestorysea-mcp-server

Version:

NEXON MapleStory SEA API MCP Server for Claude Desktop - Complete character info, union details, guild data, rankings optimized for SEA servers

128 lines 3.52 kB
"use strict"; /** * Simple in-memory cache for API responses */ Object.defineProperty(exports, "__esModule", { value: true }); exports.defaultCache = exports.MemoryCache = void 0; class MemoryCache { cache = new Map(); maxSize; constructor(maxSize = 1000) { this.maxSize = maxSize; } /** * Set a value in the cache with TTL (time to live) in milliseconds */ set(key, value, ttl = 300000) { // Default 5 minutes const now = Date.now(); // Remove expired entries if cache is getting full if (this.cache.size >= this.maxSize) { this.cleanup(); } this.cache.set(key, { data: value, timestamp: now, ttl, }); } /** * Get a value from the cache */ get(key) { const entry = this.cache.get(key); if (!entry) { return null; } const now = Date.now(); const isExpired = now - entry.timestamp > entry.ttl; if (isExpired) { this.cache.delete(key); return null; } return entry.data; } /** * Check if a key exists and is not expired */ has(key) { return this.get(key) !== null; } /** * Delete a specific key */ delete(key) { return this.cache.delete(key); } /** * Clear all cache entries */ clear() { this.cache.clear(); } /** * Remove expired entries */ cleanup() { const now = Date.now(); const keysToDelete = []; for (const [key, entry] of this.cache.entries()) { const isExpired = now - entry.timestamp > entry.ttl; if (isExpired) { keysToDelete.push(key); } } keysToDelete.forEach((key) => this.cache.delete(key)); } /** * Get cache size */ size() { return this.cache.size; } /** * Get cache statistics */ getStats() { return { size: this.cache.size, maxSize: this.maxSize, }; } /** * Generate a cache key for character OCID lookup (SEA API optimized) */ static generateOcidCacheKey(characterName) { // SEA API uses English character names only - normalize for consistent caching const normalizedName = characterName.trim().toLowerCase().replace(/\s+/g, ''); return `sea_ocid:${normalizedName}`; } /** * Generate a cache key for character basic info (SEA API optimized) */ static generateCharacterBasicCacheKey(ocid, date) { const dateKey = date || 'latest'; return `sea_char_basic:${ocid}:${dateKey}`; } /** * Generate a cache key for any SEA API endpoint */ static generateApiCacheKey(endpoint, params) { const sortedParams = Object.keys(params) .sort() .map((key) => { const value = params[key]; // Normalize character names for consistent caching if (key.includes('character_name') && typeof value === 'string') { return `${key}=${value.trim().toLowerCase().replace(/\s+/g, '')}`; } return `${key}=${value}`; }) .join('&'); return `sea_api:${endpoint}:${sortedParams}`; } } exports.MemoryCache = MemoryCache; // Default cache instance exports.defaultCache = new MemoryCache(1000); //# sourceMappingURL=cache.js.map