@aws-cdk/cloud-assembly-schema 2.22.0 → 2.24.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.jsii +29 -5
- package/.jsii.tabl.json +57 -57
- package/NOTICE +18 -1
- package/lib/integ-tests/schema.d.ts +10 -0
- package/lib/integ-tests/schema.js +1 -1
- package/lib/manifest.js +1 -1
- package/node_modules/lru-cache/LICENSE +1 -1
- package/node_modules/lru-cache/README.md +99 -632
- package/node_modules/lru-cache/index.js +251 -732
- package/node_modules/lru-cache/package.json +7 -17
- package/node_modules/semver/bin/semver.js +2 -1
- package/node_modules/semver/classes/semver.js +1 -1
- package/node_modules/semver/functions/inc.js +4 -1
- package/node_modules/semver/package.json +6 -5
- package/node_modules/yallist/LICENSE +15 -0
- package/node_modules/yallist/README.md +204 -0
- package/node_modules/yallist/iterator.js +8 -0
- package/node_modules/yallist/package.json +29 -0
- package/node_modules/yallist/yallist.js +426 -0
- package/package.json +5 -5
- package/schema/cloud-assembly.version.json +1 -1
- package/schema/integ.schema.json +9 -2
|
@@ -1,91 +1,25 @@
|
|
|
1
|
-
# lru
|
|
1
|
+
# lru cache
|
|
2
2
|
|
|
3
3
|
A cache object that deletes the least-recently-used items.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
and this cache will keep that many of the most recently accessed items.
|
|
5
|
+
[](https://travis-ci.org/isaacs/node-lru-cache) [](https://coveralls.io/github/isaacs/node-lru-cache)
|
|
7
6
|
|
|
8
|
-
|
|
9
|
-
There is no preemptive pruning of expired items, but you _may_ set a TTL
|
|
10
|
-
on the cache or on a single `set`. If you do so, it will treat expired
|
|
11
|
-
items as missing, and delete them when fetched.
|
|
7
|
+
## Installation:
|
|
12
8
|
|
|
13
|
-
|
|
14
|
-
available in JavaScript, and supports a wide diversity of use cases.
|
|
15
|
-
However, note that using some of the features will necessarily impact
|
|
16
|
-
performance, by causing the cache to have to do more work. See the
|
|
17
|
-
"Performance" section below.
|
|
18
|
-
|
|
19
|
-
## Installation
|
|
20
|
-
|
|
21
|
-
```bash
|
|
9
|
+
```javascript
|
|
22
10
|
npm install lru-cache --save
|
|
23
11
|
```
|
|
24
12
|
|
|
25
|
-
## Usage
|
|
26
|
-
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
// the number of most recently used items to keep.
|
|
36
|
-
// note that we may store fewer items than this if maxSize is hit.
|
|
37
|
-
|
|
38
|
-
max: 500, // <-- Technically optional, but see "Storage Bounds Safety" below
|
|
39
|
-
|
|
40
|
-
// if you wish to track item size, you must provide a maxSize
|
|
41
|
-
// note that we still will only keep up to max *actual items*,
|
|
42
|
-
// so size tracking may cause fewer than max items to be stored.
|
|
43
|
-
// At the extreme, a single item of maxSize size will cause everything
|
|
44
|
-
// else in the cache to be dropped when it is added. Use with caution!
|
|
45
|
-
// Note also that size tracking can negatively impact performance,
|
|
46
|
-
// though for most cases, only minimally.
|
|
47
|
-
maxSize: 5000,
|
|
48
|
-
|
|
49
|
-
// function to calculate size of items. useful if storing strings or
|
|
50
|
-
// buffers or other items where memory size depends on the object itself.
|
|
51
|
-
// also note that oversized items do NOT immediately get dropped from
|
|
52
|
-
// the cache, though they will cause faster turnover in the storage.
|
|
53
|
-
sizeCalculation: (value, key) => {
|
|
54
|
-
// return an positive integer which is the size of the item,
|
|
55
|
-
// if a positive integer is not returned, will use 0 as the size.
|
|
56
|
-
return 1
|
|
57
|
-
},
|
|
58
|
-
|
|
59
|
-
// function to call when the item is removed from the cache
|
|
60
|
-
// Note that using this can negatively impact performance.
|
|
61
|
-
dispose: (value, key) => {
|
|
62
|
-
freeFromMemoryOrWhatever(value)
|
|
63
|
-
},
|
|
64
|
-
|
|
65
|
-
// max time to live for items before they are considered stale
|
|
66
|
-
// note that stale items are NOT preemptively removed by default,
|
|
67
|
-
// and MAY live in the cache, contributing to its LRU max, long after
|
|
68
|
-
// they have expired.
|
|
69
|
-
// Also, as this cache is optimized for LRU/MRU operations, some of
|
|
70
|
-
// the staleness/TTL checks will reduce performance, as they will incur
|
|
71
|
-
// overhead by deleting items.
|
|
72
|
-
// Must be a positive integer in ms, defaults to 0, which means "no TTL"
|
|
73
|
-
ttl: 1000 * 60 * 5,
|
|
74
|
-
|
|
75
|
-
// return stale items from cache.get() before disposing of them
|
|
76
|
-
// boolean, default false
|
|
77
|
-
allowStale: false,
|
|
78
|
-
|
|
79
|
-
// update the age of items on cache.get(), renewing their TTL
|
|
80
|
-
// boolean, default false
|
|
81
|
-
updateAgeOnGet: false,
|
|
82
|
-
|
|
83
|
-
// update the age of items on cache.has(), renewing their TTL
|
|
84
|
-
// boolean, default false
|
|
85
|
-
updateAgeOnHas: false,
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
const cache = new LRU(options)
|
|
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 = new LRU(options)
|
|
22
|
+
, otherCache = new LRU(50) // sets just the max size
|
|
89
23
|
|
|
90
24
|
cache.set("key", "value")
|
|
91
25
|
cache.get("key") // "value"
|
|
@@ -102,598 +36,131 @@ assert.equal(cache.get(someObject), 'a value')
|
|
|
102
36
|
// because it's a different object identity
|
|
103
37
|
assert.equal(cache.get({ a: 1 }), undefined)
|
|
104
38
|
|
|
105
|
-
cache.
|
|
39
|
+
cache.reset() // empty the cache
|
|
106
40
|
```
|
|
107
41
|
|
|
108
42
|
If you put more stuff in it, then items will fall out.
|
|
109
43
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
* `max` - The maximum number (or size) of items that remain in the cache
|
|
113
|
-
(assuming no TTL pruning or explicit deletions). Note that fewer items
|
|
114
|
-
may be stored if size calculation is used, and `maxSize` is exceeded.
|
|
115
|
-
This must be a positive finite intger.
|
|
116
|
-
|
|
117
|
-
At least one of `max`, `maxSize`, or `TTL` is required. This must be a
|
|
118
|
-
positive integer if set.
|
|
119
|
-
|
|
120
|
-
**It is strongly recommended to set a `max` to prevent unbounded growth
|
|
121
|
-
of the cache.** See "Storage Bounds Safety" below.
|
|
122
|
-
|
|
123
|
-
* `maxSize` - Set to a positive integer to track the sizes of items added
|
|
124
|
-
to the cache, and automatically evict items in order to stay below this
|
|
125
|
-
size. Note that this may result in fewer than `max` items being stored.
|
|
126
|
-
|
|
127
|
-
Optional, must be a positive integer if provided. Required if other
|
|
128
|
-
size tracking features are used.
|
|
129
|
-
|
|
130
|
-
At least one of `max`, `maxSize`, or `TTL` is required. This must be a
|
|
131
|
-
positive integer if set.
|
|
132
|
-
|
|
133
|
-
Even if size tracking is enabled, **it is strongly recommended to set a
|
|
134
|
-
`max` to prevent unbounded growth of the cache.** See "Storage Bounds
|
|
135
|
-
Safety" below.
|
|
136
|
-
|
|
137
|
-
* `sizeCalculation` - Function used to calculate the size of stored
|
|
138
|
-
items. If you're storing strings or buffers, then you probably want to
|
|
139
|
-
do something like `n => n.length`. The item is passed as the first
|
|
140
|
-
argument, and the key is passed as the second argument.
|
|
141
|
-
|
|
142
|
-
This may be overridden by passing an options object to `cache.set()`.
|
|
143
|
-
|
|
144
|
-
Requires `maxSize` to be set.
|
|
145
|
-
|
|
146
|
-
Deprecated alias: `length`
|
|
44
|
+
If you try to put an oversized thing in it, then it'll fall out right
|
|
45
|
+
away.
|
|
147
46
|
|
|
148
|
-
|
|
149
|
-
fetches. Called with `fetchMethod(key, staleValue, { signal, options })`.
|
|
150
|
-
May return a Promise.
|
|
151
|
-
|
|
152
|
-
If `fetchMethod` is not provided, then `cache.fetch(key)` is equivalent
|
|
153
|
-
to `Promise.resolve(cache.get(key))`.
|
|
154
|
-
|
|
155
|
-
The `signal` object is an `AbortSignal`. If at any time,
|
|
156
|
-
`signal.aborted` is set to `true`, then that means that the fetch
|
|
157
|
-
should be abandoned. This may be passed along to async functions aware
|
|
158
|
-
of AbortController/AbortSignal behavior.
|
|
159
|
-
|
|
160
|
-
The `options` object is a union of the options that may be provided to
|
|
161
|
-
`set()` and `get()`. If they are modified, then that will result in
|
|
162
|
-
modifying the settings to `cache.set()` when the value is resolved.
|
|
163
|
-
For example, a DNS cache may update the TTL based on the value returned
|
|
164
|
-
from a remote DNS server by changing `options.ttl` in the
|
|
165
|
-
`fetchMethod`.
|
|
47
|
+
## Options
|
|
166
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`. Setting it to a non-number or negative number will
|
|
53
|
+
throw a `TypeError`. Setting it to 0 makes it be `Infinity`.
|
|
54
|
+
* `maxAge` Maximum age in ms. Items are not pro-actively pruned out
|
|
55
|
+
as they age, but if you try to get an item that is too old, it'll
|
|
56
|
+
drop it and return undefined instead of giving it to you.
|
|
57
|
+
Setting this to a negative value will make everything seem old!
|
|
58
|
+
Setting it to a non-number will throw a `TypeError`.
|
|
59
|
+
* `length` Function that is used to calculate the length of stored
|
|
60
|
+
items. If you're storing strings or buffers, then you probably want
|
|
61
|
+
to do something like `function(n, key){return n.length}`. The default is
|
|
62
|
+
`function(){return 1}`, which is fine if you want to store `max`
|
|
63
|
+
like-sized things. The item is passed as the first argument, and
|
|
64
|
+
the key is passed as the second argumnet.
|
|
167
65
|
* `dispose` Function that is called on items when they are dropped
|
|
168
|
-
from the cache
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
* `delete` Item was removed by explicit `cache.delete(key)` or by
|
|
190
|
-
calling `cache.clear()`, which deletes everything.
|
|
191
|
-
|
|
192
|
-
The `dispose()` method is _not_ called for canceled calls to
|
|
193
|
-
`fetchMethod()`. If you wish to handle evictions, overwrites, and
|
|
194
|
-
deletes of in-flight asynchronous fetches, you must use the
|
|
195
|
-
`AbortSignal` provided.
|
|
196
|
-
|
|
197
|
-
Optional, must be a function.
|
|
198
|
-
|
|
199
|
-
* `disposeAfter` The same as `dispose`, but called _after_ the entry is
|
|
200
|
-
completely removed and the cache is once again in a clean state.
|
|
201
|
-
|
|
202
|
-
It is safe to add an item right back into the cache at this point.
|
|
203
|
-
However, note that it is _very_ easy to inadvertently create infinite
|
|
204
|
-
recursion in this way.
|
|
205
|
-
|
|
206
|
-
The `disposeAfter()` method is _not_ called for canceled calls to
|
|
207
|
-
`fetchMethod()`. If you wish to handle evictions, overwrites, and
|
|
208
|
-
deletes of in-flight asynchronous fetches, you must use the
|
|
209
|
-
`AbortSignal` provided.
|
|
210
|
-
|
|
211
|
-
* `noDisposeOnSet` Set to `true` to suppress calling the `dispose()`
|
|
212
|
-
function if the entry key is still accessible within the cache.
|
|
213
|
-
|
|
214
|
-
This may be overridden by passing an options object to `cache.set()`.
|
|
215
|
-
|
|
216
|
-
Boolean, default `false`. Only relevant if `dispose` or `disposeAfter`
|
|
217
|
-
options are set.
|
|
218
|
-
|
|
219
|
-
* `ttl` - max time to live for items before they are considered stale.
|
|
220
|
-
Note that stale items are NOT preemptively removed by default, and MAY
|
|
221
|
-
live in the cache, contributing to its LRU max, long after they have
|
|
222
|
-
expired.
|
|
223
|
-
|
|
224
|
-
Also, as this cache is optimized for LRU/MRU operations, some of
|
|
225
|
-
the staleness/TTL checks will reduce performance, as they will incur
|
|
226
|
-
overhead by deleting from Map objects rather than simply throwing old
|
|
227
|
-
Map objects away.
|
|
228
|
-
|
|
229
|
-
This is not primarily a TTL cache, and does not make strong TTL
|
|
230
|
-
guarantees. There is no pre-emptive pruning of expired items, but you
|
|
231
|
-
_may_ set a TTL on the cache, and it will treat expired items as missing
|
|
232
|
-
when they are fetched, and delete them.
|
|
233
|
-
|
|
234
|
-
Optional, but must be a positive integer in ms if specified.
|
|
235
|
-
|
|
236
|
-
This may be overridden by passing an options object to `cache.set()`.
|
|
237
|
-
|
|
238
|
-
At least one of `max`, `maxSize`, or `TTL` is required. This must be a
|
|
239
|
-
positive integer if set.
|
|
240
|
-
|
|
241
|
-
Even if ttl tracking is enabled, **it is strongly recommended to set a
|
|
242
|
-
`max` to prevent unbounded growth of the cache.** See "Storage Bounds
|
|
243
|
-
Safety" below.
|
|
244
|
-
|
|
245
|
-
If ttl tracking is enabled, and `max` and `maxSize` are not set, and
|
|
246
|
-
`ttlAutopurge` is not set, then a warning will be emitted cautioning
|
|
247
|
-
about the potential for unbounded memory consumption.
|
|
248
|
-
|
|
249
|
-
Deprecated alias: `maxAge`
|
|
250
|
-
|
|
251
|
-
* `noUpdateTTL` - Boolean flag to tell the cache to not update the TTL when
|
|
252
|
-
setting a new value for an existing key (ie, when updating a value rather
|
|
253
|
-
than inserting a new value). Note that the TTL value is _always_ set
|
|
254
|
-
(if provided) when adding a new entry into the cache.
|
|
255
|
-
|
|
256
|
-
This may be passed as an option to `cache.set()`.
|
|
257
|
-
|
|
258
|
-
Boolean, default false.
|
|
259
|
-
|
|
260
|
-
* `ttlResolution` - Minimum amount of time in ms in which to check for
|
|
261
|
-
staleness. Defaults to `1`, which means that the current time is checked
|
|
262
|
-
at most once per millisecond.
|
|
263
|
-
|
|
264
|
-
Set to `0` to check the current time every time staleness is tested.
|
|
265
|
-
|
|
266
|
-
Note that setting this to a higher value _will_ improve performance
|
|
267
|
-
somewhat while using ttl tracking, albeit at the expense of keeping
|
|
268
|
-
stale items around a bit longer than intended.
|
|
269
|
-
|
|
270
|
-
* `ttlAutopurge` - Preemptively remove stale items from the cache.
|
|
271
|
-
|
|
272
|
-
Note that this may _significantly_ degrade performance, especially if
|
|
273
|
-
the cache is storing a large number of items. It is almost always best
|
|
274
|
-
to just leave the stale items in the cache, and let them fall out as
|
|
275
|
-
new items are added.
|
|
276
|
-
|
|
277
|
-
Note that this means that `allowStale` is a bit pointless, as stale
|
|
278
|
-
items will be deleted almost as soon as they expire.
|
|
279
|
-
|
|
280
|
-
Use with caution!
|
|
281
|
-
|
|
282
|
-
Boolean, default `false`
|
|
283
|
-
|
|
284
|
-
* `allowStale` - By default, if you set `ttl`, it'll only delete stale
|
|
285
|
-
items from the cache when you `get(key)`. That is, it's not
|
|
286
|
-
preemptively pruning items.
|
|
287
|
-
|
|
288
|
-
If you set `allowStale:true`, it'll return the stale value as well as
|
|
289
|
-
deleting it. If you don't set this, then it'll return `undefined` when
|
|
290
|
-
you try to get a stale entry.
|
|
291
|
-
|
|
292
|
-
Note that when a stale entry is fetched, _even if it is returned due to
|
|
293
|
-
`allowStale` being set_, it is removed from the cache immediately. You
|
|
294
|
-
can immediately put it back in the cache if you wish, thus resetting the
|
|
295
|
-
TTL.
|
|
296
|
-
|
|
297
|
-
This may be overridden by passing an options object to `cache.get()`.
|
|
298
|
-
The `cache.has()` method will always return `false` for stale items.
|
|
299
|
-
|
|
300
|
-
Boolean, default false, only relevant if `ttl` is set.
|
|
301
|
-
|
|
302
|
-
Deprecated alias: `stale`
|
|
303
|
-
|
|
304
|
-
* `updateAgeOnGet` - When using time-expiring entries with `ttl`, setting
|
|
305
|
-
this to `true` will make each item's age reset to 0 whenever it is
|
|
306
|
-
retrieved from cache with `get()`, causing it to not expire. (It can
|
|
307
|
-
still fall out of cache based on recency of use, of course.)
|
|
308
|
-
|
|
309
|
-
This may be overridden by passing an options object to `cache.get()`.
|
|
310
|
-
|
|
311
|
-
Boolean, default false, only relevant if `ttl` is set.
|
|
312
|
-
|
|
313
|
-
* `updateAgeOnHas` - When using time-expiring entries with `ttl`, setting
|
|
314
|
-
this to `true` will make each item's age reset to 0 whenever its presence
|
|
315
|
-
in the cache is checked with `has()`, causing it to not expire. (It can
|
|
316
|
-
still fall out of cache based on recency of use, of course.)
|
|
317
|
-
|
|
318
|
-
This may be overridden by passing an options object to `cache.has()`.
|
|
319
|
-
|
|
320
|
-
Boolean, default false, only relevant if `ttl` is set.
|
|
66
|
+
from the cache. This can be handy if you want to close file
|
|
67
|
+
descriptors or do other cleanup tasks when items are no longer
|
|
68
|
+
accessible. Called with `key, value`. It's called *before*
|
|
69
|
+
actually removing the item from the internal cache, so if you want
|
|
70
|
+
to immediately put it back in, you'll have to do that in a
|
|
71
|
+
`nextTick` or `setTimeout` callback or it won't do anything.
|
|
72
|
+
* `stale` By default, if you set a `maxAge`, it'll only actually pull
|
|
73
|
+
stale items out of the cache when you `get(key)`. (That is, it's
|
|
74
|
+
not pre-emptively doing a `setTimeout` or anything.) If you set
|
|
75
|
+
`stale:true`, it'll return the stale value before deleting it. If
|
|
76
|
+
you don't set this, then it'll return `undefined` when you try to
|
|
77
|
+
get a stale entry, as if it had already been deleted.
|
|
78
|
+
* `noDisposeOnSet` By default, if you set a `dispose()` method, then
|
|
79
|
+
it'll be called whenever a `set()` operation overwrites an existing
|
|
80
|
+
key. If you set this option, `dispose()` will only be called when a
|
|
81
|
+
key falls out of the cache, not when it is overwritten.
|
|
82
|
+
* `updateAgeOnGet` When using time-expiring entries with `maxAge`,
|
|
83
|
+
setting this to `true` will make each item's effective time update
|
|
84
|
+
to the current time whenever it is retrieved from cache, causing it
|
|
85
|
+
to not expire. (It can still fall out of cache based on recency of
|
|
86
|
+
use, of course.)
|
|
321
87
|
|
|
322
88
|
## API
|
|
323
89
|
|
|
324
|
-
* `
|
|
325
|
-
|
|
326
|
-
Create a new LRUCache. All options are documented above, and are on
|
|
327
|
-
the cache as public members.
|
|
328
|
-
|
|
329
|
-
* `cache.max`, `cache.maxSize`, `cache.allowStale`, `cache.noDisposeOnSet`,
|
|
330
|
-
`cache.sizeCalculation`, `cache.dispose`, `cache.maxSize`, `cache.ttl`,
|
|
331
|
-
`cache.updateAgeOnGet`, `cache.updateAgeOnHas`
|
|
332
|
-
|
|
333
|
-
All option names are exposed as public members on the cache object.
|
|
334
|
-
|
|
335
|
-
These are intended for read access only. Changing them during program
|
|
336
|
-
operation can cause undefined behavior.
|
|
337
|
-
|
|
338
|
-
* `cache.size`
|
|
339
|
-
|
|
340
|
-
The total number of items held in the cache at the current moment.
|
|
341
|
-
|
|
342
|
-
* `cache.calculatedSize`
|
|
343
|
-
|
|
344
|
-
The total size of items in cache when using size tracking.
|
|
345
|
-
|
|
346
|
-
* `set(key, value, [{ size, sizeCalculation, ttl, noDisposeOnSet }])`
|
|
347
|
-
|
|
348
|
-
Add a value to the cache.
|
|
349
|
-
|
|
350
|
-
Optional options object may contain `ttl` and `sizeCalculation` as
|
|
351
|
-
described above, which default to the settings on the cache object.
|
|
352
|
-
|
|
353
|
-
Options object my also include `size`, which will prevent calling the
|
|
354
|
-
`sizeCalculation` function and just use the specified number if it is a
|
|
355
|
-
positive integer, and `noDisposeOnSet` which will prevent calling a
|
|
356
|
-
`dispose` function in the case of overwrites.
|
|
357
|
-
|
|
358
|
-
Will update the recency of the entry.
|
|
359
|
-
|
|
360
|
-
Returns the cache object.
|
|
361
|
-
|
|
362
|
-
* `get(key, { updateAgeOnGet, allowStale } = {}) => value`
|
|
363
|
-
|
|
364
|
-
Return a value from the cache.
|
|
365
|
-
|
|
366
|
-
Will update the recency of the cache entry found.
|
|
367
|
-
|
|
368
|
-
If the key is not found, `get()` will return `undefined`. This can be
|
|
369
|
-
confusing when setting values specifically to `undefined`, as in
|
|
370
|
-
`cache.set(key, undefined)`. Use `cache.has()` to determine whether a
|
|
371
|
-
key is present in the cache at all.
|
|
372
|
-
|
|
373
|
-
* `async fetch(key, { updateAgeOnGet, allowStale, size, sizeCalculation, ttl, noDisposeOnSet } = {}) => Promise`
|
|
374
|
-
|
|
375
|
-
If the value is in the cache and not stale, then the returned Promise
|
|
376
|
-
resolves to the value.
|
|
377
|
-
|
|
378
|
-
If not in the cache, or beyond its TTL staleness, then
|
|
379
|
-
`fetchMethod(key, staleValue, options)` is called, and the value
|
|
380
|
-
returned will be added to the cache once resolved.
|
|
90
|
+
* `set(key, value, maxAge)`
|
|
91
|
+
* `get(key) => value`
|
|
381
92
|
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
93
|
+
Both of these will update the "recently used"-ness of the key.
|
|
94
|
+
They do what you think. `maxAge` is optional and overrides the
|
|
95
|
+
cache `maxAge` option if provided.
|
|
385
96
|
|
|
386
|
-
|
|
387
|
-
single time, and all will be resolved when the value is resolved, even
|
|
388
|
-
if different options are used.
|
|
97
|
+
If the key is not found, `get()` will return `undefined`.
|
|
389
98
|
|
|
390
|
-
|
|
391
|
-
for `Promise.resolve(cache.get(key))`.
|
|
99
|
+
The key and val can be any value.
|
|
392
100
|
|
|
393
|
-
|
|
394
|
-
aborted due to deletion, eviction, or being overwritten, then it is
|
|
395
|
-
added to the cache using the options provided.
|
|
101
|
+
* `peek(key)`
|
|
396
102
|
|
|
397
|
-
|
|
103
|
+
Returns the key value (or `undefined` if not found) without
|
|
104
|
+
updating the "recently used"-ness of the key.
|
|
398
105
|
|
|
399
|
-
|
|
106
|
+
(If you find yourself using this a lot, you *might* be using the
|
|
107
|
+
wrong sort of data structure, but there are some use cases where
|
|
108
|
+
it's handy.)
|
|
400
109
|
|
|
401
|
-
|
|
402
|
-
either on the cache or in the options object.
|
|
403
|
-
|
|
404
|
-
* `has(key, { updateAgeOnHas } = {}) => Boolean`
|
|
405
|
-
|
|
406
|
-
Check if a key is in the cache, without updating the recency of use.
|
|
407
|
-
Age is updated if `updateAgeOnHas` is set to `true` in either the
|
|
408
|
-
options or the constructor.
|
|
409
|
-
|
|
410
|
-
Will return `false` if the item is stale, even though it is technically
|
|
411
|
-
in the cache.
|
|
412
|
-
|
|
413
|
-
* `delete(key)`
|
|
110
|
+
* `del(key)`
|
|
414
111
|
|
|
415
112
|
Deletes a key out of the cache.
|
|
416
113
|
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
* `clear()`
|
|
114
|
+
* `reset()`
|
|
420
115
|
|
|
421
116
|
Clear the cache entirely, throwing away all values.
|
|
422
117
|
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
* `keys()`
|
|
426
|
-
|
|
427
|
-
Return a generator yielding the keys in the cache, in order from most
|
|
428
|
-
recently used to least recently used.
|
|
429
|
-
|
|
430
|
-
* `rkeys()`
|
|
118
|
+
* `has(key)`
|
|
431
119
|
|
|
432
|
-
|
|
433
|
-
|
|
120
|
+
Check if a key is in the cache, without updating the recent-ness
|
|
121
|
+
or deleting it for being stale.
|
|
434
122
|
|
|
435
|
-
* `
|
|
123
|
+
* `forEach(function(value,key,cache), [thisp])`
|
|
436
124
|
|
|
437
|
-
|
|
438
|
-
|
|
125
|
+
Just like `Array.prototype.forEach`. Iterates over all the keys
|
|
126
|
+
in the cache, in order of recent-ness. (Ie, more recently used
|
|
127
|
+
items are iterated over first.)
|
|
439
128
|
|
|
440
|
-
* `
|
|
129
|
+
* `rforEach(function(value,key,cache), [thisp])`
|
|
441
130
|
|
|
442
|
-
|
|
443
|
-
|
|
131
|
+
The same as `cache.forEach(...)` but items are iterated over in
|
|
132
|
+
reverse order. (ie, less recently used items are iterated over
|
|
133
|
+
first.)
|
|
444
134
|
|
|
445
|
-
* `
|
|
135
|
+
* `keys()`
|
|
446
136
|
|
|
447
|
-
Return
|
|
448
|
-
recently used to least recently used.
|
|
137
|
+
Return an array of the keys in the cache.
|
|
449
138
|
|
|
450
|
-
* `
|
|
139
|
+
* `values()`
|
|
451
140
|
|
|
452
|
-
Return
|
|
453
|
-
recently used to most recently used.
|
|
141
|
+
Return an array of the values in the cache.
|
|
454
142
|
|
|
455
|
-
* `
|
|
143
|
+
* `length`
|
|
456
144
|
|
|
457
|
-
|
|
458
|
-
|
|
145
|
+
Return total length of objects in cache taking into account
|
|
146
|
+
`length` options function.
|
|
459
147
|
|
|
460
|
-
|
|
148
|
+
* `itemCount`
|
|
461
149
|
|
|
462
|
-
|
|
463
|
-
item
|
|
150
|
+
Return total quantity of objects currently in cache. Note, that
|
|
151
|
+
`stale` (see options) items are returned as part of this item
|
|
152
|
+
count.
|
|
464
153
|
|
|
465
154
|
* `dump()`
|
|
466
155
|
|
|
467
|
-
Return an array of
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
Note: this returns an actual array, not a generator, so it can be more
|
|
471
|
-
easily passed around.
|
|
472
|
-
|
|
473
|
-
* `load(entries)`
|
|
156
|
+
Return an array of the cache entries ready for serialization and usage
|
|
157
|
+
with 'destinationCache.load(arr)`.
|
|
474
158
|
|
|
475
|
-
|
|
476
|
-
Note that the shape of the resulting cache may be different if the same
|
|
477
|
-
options are not used in both caches.
|
|
159
|
+
* `load(cacheEntriesArray)`
|
|
478
160
|
|
|
479
|
-
|
|
161
|
+
Loads another cache entries array, obtained with `sourceCache.dump()`,
|
|
162
|
+
into the cache. The destination cache is reset before loading new entries
|
|
480
163
|
|
|
481
|
-
|
|
482
|
-
`false` otherwise.
|
|
483
|
-
|
|
484
|
-
Deprecated alias: `prune`
|
|
485
|
-
|
|
486
|
-
* `getRemainingTTL(key)`
|
|
487
|
-
|
|
488
|
-
Return the number of ms left in the item's TTL. If item is not in
|
|
489
|
-
cache, returns `0`. Returns `Infinity` if item is in cache without a
|
|
490
|
-
defined TTL.
|
|
491
|
-
|
|
492
|
-
* `forEach(fn, [thisp])`
|
|
493
|
-
|
|
494
|
-
Call the `fn` function with each set of `fn(value, key, cache)` in the
|
|
495
|
-
LRU cache, from most recent to least recently used.
|
|
496
|
-
|
|
497
|
-
Does not affect recency of use.
|
|
498
|
-
|
|
499
|
-
If `thisp` is provided, function will be called in the `this`-context
|
|
500
|
-
of the provided object.
|
|
501
|
-
|
|
502
|
-
* `rforEach(fn, [thisp])`
|
|
503
|
-
|
|
504
|
-
Same as `cache.forEach(fn, thisp)`, but in order from least recently
|
|
505
|
-
used to most recently used.
|
|
506
|
-
|
|
507
|
-
* `pop()`
|
|
508
|
-
|
|
509
|
-
Evict the least recently used item, returning its value.
|
|
510
|
-
|
|
511
|
-
Returns `undefined` if cache is empty.
|
|
512
|
-
|
|
513
|
-
### Internal Methods and Properties
|
|
514
|
-
|
|
515
|
-
In order to optimize performance as much as possible, "private" members and
|
|
516
|
-
methods are exposed on the object as normal properties, rather than being
|
|
517
|
-
accessed via Symbols, private members, or closure variables.
|
|
518
|
-
|
|
519
|
-
**Do not use or rely on these.** They will change or be removed without
|
|
520
|
-
notice. They will cause undefined behavior if used inappropriately. There
|
|
521
|
-
is no need or reason to ever call them directly.
|
|
522
|
-
|
|
523
|
-
This documentation is here so that it is especially clear that this not
|
|
524
|
-
"undocumented" because someone forgot; it _is_ documented, and the
|
|
525
|
-
documentation is telling you not to do it.
|
|
526
|
-
|
|
527
|
-
**Do not report bugs that stem from using these properties.** They will be
|
|
528
|
-
ignored.
|
|
529
|
-
|
|
530
|
-
* `initializeTTLTracking()` Set up the cache for tracking TTLs
|
|
531
|
-
* `updateItemAge(index)` Called when an item age is updated, by internal ID
|
|
532
|
-
* `setItemTTL(index)` Called when an item ttl is updated, by internal ID
|
|
533
|
-
* `isStale(index)` Called to check an item's staleness, by internal ID
|
|
534
|
-
* `initializeSizeTracking()` Set up the cache for tracking item size.
|
|
535
|
-
Called automatically when a size is specified.
|
|
536
|
-
* `removeItemSize(index)` Updates the internal size calculation when an
|
|
537
|
-
item is removed or modified, by internal ID
|
|
538
|
-
* `addItemSize(index)` Updates the internal size calculation when an item
|
|
539
|
-
is added or modified, by internal ID
|
|
540
|
-
* `indexes()` An iterator over the non-stale internal IDs, from most
|
|
541
|
-
recently to least recently used.
|
|
542
|
-
* `rindexes()` An iterator over the non-stale internal IDs, from least
|
|
543
|
-
recently to most recently used.
|
|
544
|
-
* `newIndex()` Create a new internal ID, either reusing a deleted ID,
|
|
545
|
-
evicting the least recently used ID, or walking to the end of the
|
|
546
|
-
allotted space.
|
|
547
|
-
* `evict()` Evict the least recently used internal ID, returning its ID.
|
|
548
|
-
Does not do any bounds checking.
|
|
549
|
-
* `connect(p, n)` Connect the `p` and `n` internal IDs in the linked list.
|
|
550
|
-
* `moveToTail(index)` Move the specified internal ID to the most recently
|
|
551
|
-
used position.
|
|
552
|
-
* `keyMap` Map of keys to internal IDs
|
|
553
|
-
* `keyList` List of keys by internal ID
|
|
554
|
-
* `valList` List of values by internal ID
|
|
555
|
-
* `sizes` List of calculated sizes by internal ID
|
|
556
|
-
* `ttls` List of TTL values by internal ID
|
|
557
|
-
* `starts` List of start time values by internal ID
|
|
558
|
-
* `next` Array of "next" pointers by internal ID
|
|
559
|
-
* `prev` Array of "previous" pointers by internal ID
|
|
560
|
-
* `head` Internal ID of least recently used item
|
|
561
|
-
* `tail` Internal ID of most recently used item
|
|
562
|
-
* `free` Stack of deleted internal IDs
|
|
563
|
-
|
|
564
|
-
## Storage Bounds Safety
|
|
565
|
-
|
|
566
|
-
This implementation aims to be as flexible as possible, within the limits
|
|
567
|
-
of safe memory consumption and optimal performance.
|
|
568
|
-
|
|
569
|
-
At initial object creation, storage is allocated for `max` items. If `max`
|
|
570
|
-
is set to zero, then some performance is lost, and item count is unbounded.
|
|
571
|
-
Either `maxSize` or `ttl` _must_ be set if `max` is not specified.
|
|
572
|
-
|
|
573
|
-
If `maxSize` is set, then this creates a safe limit on the maximum storage
|
|
574
|
-
consumed, but without the performance benefits of pre-allocation. When
|
|
575
|
-
`maxSize` is set, every item _must_ provide a size, either via the
|
|
576
|
-
`sizeCalculation` method provided to the constructor, or via a `size` or
|
|
577
|
-
`sizeCalculation` option provided to `cache.set()`. The size of every item
|
|
578
|
-
_must_ be a positive integer.
|
|
579
|
-
|
|
580
|
-
If neither `max` nor `maxSize` are set, then `ttl` tracking must be
|
|
581
|
-
enabled. Note that, even when tracking item `ttl`, items are _not_
|
|
582
|
-
preemptively deleted when they become stale, unless `ttlAutopurge` is
|
|
583
|
-
enabled. Instead, they are only purged the next time the key is requested.
|
|
584
|
-
Thus, if `ttlAutopurge`, `max`, and `maxSize` are all not set, then the
|
|
585
|
-
cache will potentially grow unbounded.
|
|
586
|
-
|
|
587
|
-
In this case, a warning is printed to standard error. Future versions may
|
|
588
|
-
require the use of `ttlAutopurge` if `max` and `maxSize` are not specified.
|
|
589
|
-
|
|
590
|
-
If you truly wish to use a cache that is bound _only_ by TTL expiration,
|
|
591
|
-
consider using a `Map` object, and calling `setTimeout` to delete entries
|
|
592
|
-
when they expire. It will perform much better than an LRU cache.
|
|
593
|
-
|
|
594
|
-
Here is an implementation you may use, under the same [license](./LICENSE)
|
|
595
|
-
as this package:
|
|
596
|
-
|
|
597
|
-
```js
|
|
598
|
-
// a storage-unbounded ttl cache that is not an lru-cache
|
|
599
|
-
const cache = {
|
|
600
|
-
data: new Map(),
|
|
601
|
-
timers: new Map(),
|
|
602
|
-
set: (k, v, ttl) => {
|
|
603
|
-
if (cache.timers.has(k)) {
|
|
604
|
-
clearTimeout(cache.timers.get(k))
|
|
605
|
-
}
|
|
606
|
-
cache.timers.set(k, setTimeout(() => cache.del(k), ttl))
|
|
607
|
-
cache.data.set(k, v)
|
|
608
|
-
},
|
|
609
|
-
get: k => cache.data.get(k),
|
|
610
|
-
has: k => cache.data.has(k),
|
|
611
|
-
delete: k => {
|
|
612
|
-
if (cache.timers.has(k)) {
|
|
613
|
-
clearTimeout(cache.timers.get(k))
|
|
614
|
-
}
|
|
615
|
-
cache.timers.delete(k)
|
|
616
|
-
return cache.data.delete(k)
|
|
617
|
-
},
|
|
618
|
-
clear: () => {
|
|
619
|
-
cache.data.clear()
|
|
620
|
-
for (const v of cache.timers.values()) {
|
|
621
|
-
clearTimeout(v)
|
|
622
|
-
}
|
|
623
|
-
cache.timers.clear()
|
|
624
|
-
}
|
|
625
|
-
}
|
|
626
|
-
```
|
|
627
|
-
|
|
628
|
-
## Performance
|
|
629
|
-
|
|
630
|
-
As of January 2022, version 7 of this library is one of the most performant
|
|
631
|
-
LRU cache implementations in JavaScript.
|
|
632
|
-
|
|
633
|
-
Benchmarks can be extremely difficult to get right. In particular, the
|
|
634
|
-
performance of set/get/delete operations on objects will vary _wildly_
|
|
635
|
-
depending on the type of key used. V8 is highly optimized for objects with
|
|
636
|
-
keys that are short strings, especially integer numeric strings. Thus any
|
|
637
|
-
benchmark which tests _solely_ using numbers as keys will tend to find that
|
|
638
|
-
an object-based approach performs the best.
|
|
639
|
-
|
|
640
|
-
Note that coercing _anything_ to strings to use as object keys is unsafe,
|
|
641
|
-
unless you can be 100% certain that no other type of value will be used.
|
|
642
|
-
For example:
|
|
643
|
-
|
|
644
|
-
```js
|
|
645
|
-
const myCache = {}
|
|
646
|
-
const set = (k, v) => myCache[k] = v
|
|
647
|
-
const get = (k) => myCache[k]
|
|
648
|
-
|
|
649
|
-
set({}, 'please hang onto this for me')
|
|
650
|
-
set('[object Object]', 'oopsie')
|
|
651
|
-
```
|
|
164
|
+
* `prune()`
|
|
652
165
|
|
|
653
|
-
|
|
654
|
-
of large (especially: deep) object graphs can be incredibly costly, with
|
|
655
|
-
several "tipping points" where it increases exponentially. As a result,
|
|
656
|
-
putting that off until later can make it much worse, and less predictable.
|
|
657
|
-
If a library performs well, but only in a scenario where the object graph is
|
|
658
|
-
kept shallow, then that won't help you if you are using large objects as
|
|
659
|
-
keys.
|
|
660
|
-
|
|
661
|
-
In general, when attempting to use a library to improve performance (such
|
|
662
|
-
as a cache like this one), it's best to choose an option that will perform
|
|
663
|
-
well in the sorts of scenarios where you'll actually use it.
|
|
664
|
-
|
|
665
|
-
This library is optimized for repeated gets and minimizing eviction time,
|
|
666
|
-
since that is the expected need of a LRU. Set operations are somewhat
|
|
667
|
-
slower on average than a few other options, in part because of that
|
|
668
|
-
optimization. It is assumed that you'll be caching some costly operation,
|
|
669
|
-
ideally as rarely as possible, so optimizing set over get would be unwise.
|
|
670
|
-
|
|
671
|
-
If performance matters to you:
|
|
672
|
-
|
|
673
|
-
1. If it's at all possible to use small integer values as keys, and you can
|
|
674
|
-
guarantee that no other types of values will be used as keys, then do
|
|
675
|
-
that, and use a cache such as
|
|
676
|
-
[lru-fast](https://npmjs.com/package/lru-fast), or [mnemonist's
|
|
677
|
-
LRUCache](https://yomguithereal.github.io/mnemonist/lru-cache) which
|
|
678
|
-
uses an Object as its data store.
|
|
679
|
-
2. Failing that, if at all possible, use short non-numeric strings (ie,
|
|
680
|
-
less than 256 characters) as your keys, and use [mnemonist's
|
|
681
|
-
LRUCache](https://yomguithereal.github.io/mnemonist/lru-cache).
|
|
682
|
-
3. If the types of your keys will be long strings, strings that look like
|
|
683
|
-
floats, `null`, objects, or some mix of types, or if you aren't sure,
|
|
684
|
-
then this library will work well for you.
|
|
685
|
-
4. Do not use a `dispose` function, size tracking, or especially ttl
|
|
686
|
-
behavior, unless absolutely needed. These features are convenient, and
|
|
687
|
-
necessary in some use cases, and every attempt has been made to make the
|
|
688
|
-
performance impact minimal, but it isn't nothing.
|
|
689
|
-
|
|
690
|
-
## Breaking Changes in Version 7
|
|
691
|
-
|
|
692
|
-
This library changed to a different algorithm and internal data structure
|
|
693
|
-
in version 7, yielding significantly better performance, albeit with
|
|
694
|
-
some subtle changes as a result.
|
|
695
|
-
|
|
696
|
-
If you were relying on the internals of LRUCache in version 6 or before, it
|
|
697
|
-
probably will not work in version 7 and above.
|
|
698
|
-
|
|
699
|
-
For more info, see the [change log](CHANGELOG.md).
|
|
166
|
+
Manually iterates over the entire cache proactively pruning old entries
|