UNPKG

bc-webclient-mcp

Version:

Model Context Protocol (MCP) server for Microsoft Dynamics 365 Business Central via WebUI protocol. Enables AI assistants to interact with BC through the web client protocol, supporting Card, List, and Document pages with full line item support and server

156 lines 4.31 kB
/** * Cache Manager for Business Central MCP Server * * Provides time-based LRU caching with: * - TTL (Time To Live) expiration * - LRU eviction when cache full * - Cache stampede protection (coalescing) * - Hit/miss statistics * - Background cleanup * * Usage: * ```typescript * const cache = new CacheManager({ maxEntries: 1000 }); * * // Check cache * const cached = cache.get<SearchResult>('search:customer:Card:10'); * if (cached) return cached; * * // Fetch and cache * const result = await fetchData(); * cache.set('search:customer:Card:10', result, 300000); // 5 min TTL * ``` */ /** * Cache statistics */ export interface CacheStats { /** Total number of cache lookups */ totalRequests: number; /** Number of cache hits */ hits: number; /** Number of cache misses */ misses: number; /** Cache hit rate (0-1) */ hitRate: number; /** Current number of entries */ size: number; /** Maximum number of entries allowed */ maxEntries: number; /** Number of entries evicted due to size limit */ evictions: number; /** Number of entries expired */ expirations: number; } /** * Configuration for cache manager */ export interface CacheManagerConfig { /** Maximum number of entries (default: 1000) */ maxEntries?: number; /** Cleanup interval in milliseconds (default: 60000 = 1 minute) */ cleanupIntervalMs?: number; /** Default TTL if not specified in set() (default: 300000 = 5 minutes) */ defaultTtlMs?: number; /** Enable cache stampede protection (default: true) */ enableCoalescing?: boolean; } /** * Cache manager with TTL and LRU eviction * * Features: * - Time-based expiration (TTL) * - LRU eviction when cache full * - Cache stampede protection via coalescing * - Hit/miss statistics * - Background cleanup */ export declare class CacheManager { private readonly maxEntries; private readonly cleanupIntervalMs; private readonly defaultTtlMs; private readonly enableCoalescing; /** Cache storage */ private cache; /** Pending operations (for stampede protection) */ private pendingOperations; /** Cleanup interval handle */ private cleanupInterval; /** Statistics */ private stats; constructor(config?: CacheManagerConfig); /** * Get a value from the cache * * @param key The cache key * @returns The cached value or null if not found/expired */ get<T>(key: string): T | null; /** * Set a value in the cache * * @param key The cache key * @param value The value to cache * @param ttlMs Time to live in milliseconds (default: config.defaultTtlMs) */ set<T>(key: string, value: T, ttlMs?: number): void; /** * Delete a value from the cache * * @param key The cache key * @returns True if the entry was deleted */ delete(key: string): boolean; /** * Clear all entries matching a pattern * * @param pattern Glob-style pattern (e.g., "search:*") */ invalidate(pattern: string): number; /** * Clear the entire cache */ clear(): void; /** * Get cache statistics */ getStats(): CacheStats; /** * Reset statistics */ resetStats(): void; /** * Execute an operation with cache stampede protection * * If multiple requests for the same key arrive simultaneously, * only the first one executes the operation. Others wait for * the result and receive the same value. * * @param key The cache key * @param operation The operation to execute if not cached * @param ttlMs TTL for the cached result * @returns The cached or computed value */ getOrCompute<T>(key: string, operation: () => Promise<T>, ttlMs?: number): Promise<T>; /** * Shutdown the cache manager * Stops background cleanup */ shutdown(): void; /** * Evict the least recently used entry * @private */ private evictLRU; /** * Start background cleanup of expired entries * @private */ private startCleanup; /** * Remove expired entries from cache * @private */ private cleanupExpired; } //# sourceMappingURL=cache-manager.d.ts.map