@thermopylae/lib.cache
Version:
49 lines (48 loc) • 2.04 kB
TypeScript
import { Seconds, Threshold } from '@thermopylae/core.declarations';
import { EntryExpiredCallback, ExpirableEntry, GarbageCollector } from './interface';
import { IterableCacheBackend } from '../contracts/cache-backend';
interface IntervalGarbageCollectorOptions<Key, Value> {
/**
* Cache backend instance.
*/
iterableBackend: IterableCacheBackend<Key, Value>;
/**
* Interval for running GC that checks for expired entries. <br/>
* Defaults to 15 seconds.
*/
checkInterval?: Seconds;
/**
* How many entries GC needs to check for expiration. <br/>
* Defaults to 100.
*/
iterateCount?: Threshold;
}
/**
* {@link GarbageCollector} implementation which evicts expired entries at regular interval (has no internal data structures). <br/>
* Eviction timer will fire periodically at the interval specified by {@link IntervalGarbageCollectorOptions.checkInterval}.
* When it fires, it will iterate over {@link IntervalGarbageCollectorOptions.iterateCount} backend entries, check
* if any of them is expired and evict if so. After that, it will schedule running for next interval.
* On next run, it will continue iteration from the entry it will stopped on previous run.
*
* @template Key Key type.
* @template Value Value type.
* @template Entry Cache entry type.
*/
declare class IntervalGarbageCollector<Key, Value, Entry extends ExpirableEntry> implements GarbageCollector<Entry> {
private readonly options;
private readonly getNextCacheEntry;
private iterateTimeoutId;
private entryExpiredCb;
constructor(options: IntervalGarbageCollectorOptions<Key, Value>);
get idle(): boolean;
get size(): number;
manage(): void;
update(): void;
leave(): void;
clear(): void;
setEntryExpiredCallback(cb: EntryExpiredCallback<Entry>): void;
private evictExpiredEntries;
private static fillWithDefaults;
private static createCacheEntriesCircularIterator;
}
export { IntervalGarbageCollector, IntervalGarbageCollectorOptions };