Initial commit
[yaffs-website] / node_modules / lru-cache / README.md
1 # lru cache
2
3 A cache object that deletes the least-recently-used items.
4
5 ## Usage:
6
7 ```javascript
8 var LRU = require("lru-cache")
9   , options = { max: 500
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
15
16 cache.set("key", "value")
17 cache.get("key") // "value"
18
19 cache.reset()    // empty the cache
20 ```
21
22 If you put more stuff in it, then items will fall out.
23
24 If you try to put an oversized thing in it, then it'll fall out right
25 away.
26
27 ## Keys should always be Strings or Numbers
28
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:
34
35 ```JavaScript
36 // Bad Example!  Dont' do this!
37 var cache = LRU()
38 var a = {}
39 var b = {}
40 cache.set(a, 'this is a')
41 cache.set(b, 'this is b')
42 console.log(cache.get(a)) // prints: 'this is b'
43 ```
44
45 ## Options
46
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
50   to `Infinity`.
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`
58   like-sized things.
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.
72
73 ## API
74
75 * `set(key, value, maxAge)`
76 * `get(key) => value`
77
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.
81
82 * `peek(key)`
83
84     Returns the key value (or `undefined` if not found) without
85     updating the "recently used"-ness of the key.
86
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
89     it's handy.)
90
91 * `del(key)`
92
93     Deletes a key out of the cache.
94
95 * `reset()`
96
97     Clear the cache entirely, throwing away all values.
98
99 * `has(key)`
100
101     Check if a key is in the cache, without updating the recent-ness
102     or deleting it for being stale.
103
104 * `forEach(function(value,key,cache), [thisp])`
105
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.)
109
110 * `keys()`
111
112     Return an array of the keys in the cache.
113
114 * `values()`
115
116     Return an array of the values in the cache.
117
118 * `length()`
119
120     Return total length of objects in cache taking into account
121     `length` options function.
122
123 * `itemCount`
124
125     Return total quantity of objects currently in cache. Note, that
126     `stale` (see options) items are returned as part of this item
127     count.
128
129 * `dump()`
130
131     Return an array of the cache entries ready for serialization and usage
132     with 'destinationCache.load(arr)`.
133
134 * `load(cacheEntriesArray)`
135
136     Loads another cache entries array, obtained with `sourceCache.dump()`,
137     into the cache. The destination cache is reset before loading new entries