@aws-amplify/graphql-model-transformer 0.17.0-rds-support.0 → 0.17.0-rdsv2preview.0
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/CHANGELOG.md +8 -1
- package/lib/graphql-model-transformer.d.ts.map +1 -1
- package/lib/graphql-model-transformer.js +12 -8
- package/lib/graphql-model-transformer.js.map +1 -1
- package/lib/rds-lambda.zip +0 -0
- package/lib/resolvers/generators/rds-vtl-generator.d.ts.map +1 -1
- package/lib/resolvers/generators/rds-vtl-generator.js +1 -1
- package/lib/resolvers/generators/rds-vtl-generator.js.map +1 -1
- package/lib/resolvers/rds/index.d.ts +1 -0
- package/lib/resolvers/rds/index.d.ts.map +1 -1
- package/lib/resolvers/rds/index.js +1 -0
- package/lib/resolvers/rds/index.js.map +1 -1
- package/lib/resolvers/rds/mutation.d.ts.map +1 -1
- package/lib/resolvers/rds/mutation.js +7 -2
- package/lib/resolvers/rds/mutation.js.map +1 -1
- package/lib/resolvers/rds/query.d.ts +2 -0
- package/lib/resolvers/rds/query.d.ts.map +1 -0
- package/lib/resolvers/rds/query.js +25 -0
- package/lib/resolvers/rds/query.js.map +1 -0
- package/lib/resolvers/rds/resolver.d.ts.map +1 -1
- package/lib/resolvers/rds/resolver.js +10 -9
- package/lib/resolvers/rds/resolver.js.map +1 -1
- package/package.json +7 -7
- package/rds-lambda/clients/DBClient.ts +3 -3
- package/rds-lambda/clients/mysql/MySQLClient.ts +54 -15
- package/rds-lambda/clients/mysql/MySQLPasswordClient.ts +4 -3
- package/rds-lambda/interfaces/BaseRequest.ts +2 -2
- package/rds-lambda/interfaces/ListRequest.ts +11 -0
- package/rds-lambda/node_modules/.package-lock.json +399 -391
- package/rds-lambda/node_modules/@aws-sdk/abort-controller/package.json +2 -2
- package/rds-lambda/node_modules/@aws-sdk/client-ssm/package.json +30 -30
- package/rds-lambda/node_modules/@aws-sdk/client-sso/package.json +26 -26
- package/rds-lambda/node_modules/@aws-sdk/client-sso-oidc/package.json +26 -26
- package/rds-lambda/node_modules/@aws-sdk/client-sts/package.json +29 -29
- package/rds-lambda/node_modules/@aws-sdk/config-resolver/package.json +5 -5
- package/rds-lambda/node_modules/@aws-sdk/credential-provider-env/package.json +3 -3
- package/rds-lambda/node_modules/@aws-sdk/credential-provider-imds/package.json +5 -5
- package/rds-lambda/node_modules/@aws-sdk/credential-provider-ini/package.json +9 -9
- package/rds-lambda/node_modules/@aws-sdk/credential-provider-node/package.json +10 -10
- package/rds-lambda/node_modules/@aws-sdk/credential-provider-process/package.json +4 -4
- package/rds-lambda/node_modules/@aws-sdk/credential-provider-sso/package.json +6 -6
- package/rds-lambda/node_modules/@aws-sdk/credential-provider-web-identity/package.json +3 -3
- package/rds-lambda/node_modules/@aws-sdk/fetch-http-handler/package.json +5 -5
- package/rds-lambda/node_modules/@aws-sdk/hash-node/package.json +2 -2
- package/rds-lambda/node_modules/@aws-sdk/invalid-dependency/package.json +2 -2
- package/rds-lambda/node_modules/@aws-sdk/middleware-content-length/package.json +3 -3
- package/rds-lambda/node_modules/@aws-sdk/middleware-endpoint/package.json +7 -7
- package/rds-lambda/node_modules/@aws-sdk/middleware-host-header/package.json +3 -3
- package/rds-lambda/node_modules/@aws-sdk/middleware-logger/package.json +2 -2
- package/rds-lambda/node_modules/@aws-sdk/middleware-recursion-detection/package.json +3 -3
- package/rds-lambda/node_modules/@aws-sdk/middleware-retry/package.json +7 -7
- package/rds-lambda/node_modules/@aws-sdk/middleware-sdk-sts/package.json +6 -6
- package/rds-lambda/node_modules/@aws-sdk/middleware-serde/package.json +2 -2
- package/rds-lambda/node_modules/@aws-sdk/middleware-signing/package.json +6 -6
- package/rds-lambda/node_modules/@aws-sdk/middleware-stack/package.json +2 -2
- package/rds-lambda/node_modules/@aws-sdk/middleware-user-agent/package.json +4 -4
- package/rds-lambda/node_modules/@aws-sdk/node-config-provider/package.json +4 -4
- package/rds-lambda/node_modules/@aws-sdk/node-http-handler/package.json +5 -5
- package/rds-lambda/node_modules/@aws-sdk/property-provider/package.json +2 -2
- package/rds-lambda/node_modules/@aws-sdk/protocol-http/package.json +2 -2
- package/rds-lambda/node_modules/@aws-sdk/querystring-builder/package.json +2 -2
- package/rds-lambda/node_modules/@aws-sdk/querystring-parser/package.json +2 -2
- package/rds-lambda/node_modules/@aws-sdk/service-error-classification/package.json +2 -2
- package/rds-lambda/node_modules/@aws-sdk/shared-ini-file-loader/package.json +2 -2
- package/rds-lambda/node_modules/@aws-sdk/signature-v4/package.json +4 -4
- package/rds-lambda/node_modules/@aws-sdk/smithy-client/package.json +3 -3
- package/rds-lambda/node_modules/@aws-sdk/token-providers/package.json +5 -5
- package/rds-lambda/node_modules/@aws-sdk/types/package.json +1 -1
- package/rds-lambda/node_modules/@aws-sdk/url-parser/package.json +3 -3
- package/rds-lambda/node_modules/@aws-sdk/util-defaults-mode-browser/package.json +4 -4
- package/rds-lambda/node_modules/@aws-sdk/util-defaults-mode-node/package.json +7 -7
- package/rds-lambda/node_modules/@aws-sdk/util-endpoints/package.json +2 -2
- package/rds-lambda/node_modules/@aws-sdk/util-middleware/package.json +2 -2
- package/rds-lambda/node_modules/@aws-sdk/util-retry/package.json +3 -3
- package/rds-lambda/node_modules/@aws-sdk/util-user-agent-browser/package.json +3 -3
- package/rds-lambda/node_modules/@aws-sdk/util-user-agent-node/package.json +4 -4
- package/rds-lambda/node_modules/@aws-sdk/util-waiter/package.json +3 -3
- package/rds-lambda/node_modules/caniuse-lite/README.md +2 -88
- package/rds-lambda/node_modules/caniuse-lite/package.json +2 -6
- package/rds-lambda/node_modules/electron-to-chromium/full-chromium-versions.json +1 -1
- package/rds-lambda/node_modules/electron-to-chromium/full-versions.json +1 -1
- package/rds-lambda/node_modules/electron-to-chromium/package.json +1 -1
- package/rds-lambda/node_modules/long/LICENSE +202 -202
- package/rds-lambda/node_modules/long/README.md +246 -280
- package/rds-lambda/node_modules/long/package.json +32 -43
- package/rds-lambda/node_modules/mysql2/README.md +2 -8
- package/rds-lambda/node_modules/mysql2/node_modules/lru-cache/LICENSE +1 -1
- package/rds-lambda/node_modules/mysql2/node_modules/lru-cache/README.md +102 -775
- package/rds-lambda/node_modules/mysql2/node_modules/lru-cache/package.json +9 -49
- package/rds-lambda/node_modules/mysql2/node_modules/yallist/LICENSE +15 -0
- package/rds-lambda/node_modules/mysql2/node_modules/yallist/README.md +204 -0
- package/rds-lambda/node_modules/mysql2/node_modules/yallist/package.json +29 -0
- package/rds-lambda/node_modules/mysql2/package.json +15 -21
- package/rds-lambda/package-lock.json +798 -782
- package/rds-lambda/package.json +1 -1
- package/rds-lambda/utils/rds_utils.ts +107 -0
- package/src/__tests__/model-transformer.test.ts +89 -1
- package/src/__tests__/test-utils/rds_utils.test.ts +299 -0
- package/src/graphql-model-transformer.ts +5 -1
- package/src/resolvers/generators/rds-vtl-generator.ts +2 -1
- package/src/resolvers/rds/index.ts +1 -0
- package/src/resolvers/rds/mutation.ts +7 -2
- package/src/resolvers/rds/query.ts +33 -0
- package/src/resolvers/rds/resolver.ts +25 -16
- package/tsconfig.tsbuildinfo +1 -1
- package/rds-lambda/node_modules/long/umd/package.json +0 -3
|
@@ -1,76 +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
|
-
want to keep, and this cache will keep that many of the most
|
|
7
|
-
recently accessed items.
|
|
5
|
+
[](https://travis-ci.org/isaacs/node-lru-cache) [](https://coveralls.io/github/isaacs/node-lru-cache)
|
|
8
6
|
|
|
9
|
-
|
|
10
|
-
guarantees. There is no preemptive pruning of expired items by
|
|
11
|
-
default, but you _may_ set a TTL on the cache or on a single
|
|
12
|
-
`set`. If you do so, it will treat expired items as missing, and
|
|
13
|
-
delete them when fetched. If you are more interested in TTL
|
|
14
|
-
caching than LRU caching, check out
|
|
15
|
-
[@isaacs/ttlcache](http://npm.im/@isaacs/ttlcache).
|
|
7
|
+
## Installation:
|
|
16
8
|
|
|
17
|
-
|
|
18
|
-
implementations available in JavaScript, and supports a wide
|
|
19
|
-
diversity of use cases. However, note that using some of the
|
|
20
|
-
features will necessarily impact performance, by causing the
|
|
21
|
-
cache to have to do more work. See the "Performance" section
|
|
22
|
-
below.
|
|
23
|
-
|
|
24
|
-
## Installation
|
|
25
|
-
|
|
26
|
-
```bash
|
|
9
|
+
```javascript
|
|
27
10
|
npm install lru-cache --save
|
|
28
11
|
```
|
|
29
12
|
|
|
30
|
-
## Usage
|
|
31
|
-
|
|
32
|
-
```js
|
|
33
|
-
const LRU = require('lru-cache')
|
|
34
|
-
|
|
35
|
-
// At least one of 'max', 'ttl', or 'maxSize' is required, to prevent
|
|
36
|
-
// unsafe unbounded storage.
|
|
37
|
-
//
|
|
38
|
-
// In most cases, it's best to specify a max for performance, so all
|
|
39
|
-
// the required memory allocation is done up-front.
|
|
40
|
-
//
|
|
41
|
-
// All the other options are optional, see the sections below for
|
|
42
|
-
// documentation on what each one does. Most of them can be
|
|
43
|
-
// overridden for specific items in get()/set()
|
|
44
|
-
const options = {
|
|
45
|
-
max: 500,
|
|
46
|
-
|
|
47
|
-
// for use with tracking overall storage size
|
|
48
|
-
maxSize: 5000,
|
|
49
|
-
sizeCalculation: (value, key) => {
|
|
50
|
-
return 1
|
|
51
|
-
},
|
|
52
|
-
|
|
53
|
-
// for use when you need to clean up something when objects
|
|
54
|
-
// are evicted from the cache
|
|
55
|
-
dispose: (value, key) => {
|
|
56
|
-
freeFromMemoryOrWhatever(value)
|
|
57
|
-
},
|
|
13
|
+
## Usage:
|
|
58
14
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
// async method to use for cache.fetch(), for
|
|
69
|
-
// stale-while-revalidate type of behavior
|
|
70
|
-
fetchMethod: async (key, staleValue, { options, signal }) => {}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
const cache = new LRU(options)
|
|
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
|
|
74
23
|
|
|
75
24
|
cache.set("key", "value")
|
|
76
25
|
cache.get("key") // "value"
|
|
@@ -87,753 +36,131 @@ assert.equal(cache.get(someObject), 'a value')
|
|
|
87
36
|
// because it's a different object identity
|
|
88
37
|
assert.equal(cache.get({ a: 1 }), undefined)
|
|
89
38
|
|
|
90
|
-
cache.
|
|
39
|
+
cache.reset() // empty the cache
|
|
91
40
|
```
|
|
92
41
|
|
|
93
42
|
If you put more stuff in it, then items will fall out.
|
|
94
43
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
### `max`
|
|
98
|
-
|
|
99
|
-
The maximum number of items that remain in the cache (assuming no
|
|
100
|
-
TTL pruning or explicit deletions). Note that fewer items may be
|
|
101
|
-
stored if size calculation is used, and `maxSize` is exceeded.
|
|
102
|
-
This must be a positive finite intger.
|
|
103
|
-
|
|
104
|
-
At least one of `max`, `maxSize`, or `TTL` is required. This
|
|
105
|
-
must be a positive integer if set.
|
|
106
|
-
|
|
107
|
-
**It is strongly recommended to set a `max` to prevent unbounded
|
|
108
|
-
growth of the cache.** See "Storage Bounds Safety" below.
|
|
109
|
-
|
|
110
|
-
### `maxSize`
|
|
111
|
-
|
|
112
|
-
Set to a positive integer to track the sizes of items added to
|
|
113
|
-
the cache, and automatically evict items in order to stay below
|
|
114
|
-
this size. Note that this may result in fewer than `max` items
|
|
115
|
-
being stored.
|
|
116
|
-
|
|
117
|
-
Attempting to add an item to the cache whose calculated size is
|
|
118
|
-
greater that this amount will be a no-op. The item will not be
|
|
119
|
-
cached, and no other items will be evicted.
|
|
120
|
-
|
|
121
|
-
Optional, must be a positive integer if provided.
|
|
122
|
-
|
|
123
|
-
Sets `maxItemSize` to the same value, unless a different value is
|
|
124
|
-
provided for `maxItemSize`.
|
|
125
|
-
|
|
126
|
-
At least one of `max`, `maxSize`, or `TTL` is required. This
|
|
127
|
-
must be a positive integer if set.
|
|
128
|
-
|
|
129
|
-
Even if size tracking is enabled, **it is strongly recommended to
|
|
130
|
-
set a `max` to prevent unbounded growth of the cache.** See
|
|
131
|
-
"Storage Bounds Safety" below.
|
|
132
|
-
|
|
133
|
-
### `maxEntrySize`
|
|
134
|
-
|
|
135
|
-
Set to a positive integer to track the sizes of items added to
|
|
136
|
-
the cache, and prevent caching any item over a given size.
|
|
137
|
-
Attempting to add an item whose calculated size is greater than
|
|
138
|
-
this amount will be a no-op. The item will not be cached, and no
|
|
139
|
-
other items will be evicted.
|
|
140
|
-
|
|
141
|
-
Optional, must be a positive integer if provided. Defaults to
|
|
142
|
-
the value of `maxSize` if provided.
|
|
143
|
-
|
|
144
|
-
### `sizeCalculation`
|
|
145
|
-
|
|
146
|
-
Function used to calculate the size of stored items. If you're
|
|
147
|
-
storing strings or buffers, then you probably want to do
|
|
148
|
-
something like `n => n.length`. The item is passed as the first
|
|
149
|
-
argument, and the key is passed as the second argument.
|
|
150
|
-
|
|
151
|
-
This may be overridden by passing an options object to
|
|
152
|
-
`cache.set()`.
|
|
153
|
-
|
|
154
|
-
Requires `maxSize` to be set. If the resulting calculated size
|
|
155
|
-
is greater than `maxSize`, then the item will not be added to the
|
|
156
|
-
cache.
|
|
157
|
-
|
|
158
|
-
Deprecated alias: `length`
|
|
159
|
-
|
|
160
|
-
### `fetchMethod`
|
|
161
|
-
|
|
162
|
-
Function that is used to make background asynchronous fetches.
|
|
163
|
-
Called with `fetchMethod(key, staleValue, { signal, options,
|
|
164
|
-
context })`. May return a Promise.
|
|
165
|
-
|
|
166
|
-
If `fetchMethod` is not provided, then `cache.fetch(key)` is
|
|
167
|
-
equivalent to `Promise.resolve(cache.get(key))`.
|
|
168
|
-
|
|
169
|
-
The `signal` object is an `AbortSignal` if that's available in
|
|
170
|
-
the global object, otherwise it's a pretty close polyfill.
|
|
171
|
-
|
|
172
|
-
If at any time, `signal.aborted` is set to `true`, or if the
|
|
173
|
-
`signal.onabort` method is called, or if it emits an `'abort'`
|
|
174
|
-
event which you can listen to with `addEventListener`, then that
|
|
175
|
-
means that the fetch should be abandoned. This may be passed
|
|
176
|
-
along to async functions aware of AbortController/AbortSignal
|
|
177
|
-
behavior.
|
|
178
|
-
|
|
179
|
-
The `options` object is a union of the options that may be
|
|
180
|
-
provided to `set()` and `get()`. If they are modified, then that
|
|
181
|
-
will result in modifying the settings to `cache.set()` when the
|
|
182
|
-
value is resolved. For example, a DNS cache may update the TTL
|
|
183
|
-
based on the value returned from a remote DNS server by changing
|
|
184
|
-
`options.ttl` in the `fetchMethod`.
|
|
185
|
-
|
|
186
|
-
### `fetchContext`
|
|
187
|
-
|
|
188
|
-
Arbitrary data that can be passed to the `fetchMethod` as the
|
|
189
|
-
`context` option.
|
|
190
|
-
|
|
191
|
-
Note that this will only be relevant when the `cache.fetch()`
|
|
192
|
-
call needs to call `fetchMethod()`. Thus, any data which will
|
|
193
|
-
meaningfully vary the fetch response needs to be present in the
|
|
194
|
-
key. This is primarily intended for including `x-request-id`
|
|
195
|
-
headers and the like for debugging purposes, which do not affect
|
|
196
|
-
the `fetchMethod()` response.
|
|
197
|
-
|
|
198
|
-
### `noDeleteOnFetchRejection`
|
|
199
|
-
|
|
200
|
-
If a `fetchMethod` throws an error or returns a rejected promise,
|
|
201
|
-
then by default, any existing stale value will be removed from
|
|
202
|
-
the cache.
|
|
203
|
-
|
|
204
|
-
If `noDeleteOnFetchRejection` is set to `true`, then this
|
|
205
|
-
behavior is suppressed, and the stale value remains in the cache
|
|
206
|
-
in the case of a rejected `fetchMethod`.
|
|
207
|
-
|
|
208
|
-
This is important in cases where a `fetchMethod` is _only_ called
|
|
209
|
-
as a background update while the stale value is returned, when
|
|
210
|
-
`allowStale` is used.
|
|
211
|
-
|
|
212
|
-
This may be set in calls to `fetch()`, or defaulted on the
|
|
213
|
-
constructor.
|
|
214
|
-
|
|
215
|
-
### `dispose`
|
|
216
|
-
|
|
217
|
-
Function that is called on items when they are dropped from the
|
|
218
|
-
cache, as `this.dispose(value, key, reason)`.
|
|
219
|
-
|
|
220
|
-
This can be handy if you want to close file descriptors or do
|
|
221
|
-
other cleanup tasks when items are no longer stored in the cache.
|
|
222
|
-
|
|
223
|
-
**NOTE**: It is called *before* the item has been fully removed
|
|
224
|
-
from the cache, so if you want to put it right back in, you need
|
|
225
|
-
to wait until the next tick. If you try to add it back in during
|
|
226
|
-
the `dispose()` function call, it will break things in subtle and
|
|
227
|
-
weird ways.
|
|
228
|
-
|
|
229
|
-
Unlike several other options, this may _not_ be overridden by
|
|
230
|
-
passing an option to `set()`, for performance reasons. If
|
|
231
|
-
disposal functions may vary between cache entries, then the
|
|
232
|
-
entire list must be scanned on every cache swap, even if no
|
|
233
|
-
disposal function is in use.
|
|
234
|
-
|
|
235
|
-
The `reason` will be one of the following strings, corresponding
|
|
236
|
-
to the reason for the item's deletion:
|
|
237
|
-
|
|
238
|
-
* `evict` Item was evicted to make space for a new addition
|
|
239
|
-
* `set` Item was overwritten by a new value
|
|
240
|
-
* `delete` Item was removed by explicit `cache.delete(key)` or by
|
|
241
|
-
calling `cache.clear()`, which deletes everything.
|
|
242
|
-
|
|
243
|
-
The `dispose()` method is _not_ called for canceled calls to
|
|
244
|
-
`fetchMethod()`. If you wish to handle evictions, overwrites,
|
|
245
|
-
and deletes of in-flight asynchronous fetches, you must use the
|
|
246
|
-
`AbortSignal` provided.
|
|
247
|
-
|
|
248
|
-
Optional, must be a function.
|
|
249
|
-
|
|
250
|
-
### `disposeAfter`
|
|
251
|
-
|
|
252
|
-
The same as `dispose`, but called _after_ the entry is completely
|
|
253
|
-
removed and the cache is once again in a clean state.
|
|
254
|
-
|
|
255
|
-
It is safe to add an item right back into the cache at this
|
|
256
|
-
point. However, note that it is _very_ easy to inadvertently
|
|
257
|
-
create infinite recursion in this way.
|
|
258
|
-
|
|
259
|
-
The `disposeAfter()` method is _not_ called for canceled calls to
|
|
260
|
-
`fetchMethod()`. If you wish to handle evictions, overwrites,
|
|
261
|
-
and deletes of in-flight asynchronous fetches, you must use the
|
|
262
|
-
`AbortSignal` provided.
|
|
263
|
-
|
|
264
|
-
### `noDisposeOnSet`
|
|
265
|
-
|
|
266
|
-
Set to `true` to suppress calling the `dispose()` function if the
|
|
267
|
-
entry key is still accessible within the cache.
|
|
268
|
-
|
|
269
|
-
This may be overridden by passing an options object to
|
|
270
|
-
`cache.set()`.
|
|
271
|
-
|
|
272
|
-
Boolean, default `false`. Only relevant if `dispose` or
|
|
273
|
-
`disposeAfter` options are set.
|
|
274
|
-
|
|
275
|
-
### `ttl`
|
|
276
|
-
|
|
277
|
-
Max time to live for items before they are considered stale.
|
|
278
|
-
Note that stale items are NOT preemptively removed by default,
|
|
279
|
-
and MAY live in the cache, contributing to its LRU max, long
|
|
280
|
-
after they have expired.
|
|
281
|
-
|
|
282
|
-
Also, as this cache is optimized for LRU/MRU operations, some of
|
|
283
|
-
the staleness/TTL checks will reduce performance.
|
|
284
|
-
|
|
285
|
-
This is not primarily a TTL cache, and does not make strong TTL
|
|
286
|
-
guarantees. There is no pre-emptive pruning of expired items,
|
|
287
|
-
but you _may_ set a TTL on the cache, and it will treat expired
|
|
288
|
-
items as missing when they are fetched, and delete them.
|
|
289
|
-
|
|
290
|
-
Optional, but must be a positive integer in ms if specified.
|
|
291
|
-
|
|
292
|
-
This may be overridden by passing an options object to
|
|
293
|
-
`cache.set()`.
|
|
294
|
-
|
|
295
|
-
At least one of `max`, `maxSize`, or `TTL` is required. This
|
|
296
|
-
must be a positive integer if set.
|
|
297
|
-
|
|
298
|
-
Even if ttl tracking is enabled, **it is strongly recommended to
|
|
299
|
-
set a `max` to prevent unbounded growth of the cache.** See
|
|
300
|
-
"Storage Bounds Safety" below.
|
|
301
|
-
|
|
302
|
-
If ttl tracking is enabled, and `max` and `maxSize` are not set,
|
|
303
|
-
and `ttlAutopurge` is not set, then a warning will be emitted
|
|
304
|
-
cautioning about the potential for unbounded memory consumption.
|
|
305
|
-
|
|
306
|
-
Deprecated alias: `maxAge`
|
|
307
|
-
|
|
308
|
-
### `noUpdateTTL`
|
|
309
|
-
|
|
310
|
-
Boolean flag to tell the cache to not update the TTL when setting
|
|
311
|
-
a new value for an existing key (ie, when updating a value rather
|
|
312
|
-
than inserting a new value). Note that the TTL value is _always_
|
|
313
|
-
set (if provided) when adding a new entry into the cache.
|
|
314
|
-
|
|
315
|
-
This may be passed as an option to `cache.set()`.
|
|
316
|
-
|
|
317
|
-
Boolean, default false.
|
|
318
|
-
|
|
319
|
-
### `ttlResolution`
|
|
320
|
-
|
|
321
|
-
Minimum amount of time in ms in which to check for staleness.
|
|
322
|
-
Defaults to `1`, which means that the current time is checked at
|
|
323
|
-
most once per millisecond.
|
|
324
|
-
|
|
325
|
-
Set to `0` to check the current time every time staleness is
|
|
326
|
-
tested.
|
|
327
|
-
|
|
328
|
-
Note that setting this to a higher value _will_ improve
|
|
329
|
-
performance somewhat while using ttl tracking, albeit at the
|
|
330
|
-
expense of keeping stale items around a bit longer than intended.
|
|
331
|
-
|
|
332
|
-
### `ttlAutopurge`
|
|
333
|
-
|
|
334
|
-
Preemptively remove stale items from the cache.
|
|
335
|
-
|
|
336
|
-
Note that this may _significantly_ degrade performance,
|
|
337
|
-
especially if the cache is storing a large number of items. It
|
|
338
|
-
is almost always best to just leave the stale items in the cache,
|
|
339
|
-
and let them fall out as new items are added.
|
|
340
|
-
|
|
341
|
-
Note that this means that `allowStale` is a bit pointless, as
|
|
342
|
-
stale items will be deleted almost as soon as they expire.
|
|
44
|
+
If you try to put an oversized thing in it, then it'll fall out right
|
|
45
|
+
away.
|
|
343
46
|
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
Boolean, default `false`
|
|
347
|
-
|
|
348
|
-
### `allowStale`
|
|
349
|
-
|
|
350
|
-
By default, if you set `ttl`, it'll only delete stale items from
|
|
351
|
-
the cache when you `get(key)`. That is, it's not preemptively
|
|
352
|
-
pruning items.
|
|
353
|
-
|
|
354
|
-
If you set `allowStale:true`, it'll return the stale value as
|
|
355
|
-
well as deleting it. If you don't set this, then it'll return
|
|
356
|
-
`undefined` when you try to get a stale entry.
|
|
357
|
-
|
|
358
|
-
Note that when a stale entry is fetched, _even if it is returned
|
|
359
|
-
due to `allowStale` being set_, it is removed from the cache
|
|
360
|
-
immediately. You can immediately put it back in the cache if you
|
|
361
|
-
wish, thus resetting the TTL.
|
|
362
|
-
|
|
363
|
-
This may be overridden by passing an options object to
|
|
364
|
-
`cache.get()`. The `cache.has()` method will always return
|
|
365
|
-
`false` for stale items.
|
|
366
|
-
|
|
367
|
-
Boolean, default false, only relevant if `ttl` is set.
|
|
368
|
-
|
|
369
|
-
Deprecated alias: `stale`
|
|
370
|
-
|
|
371
|
-
### `noDeleteOnStaleGet`
|
|
372
|
-
|
|
373
|
-
When using time-expiring entries with `ttl`, by default stale
|
|
374
|
-
items will be removed from the cache when the key is accessed
|
|
375
|
-
with `cache.get()`.
|
|
376
|
-
|
|
377
|
-
Setting `noDeleteOnStaleGet` to `true` will cause stale items to
|
|
378
|
-
remain in the cache, until they are explicitly deleted with
|
|
379
|
-
`cache.delete(key)`, or retrieved with `noDeleteOnStaleGet` set
|
|
380
|
-
to `false`.
|
|
381
|
-
|
|
382
|
-
This may be overridden by passing an options object to
|
|
383
|
-
`cache.get()`.
|
|
384
|
-
|
|
385
|
-
Boolean, default false, only relevant if `ttl` is set.
|
|
386
|
-
|
|
387
|
-
### `updateAgeOnGet`
|
|
388
|
-
|
|
389
|
-
When using time-expiring entries with `ttl`, setting this to
|
|
390
|
-
`true` will make each item's age reset to 0 whenever it is
|
|
391
|
-
retrieved from cache with `get()`, causing it to not expire. (It
|
|
392
|
-
can still fall out of cache based on recency of use, of course.)
|
|
393
|
-
|
|
394
|
-
This may be overridden by passing an options object to
|
|
395
|
-
`cache.get()`.
|
|
396
|
-
|
|
397
|
-
Boolean, default false, only relevant if `ttl` is set.
|
|
398
|
-
|
|
399
|
-
### `updateAgeOnHas`
|
|
400
|
-
|
|
401
|
-
When using time-expiring entries with `ttl`, setting this to
|
|
402
|
-
`true` will make each item's age reset to 0 whenever its presence
|
|
403
|
-
in the cache is checked with `has()`, causing it to not expire.
|
|
404
|
-
(It can still fall out of cache based on recency of use, of
|
|
405
|
-
course.)
|
|
406
|
-
|
|
407
|
-
This may be overridden by passing an options object to
|
|
408
|
-
`cache.has()`.
|
|
47
|
+
## Options
|
|
409
48
|
|
|
410
|
-
|
|
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.
|
|
65
|
+
* `dispose` Function that is called on items when they are dropped
|
|
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.)
|
|
411
87
|
|
|
412
88
|
## API
|
|
413
89
|
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
Create a new LRUCache. All options are documented above, and are
|
|
417
|
-
on the cache as public members.
|
|
418
|
-
|
|
419
|
-
### `cache.max`, `cache.maxSize`, `cache.allowStale`,
|
|
420
|
-
`cache.noDisposeOnSet`, `cache.sizeCalculation`, `cache.dispose`,
|
|
421
|
-
`cache.maxSize`, `cache.ttl`, `cache.updateAgeOnGet`,
|
|
422
|
-
`cache.updateAgeOnHas`
|
|
423
|
-
|
|
424
|
-
All option names are exposed as public members on the cache
|
|
425
|
-
object.
|
|
426
|
-
|
|
427
|
-
These are intended for read access only. Changing them during
|
|
428
|
-
program operation can cause undefined behavior.
|
|
429
|
-
|
|
430
|
-
### `cache.size`
|
|
431
|
-
|
|
432
|
-
The total number of items held in the cache at the current
|
|
433
|
-
moment.
|
|
434
|
-
|
|
435
|
-
### `cache.calculatedSize`
|
|
436
|
-
|
|
437
|
-
The total size of items in cache when using size tracking.
|
|
438
|
-
|
|
439
|
-
### `set(key, value, [{ size, sizeCalculation, ttl, noDisposeOnSet, start }])`
|
|
440
|
-
|
|
441
|
-
Add a value to the cache.
|
|
442
|
-
|
|
443
|
-
Optional options object may contain `ttl` and `sizeCalculation`
|
|
444
|
-
as described above, which default to the settings on the cache
|
|
445
|
-
object.
|
|
446
|
-
|
|
447
|
-
If `start` is provided, then that will set the effective start
|
|
448
|
-
time for the TTL calculation. Note that this must be a previous
|
|
449
|
-
value of `performance.now()` if supported, or a previous value of
|
|
450
|
-
`Date.now()` if not.
|
|
451
|
-
|
|
452
|
-
Options object my also include `size`, which will prevent calling
|
|
453
|
-
the `sizeCalculation` function and just use the specified number
|
|
454
|
-
if it is a positive integer, and `noDisposeOnSet` which will
|
|
455
|
-
prevent calling a `dispose` function in the case of overwrites.
|
|
456
|
-
|
|
457
|
-
If the `size` (or return value of `sizeCalculation`) is greater
|
|
458
|
-
than `maxSize`, then the item will not be added to the cache.
|
|
459
|
-
|
|
460
|
-
Will update the recency of the entry.
|
|
461
|
-
|
|
462
|
-
Returns the cache object.
|
|
463
|
-
|
|
464
|
-
### `get(key, { updateAgeOnGet, allowStale } = {}) => value`
|
|
465
|
-
|
|
466
|
-
Return a value from the cache.
|
|
467
|
-
|
|
468
|
-
Will update the recency of the cache entry found.
|
|
469
|
-
|
|
470
|
-
If the key is not found, `get()` will return `undefined`. This
|
|
471
|
-
can be confusing when setting values specifically to `undefined`,
|
|
472
|
-
as in `cache.set(key, undefined)`. Use `cache.has()` to
|
|
473
|
-
determine whether a key is present in the cache at all.
|
|
90
|
+
* `set(key, value, maxAge)`
|
|
91
|
+
* `get(key) => value`
|
|
474
92
|
|
|
475
|
-
|
|
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.
|
|
476
96
|
|
|
477
|
-
If the
|
|
478
|
-
Promise resolves to the value.
|
|
97
|
+
If the key is not found, `get()` will return `undefined`.
|
|
479
98
|
|
|
480
|
-
|
|
481
|
-
`fetchMethod(key, staleValue, options)` is called, and the value
|
|
482
|
-
returned will be added to the cache once resolved.
|
|
99
|
+
The key and val can be any value.
|
|
483
100
|
|
|
484
|
-
|
|
485
|
-
currently in progress to reload a stale value, then the former
|
|
486
|
-
stale value will be returned.
|
|
101
|
+
* `peek(key)`
|
|
487
102
|
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
set, then the old value will still be returned. This is useful
|
|
491
|
-
in cases where you want to force a reload of a cached value. If
|
|
492
|
-
a background fetch is already in progress, then `forceRefresh`
|
|
493
|
-
has no effect.
|
|
103
|
+
Returns the key value (or `undefined` if not found) without
|
|
104
|
+
updating the "recently used"-ness of the key.
|
|
494
105
|
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
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.)
|
|
498
109
|
|
|
499
|
-
|
|
500
|
-
alias for `Promise.resolve(cache.get(key))`.
|
|
110
|
+
* `del(key)`
|
|
501
111
|
|
|
502
|
-
|
|
503
|
-
been aborted due to deletion, eviction, or being overwritten,
|
|
504
|
-
then it is added to the cache using the options provided.
|
|
112
|
+
Deletes a key out of the cache.
|
|
505
113
|
|
|
506
|
-
|
|
114
|
+
* `reset()`
|
|
507
115
|
|
|
508
|
-
|
|
116
|
+
Clear the cache entirely, throwing away all values.
|
|
509
117
|
|
|
510
|
-
|
|
511
|
-
set either on the cache or in the options object.
|
|
118
|
+
* `has(key)`
|
|
512
119
|
|
|
513
|
-
|
|
120
|
+
Check if a key is in the cache, without updating the recent-ness
|
|
121
|
+
or deleting it for being stale.
|
|
514
122
|
|
|
515
|
-
|
|
516
|
-
use. Age is updated if `updateAgeOnHas` is set to `true` in
|
|
517
|
-
either the options or the constructor.
|
|
123
|
+
* `forEach(function(value,key,cache), [thisp])`
|
|
518
124
|
|
|
519
|
-
|
|
520
|
-
|
|
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.)
|
|
521
128
|
|
|
522
|
-
|
|
129
|
+
* `rforEach(function(value,key,cache), [thisp])`
|
|
523
130
|
|
|
524
|
-
|
|
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.)
|
|
525
134
|
|
|
526
|
-
|
|
135
|
+
* `keys()`
|
|
527
136
|
|
|
528
|
-
|
|
137
|
+
Return an array of the keys in the cache.
|
|
529
138
|
|
|
530
|
-
|
|
139
|
+
* `values()`
|
|
531
140
|
|
|
532
|
-
|
|
141
|
+
Return an array of the values in the cache.
|
|
533
142
|
|
|
534
|
-
|
|
143
|
+
* `length`
|
|
535
144
|
|
|
536
|
-
Return
|
|
537
|
-
|
|
145
|
+
Return total length of objects in cache taking into account
|
|
146
|
+
`length` options function.
|
|
538
147
|
|
|
539
|
-
|
|
148
|
+
* `itemCount`
|
|
540
149
|
|
|
541
|
-
Return
|
|
542
|
-
|
|
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.
|
|
543
153
|
|
|
544
|
-
|
|
154
|
+
* `dump()`
|
|
545
155
|
|
|
546
|
-
Return
|
|
547
|
-
|
|
156
|
+
Return an array of the cache entries ready for serialization and usage
|
|
157
|
+
with 'destinationCache.load(arr)`.
|
|
548
158
|
|
|
549
|
-
|
|
159
|
+
* `load(cacheEntriesArray)`
|
|
550
160
|
|
|
551
|
-
|
|
552
|
-
|
|
161
|
+
Loads another cache entries array, obtained with `sourceCache.dump()`,
|
|
162
|
+
into the cache. The destination cache is reset before loading new entries
|
|
553
163
|
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
Return a generator yielding `[key, value]` pairs, in order from
|
|
557
|
-
most recently used to least recently used.
|
|
558
|
-
|
|
559
|
-
### `rentries()`
|
|
560
|
-
|
|
561
|
-
Return a generator yielding `[key, value]` pairs, in order from
|
|
562
|
-
least recently used to most recently used.
|
|
563
|
-
|
|
564
|
-
### `find(fn, [getOptions])`
|
|
565
|
-
|
|
566
|
-
Find a value for which the supplied `fn` method returns a truthy
|
|
567
|
-
value, similar to `Array.find()`.
|
|
568
|
-
|
|
569
|
-
`fn` is called as `fn(value, key, cache)`.
|
|
570
|
-
|
|
571
|
-
The optional `getOptions` are applied to the resulting `get()` of
|
|
572
|
-
the item found.
|
|
573
|
-
|
|
574
|
-
### `dump()`
|
|
575
|
-
|
|
576
|
-
Return an array of `[key, entry]` objects which can be passed to
|
|
577
|
-
`cache.load()`
|
|
578
|
-
|
|
579
|
-
The `start` fields are calculated relative to a portable
|
|
580
|
-
`Date.now()` timestamp, even if `performance.now()` is available.
|
|
581
|
-
|
|
582
|
-
Stale entries are always included in the `dump`, even if
|
|
583
|
-
`allowStale` is false.
|
|
584
|
-
|
|
585
|
-
Note: this returns an actual array, not a generator, so it can be
|
|
586
|
-
more easily passed around.
|
|
587
|
-
|
|
588
|
-
### `load(entries)`
|
|
589
|
-
|
|
590
|
-
Reset the cache and load in the items in `entries` in the order
|
|
591
|
-
listed. Note that the shape of the resulting cache may be
|
|
592
|
-
different if the same options are not used in both caches.
|
|
593
|
-
|
|
594
|
-
The `start` fields are assumed to be calculated relative to a
|
|
595
|
-
portable `Date.now()` timestamp, even if `performance.now()` is
|
|
596
|
-
available.
|
|
597
|
-
|
|
598
|
-
### `purgeStale()`
|
|
599
|
-
|
|
600
|
-
Delete any stale entries. Returns `true` if anything was
|
|
601
|
-
removed, `false` otherwise.
|
|
602
|
-
|
|
603
|
-
Deprecated alias: `prune`
|
|
604
|
-
|
|
605
|
-
### `getRemainingTTL(key)`
|
|
606
|
-
|
|
607
|
-
Return the number of ms left in the item's TTL. If item is not
|
|
608
|
-
in cache, returns `0`. Returns `Infinity` if item is in cache
|
|
609
|
-
without a defined TTL.
|
|
610
|
-
|
|
611
|
-
### `forEach(fn, [thisp])`
|
|
612
|
-
|
|
613
|
-
Call the `fn` function with each set of `fn(value, key, cache)`
|
|
614
|
-
in the LRU cache, from most recent to least recently used.
|
|
615
|
-
|
|
616
|
-
Does not affect recency of use.
|
|
617
|
-
|
|
618
|
-
If `thisp` is provided, function will be called in the
|
|
619
|
-
`this`-context of the provided object.
|
|
620
|
-
|
|
621
|
-
### `rforEach(fn, [thisp])`
|
|
622
|
-
|
|
623
|
-
Same as `cache.forEach(fn, thisp)`, but in order from least
|
|
624
|
-
recently used to most recently used.
|
|
625
|
-
|
|
626
|
-
### `pop()`
|
|
627
|
-
|
|
628
|
-
Evict the least recently used item, returning its value.
|
|
629
|
-
|
|
630
|
-
Returns `undefined` if cache is empty.
|
|
631
|
-
|
|
632
|
-
### Internal Methods and Properties
|
|
633
|
-
|
|
634
|
-
In order to optimize performance as much as possible, "private"
|
|
635
|
-
members and methods are exposed on the object as normal
|
|
636
|
-
properties, rather than being accessed via Symbols, private
|
|
637
|
-
members, or closure variables.
|
|
638
|
-
|
|
639
|
-
**Do not use or rely on these.** They will change or be removed
|
|
640
|
-
without notice. They will cause undefined behavior if used
|
|
641
|
-
inappropriately. There is no need or reason to ever call them
|
|
642
|
-
directly.
|
|
643
|
-
|
|
644
|
-
This documentation is here so that it is especially clear that
|
|
645
|
-
this not "undocumented" because someone forgot; it _is_
|
|
646
|
-
documented, and the documentation is telling you not to do it.
|
|
647
|
-
|
|
648
|
-
**Do not report bugs that stem from using these properties.**
|
|
649
|
-
They will be ignored.
|
|
650
|
-
|
|
651
|
-
* `initializeTTLTracking()` Set up the cache for tracking TTLs
|
|
652
|
-
* `updateItemAge(index)` Called when an item age is updated, by
|
|
653
|
-
internal ID
|
|
654
|
-
* `setItemTTL(index)` Called when an item ttl is updated, by
|
|
655
|
-
internal ID
|
|
656
|
-
* `isStale(index)` Called to check an item's staleness, by
|
|
657
|
-
internal ID
|
|
658
|
-
* `initializeSizeTracking()` Set up the cache for tracking item
|
|
659
|
-
size. Called automatically when a size is specified.
|
|
660
|
-
* `removeItemSize(index)` Updates the internal size calculation
|
|
661
|
-
when an item is removed or modified, by internal ID
|
|
662
|
-
* `addItemSize(index)` Updates the internal size calculation when
|
|
663
|
-
an item is added or modified, by internal ID
|
|
664
|
-
* `indexes()` An iterator over the non-stale internal IDs, from
|
|
665
|
-
most recently to least recently used.
|
|
666
|
-
* `rindexes()` An iterator over the non-stale internal IDs, from
|
|
667
|
-
least recently to most recently used.
|
|
668
|
-
* `newIndex()` Create a new internal ID, either reusing a deleted
|
|
669
|
-
ID, evicting the least recently used ID, or walking to the end
|
|
670
|
-
of the allotted space.
|
|
671
|
-
* `evict()` Evict the least recently used internal ID, returning
|
|
672
|
-
its ID. Does not do any bounds checking.
|
|
673
|
-
* `connect(p, n)` Connect the `p` and `n` internal IDs in the
|
|
674
|
-
linked list.
|
|
675
|
-
* `moveToTail(index)` Move the specified internal ID to the most
|
|
676
|
-
recently used position.
|
|
677
|
-
* `keyMap` Map of keys to internal IDs
|
|
678
|
-
* `keyList` List of keys by internal ID
|
|
679
|
-
* `valList` List of values by internal ID
|
|
680
|
-
* `sizes` List of calculated sizes by internal ID
|
|
681
|
-
* `ttls` List of TTL values by internal ID
|
|
682
|
-
* `starts` List of start time values by internal ID
|
|
683
|
-
* `next` Array of "next" pointers by internal ID
|
|
684
|
-
* `prev` Array of "previous" pointers by internal ID
|
|
685
|
-
* `head` Internal ID of least recently used item
|
|
686
|
-
* `tail` Internal ID of most recently used item
|
|
687
|
-
* `free` Stack of deleted internal IDs
|
|
688
|
-
|
|
689
|
-
## Storage Bounds Safety
|
|
690
|
-
|
|
691
|
-
This implementation aims to be as flexible as possible, within
|
|
692
|
-
the limits of safe memory consumption and optimal performance.
|
|
693
|
-
|
|
694
|
-
At initial object creation, storage is allocated for `max` items.
|
|
695
|
-
If `max` is set to zero, then some performance is lost, and item
|
|
696
|
-
count is unbounded. Either `maxSize` or `ttl` _must_ be set if
|
|
697
|
-
`max` is not specified.
|
|
698
|
-
|
|
699
|
-
If `maxSize` is set, then this creates a safe limit on the
|
|
700
|
-
maximum storage consumed, but without the performance benefits of
|
|
701
|
-
pre-allocation. When `maxSize` is set, every item _must_ provide
|
|
702
|
-
a size, either via the `sizeCalculation` method provided to the
|
|
703
|
-
constructor, or via a `size` or `sizeCalculation` option provided
|
|
704
|
-
to `cache.set()`. The size of every item _must_ be a positive
|
|
705
|
-
integer.
|
|
706
|
-
|
|
707
|
-
If neither `max` nor `maxSize` are set, then `ttl` tracking must
|
|
708
|
-
be enabled. Note that, even when tracking item `ttl`, items are
|
|
709
|
-
_not_ preemptively deleted when they become stale, unless
|
|
710
|
-
`ttlAutopurge` is enabled. Instead, they are only purged the
|
|
711
|
-
next time the key is requested. Thus, if `ttlAutopurge`, `max`,
|
|
712
|
-
and `maxSize` are all not set, then the cache will potentially
|
|
713
|
-
grow unbounded.
|
|
714
|
-
|
|
715
|
-
In this case, a warning is printed to standard error. Future
|
|
716
|
-
versions may require the use of `ttlAutopurge` if `max` and
|
|
717
|
-
`maxSize` are not specified.
|
|
718
|
-
|
|
719
|
-
If you truly wish to use a cache that is bound _only_ by TTL
|
|
720
|
-
expiration, consider using a `Map` object, and calling
|
|
721
|
-
`setTimeout` to delete entries when they expire. It will perform
|
|
722
|
-
much better than an LRU cache.
|
|
723
|
-
|
|
724
|
-
Here is an implementation you may use, under the same
|
|
725
|
-
[license](./LICENSE) as this package:
|
|
726
|
-
|
|
727
|
-
```js
|
|
728
|
-
// a storage-unbounded ttl cache that is not an lru-cache
|
|
729
|
-
const cache = {
|
|
730
|
-
data: new Map(),
|
|
731
|
-
timers: new Map(),
|
|
732
|
-
set: (k, v, ttl) => {
|
|
733
|
-
if (cache.timers.has(k)) {
|
|
734
|
-
clearTimeout(cache.timers.get(k))
|
|
735
|
-
}
|
|
736
|
-
cache.timers.set(k, setTimeout(() => cache.delete(k), ttl))
|
|
737
|
-
cache.data.set(k, v)
|
|
738
|
-
},
|
|
739
|
-
get: k => cache.data.get(k),
|
|
740
|
-
has: k => cache.data.has(k),
|
|
741
|
-
delete: k => {
|
|
742
|
-
if (cache.timers.has(k)) {
|
|
743
|
-
clearTimeout(cache.timers.get(k))
|
|
744
|
-
}
|
|
745
|
-
cache.timers.delete(k)
|
|
746
|
-
return cache.data.delete(k)
|
|
747
|
-
},
|
|
748
|
-
clear: () => {
|
|
749
|
-
cache.data.clear()
|
|
750
|
-
for (const v of cache.timers.values()) {
|
|
751
|
-
clearTimeout(v)
|
|
752
|
-
}
|
|
753
|
-
cache.timers.clear()
|
|
754
|
-
}
|
|
755
|
-
}
|
|
756
|
-
```
|
|
757
|
-
|
|
758
|
-
If that isn't to your liking, check out
|
|
759
|
-
[@isaacs/ttlcache](http://npm.im/@isaacs/ttlcache).
|
|
760
|
-
|
|
761
|
-
## Performance
|
|
762
|
-
|
|
763
|
-
As of January 2022, version 7 of this library is one of the most
|
|
764
|
-
performant LRU cache implementations in JavaScript.
|
|
765
|
-
|
|
766
|
-
Benchmarks can be extremely difficult to get right. In
|
|
767
|
-
particular, the performance of set/get/delete operations on
|
|
768
|
-
objects will vary _wildly_ depending on the type of key used. V8
|
|
769
|
-
is highly optimized for objects with keys that are short strings,
|
|
770
|
-
especially integer numeric strings. Thus any benchmark which
|
|
771
|
-
tests _solely_ using numbers as keys will tend to find that an
|
|
772
|
-
object-based approach performs the best.
|
|
773
|
-
|
|
774
|
-
Note that coercing _anything_ to strings to use as object keys is
|
|
775
|
-
unsafe, unless you can be 100% certain that no other type of
|
|
776
|
-
value will be used. For example:
|
|
777
|
-
|
|
778
|
-
```js
|
|
779
|
-
const myCache = {}
|
|
780
|
-
const set = (k, v) => myCache[k] = v
|
|
781
|
-
const get = (k) => myCache[k]
|
|
782
|
-
|
|
783
|
-
set({}, 'please hang onto this for me')
|
|
784
|
-
set('[object Object]', 'oopsie')
|
|
785
|
-
```
|
|
164
|
+
* `prune()`
|
|
786
165
|
|
|
787
|
-
|
|
788
|
-
collection of large (especially: deep) object graphs can be
|
|
789
|
-
incredibly costly, with several "tipping points" where it
|
|
790
|
-
increases exponentially. As a result, putting that off until
|
|
791
|
-
later can make it much worse, and less predictable. If a library
|
|
792
|
-
performs well, but only in a scenario where the object graph is
|
|
793
|
-
kept shallow, then that won't help you if you are using large
|
|
794
|
-
objects as keys.
|
|
795
|
-
|
|
796
|
-
In general, when attempting to use a library to improve
|
|
797
|
-
performance (such as a cache like this one), it's best to choose
|
|
798
|
-
an option that will perform well in the sorts of scenarios where
|
|
799
|
-
you'll actually use it.
|
|
800
|
-
|
|
801
|
-
This library is optimized for repeated gets and minimizing
|
|
802
|
-
eviction time, since that is the expected need of a LRU. Set
|
|
803
|
-
operations are somewhat slower on average than a few other
|
|
804
|
-
options, in part because of that optimization. It is assumed
|
|
805
|
-
that you'll be caching some costly operation, ideally as rarely
|
|
806
|
-
as possible, so optimizing set over get would be unwise.
|
|
807
|
-
|
|
808
|
-
If performance matters to you:
|
|
809
|
-
|
|
810
|
-
1. If it's at all possible to use small integer values as keys,
|
|
811
|
-
and you can guarantee that no other types of values will be
|
|
812
|
-
used as keys, then do that, and use a cache such as
|
|
813
|
-
[lru-fast](https://npmjs.com/package/lru-fast), or
|
|
814
|
-
[mnemonist's
|
|
815
|
-
LRUCache](https://yomguithereal.github.io/mnemonist/lru-cache)
|
|
816
|
-
which uses an Object as its data store.
|
|
817
|
-
2. Failing that, if at all possible, use short non-numeric
|
|
818
|
-
strings (ie, less than 256 characters) as your keys, and use
|
|
819
|
-
[mnemonist's
|
|
820
|
-
LRUCache](https://yomguithereal.github.io/mnemonist/lru-cache).
|
|
821
|
-
3. If the types of your keys will be long strings, strings that
|
|
822
|
-
look like floats, `null`, objects, or some mix of types, or if
|
|
823
|
-
you aren't sure, then this library will work well for you.
|
|
824
|
-
4. Do not use a `dispose` function, size tracking, or especially
|
|
825
|
-
ttl behavior, unless absolutely needed. These features are
|
|
826
|
-
convenient, and necessary in some use cases, and every attempt
|
|
827
|
-
has been made to make the performance impact minimal, but it
|
|
828
|
-
isn't nothing.
|
|
829
|
-
|
|
830
|
-
## Breaking Changes in Version 7
|
|
831
|
-
|
|
832
|
-
This library changed to a different algorithm and internal data
|
|
833
|
-
structure in version 7, yielding significantly better
|
|
834
|
-
performance, albeit with some subtle changes as a result.
|
|
835
|
-
|
|
836
|
-
If you were relying on the internals of LRUCache in version 6 or
|
|
837
|
-
before, it probably will not work in version 7 and above.
|
|
838
|
-
|
|
839
|
-
For more info, see the [change log](CHANGELOG.md).
|
|
166
|
+
Manually iterates over the entire cache proactively pruning old entries
|