UNPKG

@thermopylae/lib.cache

Version:
40 lines (39 loc) 1.64 kB
import { UnixTimestamp } from '@thermopylae/core.declarations'; import { EntryExpiredCallback, ExpirableEntry, GarbageCollector } from './interface'; import { HeapNode } from '../data-structures/heap'; /** * @private */ interface HeapExpirableEntry extends ExpirableEntry, HeapNode { } /** * {@link GarbageCollector} which uses a binary min heap to keep entries that needs to be evicted. <br/> * Each heap node is represent by cache entry which contains expiration time. <br/> * Eviction timer is always synchronized with heap root and fires at expiration time of root entry. * When it fires, will evict root and other nodes that have expiration time equal to root. * After that, timer will fire again at the expiration time of the newest root. * * @template T Type of the cache entry. */ declare class HeapGarbageCollector<T extends HeapExpirableEntry> implements GarbageCollector<T> { private readonly entries; private cleanUpInterval; private entryExpiredCb; constructor(entryExpiredCb?: EntryExpiredCallback<T>); get size(): number; get idle(): boolean; manage(entry: T): void; update(_oldExpiration: UnixTimestamp, entry: T): void; leave(entry: T): void; clear(): void; setEntryExpiredCallback(cb: EntryExpiredCallback<T>): void; /** * This method synchronizes garbage collection. <br/> * It needs to be called every time expirable keys heap is altered. */ private synchronizeEvictionTimer; private scheduleNextGc; private evictExpiredEntries; private static defaultEntryExpiredCallback; } export { HeapGarbageCollector, HeapExpirableEntry };