sussy-util
Version:
Util package made by me
65 lines (64 loc) • 1.98 kB
TypeScript
/**
* A Least Recently Used (LRU) Cache implementation with a fixed capacity.
* Provides O(1) lookup, insertion, and removal.
*
* @template Key - The type of keys stored in the cache.
* @template Value - The type of values stored in the cache.
*/
export declare class LRUCache<Key, Value> {
private readonly capacity;
private cache;
private nodes;
private usageOrder;
private evictionCallback;
/**
* @param capacity The maximum number of entries the cache can hold.
*/
constructor(capacity: number);
/**
* Retrieve a value from the cache.
* Moves the accessed item to the front of the usage order.
*
* @param key The key to look up.
* @returns The value associated with the key, or undefined if not found.
*/
get: (key: Key) => Value | undefined;
/**
* Add or update a key-value pair.
* If adding the item causes the cache to exceed capacity, evicts the least recently used item.
*
* @param key The key.
* @param value The value.
*/
put: (key: Key, value: Value) => void;
/**
* Remove an item from the cache.
*
* @param key The key of the item to remove.
* @returns True if the item was removed, false otherwise.
*/
remove: (key: Key) => boolean;
/**
* Clear the cache of all entries.
*/
clear: () => void;
/**
* Get all keys currently in the cache (order not guaranteed).
*/
getAllKeys: () => Key[];
/**
* Get all values currently in the cache (order not guaranteed).
*/
getAllValues: () => Value[];
/**
* Set a callback to be called when an item is evicted.
*
* @param callback The callback function.
*/
setEvictionCallback: (callback: (key: Key, value: Value) => void) => void;
/**
* Get a generator yielding all entries in the cache (order not guaranteed).
*/
entries(): Generator<[Key, Value]>;
}
export default LRUCache;