| 1 | declare namespace QuickLRU {
|
|---|
| 2 | interface Options<KeyType, ValueType> {
|
|---|
| 3 | /**
|
|---|
| 4 | The maximum number of milliseconds an item should remain in the cache.
|
|---|
| 5 |
|
|---|
| 6 | @default Infinity
|
|---|
| 7 |
|
|---|
| 8 | By default, `maxAge` will be `Infinity`, which means that items will never expire.
|
|---|
| 9 | Lazy expiration upon the next write or read call.
|
|---|
| 10 |
|
|---|
| 11 | Individual expiration of an item can be specified by the `set(key, value, maxAge)` method.
|
|---|
| 12 | */
|
|---|
| 13 | readonly maxAge?: number;
|
|---|
| 14 |
|
|---|
| 15 | /**
|
|---|
| 16 | The maximum number of items before evicting the least recently used items.
|
|---|
| 17 | */
|
|---|
| 18 | readonly maxSize: number;
|
|---|
| 19 |
|
|---|
| 20 | /**
|
|---|
| 21 | Called right before an item is evicted from the cache.
|
|---|
| 22 |
|
|---|
| 23 | Useful for side effects or for items like object URLs that need explicit cleanup (`revokeObjectURL`).
|
|---|
| 24 | */
|
|---|
| 25 | onEviction?: (key: KeyType, value: ValueType) => void;
|
|---|
| 26 | }
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | declare class QuickLRU<KeyType, ValueType>
|
|---|
| 30 | implements Iterable<[KeyType, ValueType]> {
|
|---|
| 31 | /**
|
|---|
| 32 | The stored item count.
|
|---|
| 33 | */
|
|---|
| 34 | readonly size: number;
|
|---|
| 35 |
|
|---|
| 36 | /**
|
|---|
| 37 | Simple ["Least Recently Used" (LRU) cache](https://en.m.wikipedia.org/wiki/Cache_replacement_policies#Least_Recently_Used_.28LRU.29).
|
|---|
| 38 |
|
|---|
| 39 | The instance is [`iterable`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Iteration_protocols) so you can use it directly in a [`forβ¦of`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/for...of) loop.
|
|---|
| 40 |
|
|---|
| 41 | @example
|
|---|
| 42 | ```
|
|---|
| 43 | import QuickLRU = require('quick-lru');
|
|---|
| 44 |
|
|---|
| 45 | const lru = new QuickLRU({maxSize: 1000});
|
|---|
| 46 |
|
|---|
| 47 | lru.set('π¦', 'π');
|
|---|
| 48 |
|
|---|
| 49 | lru.has('π¦');
|
|---|
| 50 | //=> true
|
|---|
| 51 |
|
|---|
| 52 | lru.get('π¦');
|
|---|
| 53 | //=> 'π'
|
|---|
| 54 | ```
|
|---|
| 55 | */
|
|---|
| 56 | constructor(options: QuickLRU.Options<KeyType, ValueType>);
|
|---|
| 57 |
|
|---|
| 58 | [Symbol.iterator](): IterableIterator<[KeyType, ValueType]>;
|
|---|
| 59 |
|
|---|
| 60 | /**
|
|---|
| 61 | Set an item. Returns the instance.
|
|---|
| 62 |
|
|---|
| 63 | Individual expiration of an item can be specified with the `maxAge` option. If not specified, the global `maxAge` value will be used in case it is specified in the constructor, otherwise the item will never expire.
|
|---|
| 64 |
|
|---|
| 65 | @returns The list instance.
|
|---|
| 66 | */
|
|---|
| 67 | set(key: KeyType, value: ValueType, options?: {maxAge?: number}): this;
|
|---|
| 68 |
|
|---|
| 69 | /**
|
|---|
| 70 | Get an item.
|
|---|
| 71 |
|
|---|
| 72 | @returns The stored item or `undefined`.
|
|---|
| 73 | */
|
|---|
| 74 | get(key: KeyType): ValueType | undefined;
|
|---|
| 75 |
|
|---|
| 76 | /**
|
|---|
| 77 | Check if an item exists.
|
|---|
| 78 | */
|
|---|
| 79 | has(key: KeyType): boolean;
|
|---|
| 80 |
|
|---|
| 81 | /**
|
|---|
| 82 | Get an item without marking it as recently used.
|
|---|
| 83 |
|
|---|
| 84 | @returns The stored item or `undefined`.
|
|---|
| 85 | */
|
|---|
| 86 | peek(key: KeyType): ValueType | undefined;
|
|---|
| 87 |
|
|---|
| 88 | /**
|
|---|
| 89 | Delete an item.
|
|---|
| 90 |
|
|---|
| 91 | @returns `true` if the item is removed or `false` if the item doesn't exist.
|
|---|
| 92 | */
|
|---|
| 93 | delete(key: KeyType): boolean;
|
|---|
| 94 |
|
|---|
| 95 | /**
|
|---|
| 96 | Delete all items.
|
|---|
| 97 | */
|
|---|
| 98 | clear(): void;
|
|---|
| 99 |
|
|---|
| 100 | /**
|
|---|
| 101 | Update the `maxSize` in-place, discarding items as necessary. Insertion order is mostly preserved, though this is not a strong guarantee.
|
|---|
| 102 |
|
|---|
| 103 | Useful for on-the-fly tuning of cache sizes in live systems.
|
|---|
| 104 | */
|
|---|
| 105 | resize(maxSize: number): void;
|
|---|
| 106 |
|
|---|
| 107 | /**
|
|---|
| 108 | Iterable for all the keys.
|
|---|
| 109 | */
|
|---|
| 110 | keys(): IterableIterator<KeyType>;
|
|---|
| 111 |
|
|---|
| 112 | /**
|
|---|
| 113 | Iterable for all the values.
|
|---|
| 114 | */
|
|---|
| 115 | values(): IterableIterator<ValueType>;
|
|---|
| 116 |
|
|---|
| 117 | /**
|
|---|
| 118 | Iterable for all entries, starting with the oldest (ascending in recency).
|
|---|
| 119 | */
|
|---|
| 120 | entriesAscending(): IterableIterator<[KeyType, ValueType]>;
|
|---|
| 121 |
|
|---|
| 122 | /**
|
|---|
| 123 | Iterable for all entries, starting with the newest (descending in recency).
|
|---|
| 124 | */
|
|---|
| 125 | entriesDescending(): IterableIterator<[KeyType, ValueType]>;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | export = QuickLRU;
|
|---|