@m4x1m1l14n/cache
Version:
Lightweight in-memory isomorphic cache implementation with TTL for browser & Node JS written in TypeScript
63 lines (62 loc) • 1.88 kB
TypeScript
import { ExpirationEntry } from '../models/ExpirationEntry';
/**
* A min-heap implementation for efficiently tracking cache item expiration times.
* The heap is ordered by expiration timestamp, with the earliest expiration at the root.
*/
export declare class MinHeap<K> {
private heap;
/**
* Gets the number of items in the heap
*/
get size(): number;
/**
* Checks if the heap is empty
*/
get isEmpty(): boolean;
/**
* Gets the minimum expiration entry without removing it
* @returns The entry with the earliest expiration time, or undefined if heap is empty
*/
peek(): ExpirationEntry<K> | undefined;
/**
* Inserts a new expiration entry into the heap
* @param entry The expiration entry to insert
*/
insert(entry: ExpirationEntry<K>): void;
/**
* Removes and returns the minimum expiration entry
* @returns The entry with the earliest expiration time, or undefined if heap is empty
*/
extractMin(): ExpirationEntry<K> | undefined;
/**
* Removes all entries with the specified key
* @param key The key to remove
* @returns The number of entries removed
*/
removeByKey(key: K): number;
/**
* Removes all entries from the heap
*/
clear(): void;
/**
* Removes the entry at the specified index
* @param index The index to remove
*/
private removeAt;
/**
* Moves an element up the heap to maintain heap property
* @param index The index of the element to move up
*/
private heapifyUp;
/**
* Moves an element down the heap to maintain heap property
* @param index The index of the element to move down
*/
private heapifyDown;
/**
* Swaps two elements in the heap
* @param i First index
* @param j Second index
*/
private swap;
}