index.d.ts 3.2 KB

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