3 A cache object that deletes the least-recently-used items.
5 [![Build Status](https://travis-ci.org/isaacs/node-lru-cache.svg?branch=master)](https://travis-ci.org/isaacs/node-lru-cache) [![Coverage Status](https://coveralls.io/repos/isaacs/node-lru-cache/badge.svg?service=github)](https://coveralls.io/github/isaacs/node-lru-cache)
10 npm install lru-cache --save
16 var LRU = require("lru-cache")
17 , options = { max: 500
18 , length: function (n, key) { return n * 2 + key.length }
19 , dispose: function (key, n) { n.close() }
20 , maxAge: 1000 * 60 * 60 }
21 , cache = LRU(options)
22 , otherCache = LRU(50) // sets just the max size
24 cache.set("key", "value")
25 cache.get("key") // "value"
27 // non-string keys ARE fully supported
28 // but note that it must be THE SAME object, not
29 // just a JSON-equivalent object.
30 var someObject = { a: 1 }
31 cache.set(someObject, 'a value')
32 // Object keys are not toString()-ed
33 cache.set('[object Object]', 'a different value')
34 assert.equal(cache.get(someObject), 'a value')
35 // A similar object with same keys/values won't work,
36 // because it's a different object identity
37 assert.equal(cache.get({ a: 1 }), undefined)
39 cache.reset() // empty the cache
42 If you put more stuff in it, then items will fall out.
44 If you try to put an oversized thing in it, then it'll fall out right
49 * `max` The maximum size of the cache, checked by applying the length
50 function to all values in the cache. Not setting this is kind of
51 silly, since that's the whole purpose of this lib, but it defaults
53 * `maxAge` Maximum age in ms. Items are not pro-actively pruned out
54 as they age, but if you try to get an item that is too old, it'll
55 drop it and return undefined instead of giving it to you.
56 * `length` Function that is used to calculate the length of stored
57 items. If you're storing strings or buffers, then you probably want
58 to do something like `function(n, key){return n.length}`. The default is
59 `function(){return 1}`, which is fine if you want to store `max`
60 like-sized things. The item is passed as the first argument, and
61 the key is passed as the second argumnet.
62 * `dispose` Function that is called on items when they are dropped
63 from the cache. This can be handy if you want to close file
64 descriptors or do other cleanup tasks when items are no longer
65 accessible. Called with `key, value`. It's called *before*
66 actually removing the item from the internal cache, so if you want
67 to immediately put it back in, you'll have to do that in a
68 `nextTick` or `setTimeout` callback or it won't do anything.
69 * `stale` By default, if you set a `maxAge`, it'll only actually pull
70 stale items out of the cache when you `get(key)`. (That is, it's
71 not pre-emptively doing a `setTimeout` or anything.) If you set
72 `stale:true`, it'll return the stale value before deleting it. If
73 you don't set this, then it'll return `undefined` when you try to
74 get a stale entry, as if it had already been deleted.
75 * `noDisposeOnSet` By default, if you set a `dispose()` method, then
76 it'll be called whenever a `set()` operation overwrites an existing
77 key. If you set this option, `dispose()` will only be called when a
78 key falls out of the cache, not when it is overwritten.
82 * `set(key, value, maxAge)`
85 Both of these will update the "recently used"-ness of the key.
86 They do what you think. `maxAge` is optional and overrides the
87 cache `maxAge` option if provided.
89 If the key is not found, `get()` will return `undefined`.
91 The key and val can be any value.
95 Returns the key value (or `undefined` if not found) without
96 updating the "recently used"-ness of the key.
98 (If you find yourself using this a lot, you *might* be using the
99 wrong sort of data structure, but there are some use cases where
104 Deletes a key out of the cache.
108 Clear the cache entirely, throwing away all values.
112 Check if a key is in the cache, without updating the recent-ness
113 or deleting it for being stale.
115 * `forEach(function(value,key,cache), [thisp])`
117 Just like `Array.prototype.forEach`. Iterates over all the keys
118 in the cache, in order of recent-ness. (Ie, more recently used
119 items are iterated over first.)
121 * `rforEach(function(value,key,cache), [thisp])`
123 The same as `cache.forEach(...)` but items are iterated over in
124 reverse order. (ie, less recently used items are iterated over
129 Return an array of the keys in the cache.
133 Return an array of the values in the cache.
137 Return total length of objects in cache taking into account
138 `length` options function.
142 Return total quantity of objects currently in cache. Note, that
143 `stale` (see options) items are returned as part of this item
148 Return an array of the cache entries ready for serialization and usage
149 with 'destinationCache.load(arr)`.
151 * `load(cacheEntriesArray)`
153 Loads another cache entries array, obtained with `sourceCache.dump()`,
154 into the cache. The destination cache is reset before loading new entries
158 Manually iterates over the entire cache proactively pruning old entries