export interface Options { /** * The target maximum number of items before evicting the least recently used items. * * __Note:__ The cache keeps two generations of items to avoid expensive deletes, so it can hold up to `2 × maxSize` items, though `size` never reports more than `maxSize`. */ readonly maxSize: number; /** * The maximum number of milliseconds an item should remain in the cache. Items expire lazily, on the next read or write that touches them. * * Individual items can override it with `set(key, value, {maxAge})`. * * @default Infinity */ readonly maxAge?: number; /** * Called when an item leaves the cache due to LRU pressure, TTL expiration, `resize()` or `evict()`. Useful for cleaning up items like object URLs (`revokeObjectURL`). * * It is not called for `delete()`, `clear()`, or when `set()` overwrites an existing key. */ onEviction?: (key: KeyType, value: ValueType) => void; } /** * Simple ["Least Recently Used" (LRU) cache](https://en.m.wikipedia.org/wiki/Cache_replacement_policies#Least_Recently_Used_.28LRU.29). * * The instance is an iterable of `[key, value]` pairs, oldest first. * * @example * ``` * import QuickLRU from '@jbrowse/quick-lru' * * const lru = new QuickLRU({maxSize: 1000}) * * lru.set('🦄', '🌈') * * lru.has('🦄') * //=> true * * lru.get('🦄') * //=> '🌈' * ``` */ export default class QuickLRU implements Iterable<[ KeyType, ValueType ]> { #private; constructor(options: Options); /** * Get an item, marking it as recently used. * * @returns The stored item or `undefined`. */ get(key: KeyType): ValueType | undefined; /** * Set an item, marking it as recently used. * * The `maxAge` option overrides the cache-wide `maxAge` for this item. * * @returns The cache instance. */ set(key: KeyType, value: ValueType, { maxAge }?: { maxAge?: number; }): this; /** Check if an item exists, without marking it as recently used. */ has(key: KeyType): boolean; /** * Get an item without marking it as recently used. * * @returns The stored item or `undefined`. */ peek(key: KeyType): ValueType | undefined; /** * Get the remaining time to live of an item, in milliseconds. Neither marks the item as recently used nor removes it if expired, so the result may be negative. * * @returns The remaining milliseconds, `Infinity` if the item never expires, or `undefined` if it is not in the cache. */ expiresIn(key: KeyType): number | undefined; /** * Delete an item. * * @returns `true` if the item was removed, `false` if it did not exist. */ delete(key: KeyType): boolean; /** Delete all items. */ clear(): void; /** Update `maxSize` in place, evicting the least recently used items if the cache no longer fits. */ resize(maxSize: number): void; /** * Evict the least recently used items. Always keeps at least one item. * * @param count - The number of items to evict. */ evict(count?: number): void; /** The stored item count, capped at `maxSize`. */ get size(): number; get maxSize(): number; get maxAge(): number; /** Iterate over all entries, oldest first. */ entriesAscending(): IterableIterator<[KeyType, ValueType]>; /** Iterate over all entries, newest first. */ entriesDescending(): IterableIterator<[KeyType, ValueType]>; /** Iterate over all entries, oldest first. Alias for `entriesAscending()`. */ entries(): IterableIterator<[KeyType, ValueType]>; [Symbol.iterator](): IterableIterator<[KeyType, ValueType]>; keys(): IterableIterator; values(): IterableIterator; forEach(callbackFunction: (value: ValueType, key: KeyType, map: QuickLRU) => void, thisArgument?: unknown): void; get [Symbol.toStringTag](): string; toString(): string; }