| 1 | # quick-lru [](https://travis-ci.org/sindresorhus/quick-lru) [](https://coveralls.io/github/sindresorhus/quick-lru?branch=master)
|
|---|
| 2 |
|
|---|
| 3 | > Simple [“Least Recently Used” (LRU) cache](https://en.m.wikipedia.org/wiki/Cache_replacement_policies#Least_Recently_Used_.28LRU.29)
|
|---|
| 4 |
|
|---|
| 5 | Useful when you need to cache something and limit memory usage.
|
|---|
| 6 |
|
|---|
| 7 | Inspired by the [`hashlru` algorithm](https://github.com/dominictarr/hashlru#algorithm), but instead uses [`Map`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Map) to support keys of any type, not just strings, and values can be `undefined`.
|
|---|
| 8 |
|
|---|
| 9 | ## Install
|
|---|
| 10 |
|
|---|
| 11 | ```
|
|---|
| 12 | $ npm install quick-lru
|
|---|
| 13 | ```
|
|---|
| 14 |
|
|---|
| 15 | ## Usage
|
|---|
| 16 |
|
|---|
| 17 | ```js
|
|---|
| 18 | const QuickLRU = require('quick-lru');
|
|---|
| 19 |
|
|---|
| 20 | const lru = new QuickLRU({maxSize: 1000});
|
|---|
| 21 |
|
|---|
| 22 | lru.set('🦄', '🌈');
|
|---|
| 23 |
|
|---|
| 24 | lru.has('🦄');
|
|---|
| 25 | //=> true
|
|---|
| 26 |
|
|---|
| 27 | lru.get('🦄');
|
|---|
| 28 | //=> '🌈'
|
|---|
| 29 | ```
|
|---|
| 30 |
|
|---|
| 31 | ## API
|
|---|
| 32 |
|
|---|
| 33 | ### new QuickLRU(options?)
|
|---|
| 34 |
|
|---|
| 35 | Returns a new instance.
|
|---|
| 36 |
|
|---|
| 37 | ### options
|
|---|
| 38 |
|
|---|
| 39 | Type: `object`
|
|---|
| 40 |
|
|---|
| 41 | #### maxSize
|
|---|
| 42 |
|
|---|
| 43 | *Required*\
|
|---|
| 44 | Type: `number`
|
|---|
| 45 |
|
|---|
| 46 | The maximum number of items before evicting the least recently used items.
|
|---|
| 47 |
|
|---|
| 48 | #### maxAge
|
|---|
| 49 |
|
|---|
| 50 | Type: `number`\
|
|---|
| 51 | Default: `Infinity`
|
|---|
| 52 |
|
|---|
| 53 | The maximum number of milliseconds an item should remain in cache.
|
|---|
| 54 | By default maxAge will be Infinity, which means that items will never expire.
|
|---|
| 55 |
|
|---|
| 56 | Lazy expiration happens upon the next `write` or `read` call.
|
|---|
| 57 |
|
|---|
| 58 | Individual expiration of an item can be specified by the `set(key, value, options)` method.
|
|---|
| 59 |
|
|---|
| 60 | #### onEviction
|
|---|
| 61 |
|
|---|
| 62 | *Optional*\
|
|---|
| 63 | Type: `(key, value) => void`
|
|---|
| 64 |
|
|---|
| 65 | Called right before an item is evicted from the cache.
|
|---|
| 66 |
|
|---|
| 67 | Useful for side effects or for items like object URLs that need explicit cleanup (`revokeObjectURL`).
|
|---|
| 68 |
|
|---|
| 69 | ### Instance
|
|---|
| 70 |
|
|---|
| 71 | 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.
|
|---|
| 72 |
|
|---|
| 73 | Both `key` and `value` can be of any type.
|
|---|
| 74 |
|
|---|
| 75 | #### .set(key, value, options?)
|
|---|
| 76 |
|
|---|
| 77 | Set an item. Returns the instance.
|
|---|
| 78 |
|
|---|
| 79 | 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 on the constructor, otherwise the item will never expire.
|
|---|
| 80 |
|
|---|
| 81 | #### .get(key)
|
|---|
| 82 |
|
|---|
| 83 | Get an item.
|
|---|
| 84 |
|
|---|
| 85 | #### .has(key)
|
|---|
| 86 |
|
|---|
| 87 | Check if an item exists.
|
|---|
| 88 |
|
|---|
| 89 | #### .peek(key)
|
|---|
| 90 |
|
|---|
| 91 | Get an item without marking it as recently used.
|
|---|
| 92 |
|
|---|
| 93 | #### .delete(key)
|
|---|
| 94 |
|
|---|
| 95 | Delete an item.
|
|---|
| 96 |
|
|---|
| 97 | Returns `true` if the item is removed or `false` if the item doesn't exist.
|
|---|
| 98 |
|
|---|
| 99 | #### .clear()
|
|---|
| 100 |
|
|---|
| 101 | Delete all items.
|
|---|
| 102 |
|
|---|
| 103 | #### .resize(maxSize)
|
|---|
| 104 |
|
|---|
| 105 | Update the `maxSize`, discarding items as necessary. Insertion order is mostly preserved, though this is not a strong guarantee.
|
|---|
| 106 |
|
|---|
| 107 | Useful for on-the-fly tuning of cache sizes in live systems.
|
|---|
| 108 |
|
|---|
| 109 | #### .keys()
|
|---|
| 110 |
|
|---|
| 111 | Iterable for all the keys.
|
|---|
| 112 |
|
|---|
| 113 | #### .values()
|
|---|
| 114 |
|
|---|
| 115 | Iterable for all the values.
|
|---|
| 116 |
|
|---|
| 117 | #### .entriesAscending()
|
|---|
| 118 |
|
|---|
| 119 | Iterable for all entries, starting with the oldest (ascending in recency).
|
|---|
| 120 |
|
|---|
| 121 | #### .entriesDescending()
|
|---|
| 122 |
|
|---|
| 123 | Iterable for all entries, starting with the newest (descending in recency).
|
|---|
| 124 |
|
|---|
| 125 | #### .size
|
|---|
| 126 |
|
|---|
| 127 | The stored item count.
|
|---|
| 128 |
|
|---|
| 129 | ---
|
|---|
| 130 |
|
|---|
| 131 | <div align="center">
|
|---|
| 132 | <b>
|
|---|
| 133 | <a href="https://tidelift.com/subscription/pkg/npm-quick-lru?utm_source=npm-quick-lru&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
|
|---|
| 134 | </b>
|
|---|
| 135 | <br>
|
|---|
| 136 | <sub>
|
|---|
| 137 | Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
|
|---|
| 138 | </sub>
|
|---|
| 139 | </div>
|
|---|