Initial commit
[yaffs-website] / node_modules / cross-spawn / node_modules / lru-cache / README.md
1 # lru cache
2
3 A cache object that deletes the least-recently-used items.
4
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)
6
7 ## Installation:
8
9 ```javascript
10 npm install lru-cache --save
11 ```
12
13 ## Usage:
14
15 ```javascript
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
23
24 cache.set("key", "value")
25 cache.get("key") // "value"
26
27 // non-string keys ARE fully supported
28 var someObject = {}
29 cache.set(someObject, 'a value')
30 cache.set('[object Object]', 'a different value')
31 assert.equal(cache.get(someObject), 'a value')
32
33 cache.reset()    // empty the cache
34 ```
35
36 If you put more stuff in it, then items will fall out.
37
38 If you try to put an oversized thing in it, then it'll fall out right
39 away.
40
41 ## Options
42
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
46   to `Infinity`.
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.
69
70 ## API
71
72 * `set(key, value, maxAge)`
73 * `get(key) => value`
74
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.
78
79     If the key is not found, `get()` will return `undefined`.
80
81     The key and val can be any value.
82
83 * `peek(key)`
84
85     Returns the key value (or `undefined` if not found) without
86     updating the "recently used"-ness of the key.
87
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
90     it's handy.)
91
92 * `del(key)`
93
94     Deletes a key out of the cache.
95
96 * `reset()`
97
98     Clear the cache entirely, throwing away all values.
99
100 * `has(key)`
101
102     Check if a key is in the cache, without updating the recent-ness
103     or deleting it for being stale.
104
105 * `forEach(function(value,key,cache), [thisp])`
106
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.)
110
111 * `rforEach(function(value,key,cache), [thisp])`
112
113     The same as `cache.forEach(...)` but items are iterated over in
114     reverse order.  (ie, less recently used items are iterated over
115     first.)
116
117 * `keys()`
118
119     Return an array of the keys in the cache.
120
121 * `values()`
122
123     Return an array of the values in the cache.
124
125 * `length`
126
127     Return total length of objects in cache taking into account
128     `length` options function.
129
130 * `itemCount`
131
132     Return total quantity of objects currently in cache. Note, that
133     `stale` (see options) items are returned as part of this item
134     count.
135
136 * `dump()`
137
138     Return an array of the cache entries ready for serialization and usage
139     with 'destinationCache.load(arr)`.
140
141 * `load(cacheEntriesArray)`
142
143     Loads another cache entries array, obtained with `sourceCache.dump()`,
144     into the cache. The destination cache is reset before loading new entries
145
146 * `prune()`
147
148     Manually iterates over the entire cache proactively pruning old entries