cache-entanglement
Version:
Manage caches that are dependent on each other efficiently.
83 lines (82 loc) • 2.52 kB
TypeScript
/**
* Represents a node in the LRU cache's doubly linked list.
* @template K Type of the key.
* @template V Type of the value.
*/
export interface CacheNode<K, V> {
key: K;
value: V;
prev: CacheNode<K, V> | null;
next: CacheNode<K, V> | null;
}
/**
* A Map-like data structure that implements the Least Recently Used (LRU) eviction policy.
* Once the capacity is reached, the least recently accessed item is removed.
* @template K Type of the key.
* @template V Type of the value.
*/
export declare class LRUMap<K, V> {
private capacity;
private map;
private head;
private tail;
/**
* Creates an instance of LRUMap.
* @param capacity The maximum number of items the cache can hold.
*/
constructor(capacity: number);
/**
* Promotes a node to the head of the linked list (marks as most recently used).
* @param node The node to promote.
*/
private promote;
/**
* Disconnects a node from the doubly linked list.
* @param node The node to extract.
*/
private extract;
/**
* Inserts a node at the head of the doubly linked list.
* @param node The node to prepend.
*/
private prepend;
/**
* Stores or updates a value by key.
* If the capacity is exceeded, the least recently used item (tail) is removed.
* @param key The key to store.
* @param value The value to store.
*/
set(key: K, value: V): void;
/**
* Retrieves a value by key.
* Accessing the item moves it to the "most recently used" position.
* @param key The key to look for.
* @returns The value associated with the key, or undefined if not found.
*/
get(key: K): V | undefined;
/**
* Checks if a key exists in the cache without changing its access order.
* @param key The key to check.
* @returns True if the key exists, false otherwise.
*/
has(key: K): boolean;
/**
* Removes a key and its associated value from the cache.
* @param key The key to remove.
* @returns True if the key was found and removed, false otherwise.
*/
delete(key: K): boolean;
/**
* Returns an iterator of keys in the order of most recently used to least recently used.
* @returns An iterable iterator of keys.
*/
keys(): IterableIterator<K>;
/**
* Returns the current number of items in the cache.
*/
get size(): number;
/**
* Clears all items from the cache.
*/
clear(): void;
}