memoru
Version:
A hash-based LRU cache that evicts entries based on memory usage rather than time or item count.
86 lines • 2.49 kB
TypeScript
import { type MemoryStatsMonitorOptions } from './memory-stats';
/**
* Options for configuring the Memoru cache.
* @public
*/
export interface MemoruOptions {
/**
* Maximum number of items in the cache. If not set, the cache is only rotated on memory threshold events.
* @defaultValue undefined
*/
max?: number;
/**
* Optional memory stats monitor configuration. If set, the cache will rotate when a memory threshold is reached.
* @defaultValue undefined
*/
memoryStats?: MemoryStatsMonitorOptions;
/**
* Whether to respect GC events when deciding to rotate the cache.
* If true, the cache won't rotate during or immediately after GC.
* Requires memoryStats to be configured with monitorGC: true
* @defaultValue false
*/
respectGC?: boolean;
}
/**
* Memoru is a high-performance LRU cache using a two-map hashlru algorithm.
* Supports any key type and can be rotated by size or memory threshold.
* @public
*/
export declare class Memoru<K, V> {
private max;
private size;
private cache;
private _cache;
private memoryMonitor?;
/**
* Create a new Memoru instance.
* @param options - Configuration options for the cache
* @public
*/
constructor(options: MemoruOptions);
/**
* Rotate the cache, moving current cache to shadow cache and clearing the main cache.
* Called internally on size or memory threshold.
* @internal
*/
private rotate;
/**
* Internal update method for inserting a new item and handling rotation.
* @param key - The key to insert
* @param value - The value to insert
* @internal
*/
private update;
/**
* Check if the cache contains a key.
* @param key - The key to check
* @public
*/
has(key: K): boolean;
/**
* Remove a key from the cache and shadow cache.
* @param key - The key to remove
* @public
*/
remove(key: K): void;
/**
* Get a value from the cache. If found in the shadow cache, it is promoted to the main cache.
* @param key - The key to retrieve
* @public
*/
get(key: K): V | undefined;
/**
* Set a value in the cache.
* @param key - The key to set
* @param value - The value to set
* @public
*/
set(key: K, value: V): void;
/**
* Clear the cache and shadow cache.
* @public
*/
clear(): void;
}
//# sourceMappingURL=lru.d.ts.map