UNPKG

avo-inspector

Version:

[![npm version](https://badge.fury.io/js/avo-inspector.svg)](https://badge.fury.io/js/avo-inspector)

74 lines (73 loc) 2.42 kB
import type { EventSpec } from "./types"; /** * EventSpecCache implements a dual-condition cache with LRU eviction. * * Cache Policy: * - Entries expire after 5 minutes OR 50 events, whichever comes first * - When 50 events are processed, the oldest cached entry is evicted * - Each cache entry tracks: spec, timestamp, and eventCount * * Cache Key Format: ${schemaId}:${sourceId}:${eventName}:${branchId} */ export declare class EventSpecCache { /** Cache storage: key -> CacheEntry */ private cache; /** Time-to-live in milliseconds (5 minutes) */ private readonly TTL_MS; /** Maximum event count before rotating cache (50 events) */ private readonly MAX_EVENT_COUNT; /** Global event counter to track when to rotate cache */ private globalEventCount; /** Whether to log debug information */ private readonly shouldLog; constructor(shouldLog?: boolean); /** * Generates a cache key from the provided parameters. */ private generateKey; /** * Retrieves an event spec from the cache if it exists and is valid. * Returns null if the entry is missing, expired, or has exceeded event count. */ get(schemaId: string, sourceId: string, eventName: string, branchId: string): EventSpec | null; /** * Stores an event spec in the cache. */ set(schemaId: string, sourceId: string, eventName: string, branchId: string, spec: EventSpec): void; /** * Increments the event count for all cached entries and triggers * eviction if the global event count reaches MAX_EVENT_COUNT. */ incrementEventCount(): void; /** * Determines if a cache entry should be evicted based on: * - Age (older than 5 minutes) * - Event count (50 or more events processed) */ private shouldEvict; /** * Evicts the oldest cache entry based on timestamp. * This implements the LRU (Least Recently Used) eviction policy. */ private evictOldest; /** * Clears all cached entries. Useful for testing. */ clear(): void; /** * Returns the current size of the cache. */ size(): number; /** * Returns cache statistics for debugging. */ getStats(): { size: number; globalEventCount: number; entries: Array<{ key: string; age: number; eventCount: number; }>; }; }