AK-21/Graphite-Industrial-Intelligence
0
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 5Useful when you need to cache something and limit memory usage.6 7Inspired 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## Install10 11```12$ npm install quick-lru13```14 15## Usage16 17```js18const QuickLRU = require('quick-lru');19 20const lru = new QuickLRU({maxSize: 1000});21 22lru.set('🦄', '🌈');23 24lru.has('🦄');25//=> true26 27lru.get('🦄');28//=> '🌈'29```30 31## API32 33### new QuickLRU(options?)34 35Returns a new instance.36 37### options38 39Type: `object`40 41#### maxSize42 43*Required*\44Type: `number`45 46The maximum number of items before evicting the least recently used items.47 48#### maxAge49 50Type: `number`\51Default: `Infinity`52 53The maximum number of milliseconds an item should remain in cache.54By default maxAge will be Infinity, which means that items will never expire.55 56Lazy expiration happens upon the next `write` or `read` call.57 58Individual expiration of an item can be specified by the `set(key, value, options)` method.59 60#### onEviction61 62*Optional*\63Type: `(key, value) => void`64 65Called right before an item is evicted from the cache.66 67Useful for side effects or for items like object URLs that need explicit cleanup (`revokeObjectURL`).68 69### Instance70 71The 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 73Both `key` and `value` can be of any type.74 75#### .set(key, value, options?)76 77Set an item. Returns the instance.78 79Individual 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 83Get an item.84 85#### .has(key)86 87Check if an item exists.88 89#### .peek(key)90 91Get an item without marking it as recently used.92 93#### .delete(key)94 95Delete an item.96 97Returns `true` if the item is removed or `false` if the item doesn't exist.98 99#### .clear()100 101Delete all items.102 103#### .resize(maxSize)104 105Update the `maxSize`, discarding items as necessary. Insertion order is mostly preserved, though this is not a strong guarantee.106 107Useful for on-the-fly tuning of cache sizes in live systems.108 109#### .keys()110 111Iterable for all the keys.112 113#### .values()114 115Iterable for all the values.116 117#### .entriesAscending()118 119Iterable for all entries, starting with the oldest (ascending in recency).120 121#### .entriesDescending()122 123Iterable for all entries, starting with the newest (descending in recency).124 125#### .size126 127The 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>140 