index.d.ts 3.1 KB

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