• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..21-May-2021-

LICENSEH A D21-May-2021765 1612

README.mdH A D21-May-20215.3 KiB159113

package.jsonH A D21-May-2021778 3433

README.md

1# lru cache
2
3A 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
10npm install lru-cache --save
11```
12
13## Usage:
14
15```javascript
16var 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
24cache.set("key", "value")
25cache.get("key") // "value"
26
27// non-string keys ARE fully supported
28// but note that it must be THE SAME object, not
29// just a JSON-equivalent object.
30var someObject = { a: 1 }
31cache.set(someObject, 'a value')
32// Object keys are not toString()-ed
33cache.set('[object Object]', 'a different value')
34assert.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
37assert.equal(cache.get({ a: 1 }), undefined)
38
39cache.reset()    // empty the cache
40```
41
42If you put more stuff in it, then items will fall out.
43
44If you try to put an oversized thing in it, then it'll fall out right
45away.
46
47## Options
48
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
52  to `Infinity`.
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.
79
80## API
81
82* `set(key, value, maxAge)`
83* `get(key) => value`
84
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.
88
89    If the key is not found, `get()` will return `undefined`.
90
91    The key and val can be any value.
92
93* `peek(key)`
94
95    Returns the key value (or `undefined` if not found) without
96    updating the "recently used"-ness of the key.
97
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
100    it's handy.)
101
102* `del(key)`
103
104    Deletes a key out of the cache.
105
106* `reset()`
107
108    Clear the cache entirely, throwing away all values.
109
110* `has(key)`
111
112    Check if a key is in the cache, without updating the recent-ness
113    or deleting it for being stale.
114
115* `forEach(function(value,key,cache), [thisp])`
116
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.)
120
121* `rforEach(function(value,key,cache), [thisp])`
122
123    The same as `cache.forEach(...)` but items are iterated over in
124    reverse order.  (ie, less recently used items are iterated over
125    first.)
126
127* `keys()`
128
129    Return an array of the keys in the cache.
130
131* `values()`
132
133    Return an array of the values in the cache.
134
135* `length`
136
137    Return total length of objects in cache taking into account
138    `length` options function.
139
140* `itemCount`
141
142    Return total quantity of objects currently in cache. Note, that
143    `stale` (see options) items are returned as part of this item
144    count.
145
146* `dump()`
147
148    Return an array of the cache entries ready for serialization and usage
149    with 'destinationCache.load(arr)`.
150
151* `load(cacheEntriesArray)`
152
153    Loads another cache entries array, obtained with `sourceCache.dump()`,
154    into the cache. The destination cache is reset before loading new entries
155
156* `prune()`
157
158    Manually iterates over the entire cache proactively pruning old entries
159