/** * Eviction Policy Implementation * Supports LRU, LFU, and hybrid eviction strategies * Requirements: 5.1, 5.2, 5.3, 5.4 */ import { Tier } from '../types/index.js'; /** * Policy type for eviction strategy selection */ export type PolicyType = 'lru' | 'lfu' | 'hybrid'; /** * Eviction hint for key-level priority control */ export interface EvictionHint { /** Priority level - high priority keys are evicted last */ priority: 'low' | 'normal' | 'high'; /** If true, key will never be evicted */ sticky?: boolean; } /** * Internal tracking data for a key */ interface KeyTrackingData { /** Last access timestamp */ lastAccess: number; /** Access count for LFU */ accessCount: number; /** When the key was set */ setTime: number; /** Current tier */ tier: Tier; } /** * Candidate information returned during eviction selection */ export interface EvictionCandidate { key: string; score: number; tier: Tier; } /** * EvictionPolicy manages eviction candidate selection using LRU, LFU, or hybrid strategies. * * - LRU (Least Recently Used): Evicts keys that haven't been accessed recently * - LFU (Least Frequently Used): Evicts keys with the lowest access count * - Hybrid: Combines LRU and LFU scores with configurable weighting * * Requirements: * - 5.1: Support LRU eviction strategy * - 5.2: Support LFU eviction strategy * - 5.3: Support hybrid LRU-LFU eviction strategy * - 5.4: Allow configuration of eviction policy at startup */ export declare class EvictionPolicy { private policy; private keyTracking; private keyHints; /** Weight for LRU component in hybrid mode (0-1) */ private hybridLruWeight; constructor(policy?: PolicyType, hybridLruWeight?: number); /** * Select eviction candidates from a tier based on the configured policy. * Returns keys sorted by eviction priority (first key = best candidate to evict). * * @param tier - The tier to select candidates from * @param count - Number of candidates to return * @returns Array of keys to evict, ordered by priority * * Requirements: 5.1, 5.2, 5.3, 5.5, 5.6 */ selectEvictionCandidates(tier: Tier, count: number): string[]; /** * Hook called when a key is accessed (GET operation). * Updates LRU timestamp and LFU counter. * * @param key - The key that was accessed */ onAccess(key: string): void; /** * Hook called when a key is set (SET operation). * Initializes or updates tracking data. * * @param key - The key that was set * @param tier - The tier where the key was stored */ onSet(key: string, tier?: Tier): void; /** * Hook called when a key is deleted (DEL operation). * Removes tracking data and hints. * * @param key - The key that was deleted */ onDelete(key: string): void; /** * Switch to a different eviction policy. * * @param policy - The new policy type * * Requirement 5.4: Allow configuration of eviction policy */ setPolicy(policy: PolicyType): void; /** * Get the current eviction policy. */ getPolicy(): PolicyType; /** * Set an eviction hint for a specific key. * Hints control eviction priority and can mark keys as sticky (never evict). * * @param key - The key to set hint for * @param hint - The eviction hint * * Requirement 5.6: Respect key-level eviction hints */ setKeyHint(key: string, hint: EvictionHint): void; /** * Get the eviction hint for a key. */ getKeyHint(key: string): EvictionHint | undefined; /** * Remove the eviction hint for a key. */ removeKeyHint(key: string): boolean; /** * Update the tier for a key (used during promotion/demotion). */ updateKeyTier(key: string, tier: Tier): void; /** * Set the LRU weight for hybrid mode. * @param weight - Weight between 0 (pure LFU) and 1 (pure LRU) */ setHybridLruWeight(weight: number): void; /** * Get the current hybrid LRU weight. */ getHybridLruWeight(): number; /** * Get tracking data for a key (for testing/debugging). */ getKeyTracking(key: string): KeyTrackingData | undefined; /** * Get the number of tracked keys. */ getTrackedKeyCount(): number; /** * Clear all tracking data and hints. */ clear(): void; /** * Get all keys in a specific tier. */ private getKeysInTier; /** * Calculate eviction score for a key based on the current policy. * Lower score = better candidate for eviction. */ private calculateEvictionScore; /** * Calculate LRU score - based on time since last access. * Lower score (older access) = better candidate for eviction. * * Requirement 5.1: LRU eviction strategy */ private calculateLruScore; /** * Calculate LFU score - based on access count. * Lower score (fewer accesses) = better candidate for eviction. * * Requirement 5.2: LFU eviction strategy */ private calculateLfuScore; /** * Calculate hybrid score - combines LRU and LFU. * Uses normalized scores weighted by hybridLruWeight. * * Requirement 5.3: Hybrid LRU-LFU eviction strategy */ private calculateHybridScore; /** * Convert priority string to numeric value for sorting. * Lower value = evict first. */ private getPriorityValue; } export {}; //# sourceMappingURL=EvictionPolicy.d.ts.map