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
29 cache.set(someObject, 'a value')
30 cache.set('[object Object]', 'a different value')
31 assert.equal(cache.get(someObject), 'a value')
33 cache.reset() // empty the cache
36 If you put more stuff in it, then items will fall out.
38 If you try to put an oversized thing in it, then it'll fall out right
43 * `max` The maximum size of the cache, checked by applying the length
44 function to all values in the cache. Not setting this is kind of
45 silly, since that's the whole purpose of this lib, but it defaults
47 * `maxAge` Maximum age in ms. Items are not pro-actively pruned out
48 as they age, but if you try to get an item that is too old, it'll
49 drop it and return undefined instead of giving it to you.
50 * `length` Function that is used to calculate the length of stored
51 items. If you're storing strings or buffers, then you probably want
52 to do something like `function(n, key){return n.length}`. The default is
53 `function(){return 1}`, which is fine if you want to store `max`
54 like-sized things. The item is passed as the first argument, and
55 the key is passed as the second argumnet.
56 * `dispose` Function that is called on items when they are dropped
57 from the cache. This can be handy if you want to close file
58 descriptors or do other cleanup tasks when items are no longer
59 accessible. Called with `key, value`. It's called *before*
60 actually removing the item from the internal cache, so if you want
61 to immediately put it back in, you'll have to do that in a
62 `nextTick` or `setTimeout` callback or it won't do anything.
63 * `stale` By default, if you set a `maxAge`, it'll only actually pull
64 stale items out of the cache when you `get(key)`. (That is, it's
65 not pre-emptively doing a `setTimeout` or anything.) If you set
66 `stale:true`, it'll return the stale value before deleting it. If
67 you don't set this, then it'll return `undefined` when you try to
68 get a stale entry, as if it had already been deleted.
72 * `set(key, value, maxAge)`
75 Both of these will update the "recently used"-ness of the key.
76 They do what you think. `maxAge` is optional and overrides the
77 cache `maxAge` option if provided.
79 If the key is not found, `get()` will return `undefined`.
81 The key and val can be any value.
85 Returns the key value (or `undefined` if not found) without
86 updating the "recently used"-ness of the key.
88 (If you find yourself using this a lot, you *might* be using the
89 wrong sort of data structure, but there are some use cases where
94 Deletes a key out of the cache.
98 Clear the cache entirely, throwing away all values.
102 Check if a key is in the cache, without updating the recent-ness
103 or deleting it for being stale.
105 * `forEach(function(value,key,cache), [thisp])`
107 Just like `Array.prototype.forEach`. Iterates over all the keys
108 in the cache, in order of recent-ness. (Ie, more recently used
109 items are iterated over first.)
111 * `rforEach(function(value,key,cache), [thisp])`
113 The same as `cache.forEach(...)` but items are iterated over in
114 reverse order. (ie, less recently used items are iterated over
119 Return an array of the keys in the cache.
123 Return an array of the values in the cache.
127 Return total length of objects in cache taking into account
128 `length` options function.
132 Return total quantity of objects currently in cache. Note, that
133 `stale` (see options) items are returned as part of this item
138 Return an array of the cache entries ready for serialization and usage
139 with 'destinationCache.load(arr)`.
141 * `load(cacheEntriesArray)`
143 Loads another cache entries array, obtained with `sourceCache.dump()`,
144 into the cache. The destination cache is reset before loading new entries
148 Manually iterates over the entire cache proactively pruning old entries