runtime-memory-cache
Version:
A lightweight, high-performance in-memory cache for Node.js with TTL support, configurable eviction policies (FIFO/LRU), statistics tracking, and zero dependencies.
72 lines (71 loc) • 1.8 kB
TypeScript
import { CacheOptions, CacheStats, EvictionPolicy, MemoryUsage } from './types';
/**
* A lightweight in-memory cache with TTL support, configurable eviction policies,
* memory tracking, and comprehensive error handling
*/
export declare class RuntimeMemoryCache {
private readonly store;
private readonly maxSize;
private readonly ttl?;
private readonly evictionPolicy;
private readonly statsTracker?;
constructor(options?: CacheOptions);
/**
* Store a value in the cache with optional TTL override
*/
set(key: string, value: any, ttl?: number): void;
/**
* Retrieve a value from the cache
*/
get(key: string): any;
/**
* Check if a key exists and is not expired
*/
has(key: string): boolean;
/**
* Delete a specific key from the cache
*/
del(key: string): boolean;
/**
* Get current cache size
*/
size(): number;
/**
* Clear all entries from the cache
*/
clear(): void;
/**
* Get all keys in the cache
*/
keys(): string[];
/**
* Manually cleanup expired entries
*/
cleanup(): number;
/**
* Get cache statistics (if enabled)
*/
getStats(): CacheStats | null;
/**
* Reset statistics (if enabled)
*/
resetStats(): void;
/**
* Get current eviction policy
*/
getEvictionPolicy(): EvictionPolicy;
/**
* Get estimated memory usage
*/
getMemoryUsage(): MemoryUsage;
/**
* Evict an entry based on the configured eviction policy
*/
private evictEntry;
/**
* Update internal statistics
*/
private updateStats;
}
export { CacheOptions, CacheStats, EvictionPolicy, MemoryUsage } from './types';
export default RuntimeMemoryCache;