matrix-react-sdk
Version:
SDK for matrix.org using React
71 lines (70 loc) • 1.99 kB
TypeScript
/**
* Least Recently Used cache.
* Can be initialised with a capacity and drops the least recently used items.
* This cache should be error robust: Cache miss on error.
*
* Implemented via a key lookup map and a double linked list:
* head tail
* a next → b next → c → next null
* null ← prev a ← prev b ← prev c
*
* @template K - Type of the key used to look up the values inside the cache
* @template V - Type of the values inside the cache
*/
export declare class LruCache<K, V> {
private capacity;
/** Head of the list. */
private head;
/** Tail of the list */
private tail;
/** Key lookup map */
private map;
/**
* @param capacity - Cache capcity.
* @throws {Error} - Raises an error if the cache capacity is less than 1.
*/
constructor(capacity: number);
/**
* Whether the cache contains an item under this key.
* Marks the item as most recently used.
*
* @param key - Key of the item
* @returns true: item in cache, else false
*/
has(key: K): boolean;
/**
* Returns an item from the cache.
* Marks the item as most recently used.
*
* @param key - Key of the item
* @returns The value if found, else undefined
*/
get(key: K): V | undefined;
/**
* Adds an item to the cache.
* A newly added item will be the set as the most recently used.
*
* @param key - Key of the item
* @param value - Item value
*/
set(key: K, value: V): void;
/**
* Deletes an item from the cache.
*
* @param key - Key of the item to be removed
*/
delete(key: K): void;
/**
* Clears the cache.
*/
clear(): void;
/**
* Returns an iterator over the cached values.
*/
values(): IterableIterator<V>;
private safeSet;
private onError;
private getItem;
private setHeadTail;
private removeItemFromList;
}