projen 0.54.8 → 0.54.11

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