/** * Nuvex - Memory Storage Layer (L1) * Next-gen Unified Vault Experience * * In-memory cache layer with LRU (Least Recently Used) eviction policy. * Provides the fastest access tier in the multi-layer storage architecture. * * Features: * - LRU eviction with configurable maxSize * - TTL-based expiration * - Sub-millisecond access times * - Automatic cleanup of expired entries * * @author Waren Gonzaga, WG Technology Labs * @since 2025 */ import type { StorageLayerInterface, Logger } from '../interfaces/index.js'; /** * Memory Storage Layer - L1 Cache with LRU Eviction * * Implements an in-memory cache with Least Recently Used (LRU) eviction policy. * This layer provides the fastest access times but has limited capacity defined * by maxSize. When the cache is full, the least recently accessed item is evicted * to make room for new entries. * * **LRU Implementation:** * - Uses JavaScript Map which maintains insertion order * - On get(), moves accessed entry to end (marks as recently used) * - On set(), evicts first entry (oldest/least recently used) when full * - Combines LRU with TTL-based expiration for optimal memory management * * **Performance Characteristics:** * - Get: O(1) average, O(n) worst case due to delete+set for LRU * - Set: O(1) with occasional O(1) eviction * - Memory: O(maxSize) * * @implements {StorageLayerInterface} * * @example * ```typescript * // Create memory layer with 1000 entry limit * const memory = new MemoryStorage(1000); * * // Store with 60 second TTL * await memory.set('user:123', userData, 60); * * // Retrieve (marks as recently used) * const data = await memory.get('user:123'); * * // Check health * const isHealthy = await memory.ping(); // Always true * ``` * * @class MemoryStorage * @since 1.0.0 */ export declare class MemoryStorage implements StorageLayerInterface { /** In-memory cache using Map for LRU ordering */ private cache; /** Maximum number of entries before LRU eviction kicks in */ private readonly maxSize; /** Optional logger for debugging and monitoring */ private logger; /** * Creates a new MemoryStorage instance * * @param maxSize - Maximum number of entries to store (default: 10,000) * @param logger - Optional logger for debugging * * @example * ```typescript * // Default configuration * const memory = new MemoryStorage(); * * // Custom size limit * const memory = new MemoryStorage(5000); * * // With logging * const memory = new MemoryStorage(10000, console); * ``` */ constructor(maxSize?: number, logger?: Logger | null); /** * Retrieve a value from memory cache * * Implements LRU by moving accessed entries to the end of the Map, * marking them as recently used. Automatically removes expired entries. * * @param key - The key to retrieve * @returns Promise resolving to the value or null if not found/expired * * @example * ```typescript * const value = await memory.get('user:123'); * if (value !== null) { * console.log('Cache hit!'); * } * ``` */ get(key: string): Promise; /** * Store a value in memory cache * * Implements LRU eviction when cache is full. If the cache has reached * maxSize and the key doesn't already exist, the oldest entry (first in Map) * is evicted to make room for the new entry. * * @param key - The key to store * @param value - The value to store * @param ttlSeconds - Optional TTL in seconds * * @example * ```typescript * // Store without TTL * await memory.set('config:app', configData); * * // Store with 5 minute TTL * await memory.set('session:abc', sessionData, 300); * ``` */ set(key: string, value: unknown, ttlSeconds?: number): Promise; /** * Delete a value from memory cache * * @param key - The key to delete * * @example * ```typescript * await memory.delete('user:123'); * ``` */ delete(key: string): Promise; /** * Check if a key exists in memory cache * * Verifies existence and checks if the entry has expired. * Automatically removes expired entries. * * @param key - The key to check * @returns Promise resolving to true if the key exists and is not expired * * @example * ```typescript * if (await memory.exists('user:123')) { * console.log('Key exists in memory'); * } * ``` */ exists(key: string): Promise; /** * Clear all entries from memory cache * * Removes all stored entries, resetting the cache to empty state. * * @example * ```typescript * await memory.clear(); * console.log('All memory cache cleared'); * ``` */ clear(): Promise; /** * Get all keys matching an optional pattern * * Returns all non-expired keys from the memory cache, optionally filtered * by a glob pattern. Supports '*' (match any characters) and '?' (match single character). * * @param pattern - Optional glob pattern (default: '*' returns all keys) * @returns Promise resolving to array of matching keys * * @example * ```typescript * // Get all keys * const allKeys = await memory.keys(); * * // Get keys matching pattern * const userKeys = await memory.keys('user:*'); * ``` */ keys(pattern?: string): Promise; /** * Health check for memory storage layer * * Memory storage is always available if the application is running, * so this method always returns true unless there's a critical failure. * * @returns Promise resolving to true (memory is always available) * * @example * ```typescript * const isHealthy = await memory.ping(); * console.log('Memory layer healthy:', isHealthy); * ``` */ ping(): Promise; /** * Get current cache size * * Returns the number of entries currently stored in the cache. * Useful for monitoring and debugging. * * @returns Current number of entries in cache * * @example * ```typescript * console.log(`Cache usage: ${memory.size()}/${memory.getMaxSize()}`); * ``` */ size(): number; /** * Get maximum cache size * * Returns the configured maximum number of entries. * * @returns Maximum cache size */ getMaxSize(): number; /** * Clean up expired entries * * Iterates through all entries and removes expired ones. * This is useful for manual cleanup or scheduled maintenance. * * @returns Number of entries removed * * @example * ```typescript * const cleaned = await memory.cleanup(); * console.log(`Removed ${cleaned} expired entries`); * ``` */ cleanup(): Promise; /** * Atomically increment a numeric value * * This operation is thread-safe for single-instance deployments. * If the key doesn't exist or is expired, it's initialized to 0 before incrementing. * * @param key - The key to increment * @param delta - The amount to increment by (can be negative for decrement) * @param ttlSeconds - Optional TTL in seconds * @returns Promise resolving to the new value after increment * * @example * ```typescript * const newValue = await memory.increment('counter', 1, 60); * console.log(`Counter is now: ${newValue}`); * ``` */ increment(key: string, delta: number, ttlSeconds?: number): Promise; /** * Log a message if logger is configured * * @private * @param level - Log level * @param message - Log message * @param meta - Optional metadata */ private log; } //# sourceMappingURL=memory.d.ts.map