3 A cache object that deletes the least-recently-used items.
8 var LRU = require("lru-cache")
10 , length: function (n) { return n * 2 }
11 , dispose: function (key, n) { n.close() }
12 , maxAge: 1000 * 60 * 60 }
13 , cache = LRU(options)
14 , otherCache = LRU(50) // sets just the max size
16 cache.set("key", "value")
17 cache.get("key") // "value"
19 cache.reset() // empty the cache
22 If you put more stuff in it, then items will fall out.
24 If you try to put an oversized thing in it, then it'll fall out right
27 ## Keys should always be Strings or Numbers
29 Note: this module will print warnings to `console.error` if you use a
30 key that is not a String or Number. Because items are stored in an
31 object, which coerces keys to a string, it won't go well for you if
32 you try to use a key that is not a unique string, it'll cause surprise
33 collisions. For example:
36 // Bad Example! Dont' do this!
40 cache.set(a, 'this is a')
41 cache.set(b, 'this is b')
42 console.log(cache.get(a)) // prints: 'this is b'
47 * `max` The maximum size of the cache, checked by applying the length
48 function to all values in the cache. Not setting this is kind of
49 silly, since that's the whole purpose of this lib, but it defaults
51 * `maxAge` Maximum age in ms. Items are not pro-actively pruned out
52 as they age, but if you try to get an item that is too old, it'll
53 drop it and return undefined instead of giving it to you.
54 * `length` Function that is used to calculate the length of stored
55 items. If you're storing strings or buffers, then you probably want
56 to do something like `function(n){return n.length}`. The default is
57 `function(n){return 1}`, which is fine if you want to store `max`
59 * `dispose` Function that is called on items when they are dropped
60 from the cache. This can be handy if you want to close file
61 descriptors or do other cleanup tasks when items are no longer
62 accessible. Called with `key, value`. It's called *before*
63 actually removing the item from the internal cache, so if you want
64 to immediately put it back in, you'll have to do that in a
65 `nextTick` or `setTimeout` callback or it won't do anything.
66 * `stale` By default, if you set a `maxAge`, it'll only actually pull
67 stale items out of the cache when you `get(key)`. (That is, it's
68 not pre-emptively doing a `setTimeout` or anything.) If you set
69 `stale:true`, it'll return the stale value before deleting it. If
70 you don't set this, then it'll return `undefined` when you try to
71 get a stale entry, as if it had already been deleted.
75 * `set(key, value, maxAge)`
78 Both of these will update the "recently used"-ness of the key.
79 They do what you think. `max` is optional and overrides the
80 cache `max` option if provided.
84 Returns the key value (or `undefined` if not found) without
85 updating the "recently used"-ness of the key.
87 (If you find yourself using this a lot, you *might* be using the
88 wrong sort of data structure, but there are some use cases where
93 Deletes a key out of the cache.
97 Clear the cache entirely, throwing away all values.
101 Check if a key is in the cache, without updating the recent-ness
102 or deleting it for being stale.
104 * `forEach(function(value,key,cache), [thisp])`
106 Just like `Array.prototype.forEach`. Iterates over all the keys
107 in the cache, in order of recent-ness. (Ie, more recently used
108 items are iterated over first.)
112 Return an array of the keys in the cache.
116 Return an array of the values in the cache.
120 Return total length of objects in cache taking into account
121 `length` options function.
125 Return total quantity of objects currently in cache. Note, that
126 `stale` (see options) items are returned as part of this item
131 Return an array of the cache entries ready for serialization and usage
132 with 'destinationCache.load(arr)`.
134 * `load(cacheEntriesArray)`
136 Loads another cache entries array, obtained with `sourceCache.dump()`,
137 into the cache. The destination cache is reset before loading new entries