import type { EventSpecResponse, EventSpecCoordinates } from "./AvoEventSpecFetchTypes"; import { type ShouldLogFlag } from "../utils"; /** * EventSpecCache implements a dual-condition cache with LRU eviction. * * Cache Policy: * - Entries expire after 5 minutes OR 50 cache hits, whichever comes first * - When 50 cache hits occur globally, the oldest cached entry is evicted * - Each cache entry tracks: spec, timestamp, and hit count * * Cache Key Format: ${apiKey}:${streamId}:${eventName} * With gateway coordinates (originHint / outputReference, web GTM tag template * only) the key is JSON.stringify([apiKey, streamId, eventName, originHint, outputReference]) * instead, so specs for different checkpoints of the same event never share an entry. * * Note: Event count only increments on cache hits, not on every tracked event. * This ensures the cache evicts based on actual usage, not overall tracking volume. */ export declare class EventSpecCache { /** Cache storage: key -> CacheEntry */ private cache; /** Time-to-live in milliseconds (1 minute) */ private readonly TTL_MS; /** Maximum cache hit count before rotating cache (50 hits) */ private readonly MAX_EVENT_COUNT; /** Global cache hit counter to track when to rotate cache */ private globalEventCount; /** * Reads the current logging flag live, so a later enableLogging() / * AvoInspector.shouldLog change is reflected without re-pushing. See AVO-3079. */ private shouldLog; constructor(shouldLog?: ShouldLogFlag); /** * Generates a cache key from the provided parameters. */ private generateKey; /** * Checks if a non-expired cache entry exists for the given key. * Returns true even for cached null specs (empty backend responses). * Use this to distinguish between "cache miss" and "cached empty response". */ contains(apiKey: string, streamId: string, eventName: string, coordinates?: EventSpecCoordinates): boolean; /** * Retrieves an event spec response from the cache if it exists and is valid. * Returns null if the entry is missing, expired, has exceeded event count, * or if the cached spec itself is null (empty backend response). * * On cache hit, increments the hit count for this entry and the global counter. */ get(apiKey: string, streamId: string, eventName: string, coordinates?: EventSpecCoordinates): EventSpecResponse | null; /** * Stores an event spec response in the cache. * Pass null to cache an empty response (event not found on backend). */ set(apiKey: string, streamId: string, eventName: string, spec: EventSpecResponse | null, coordinates?: EventSpecCoordinates): void; /** * Determines if a cache entry should be evicted based on: * - Age (older than 5 minutes) * - Hit count (50 or more cache hits) */ private shouldEvict; /** * Evicts the least recently used cache entry based on lastAccessed 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; lastAccessedAgo: number; eventCount: number; }>; }; }