@cacheable/node-cache 1.4.0 → 1.4.2
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/README.md +30 -29
- package/dist/index.cjs +175 -33
- package/dist/index.d.cts +229 -1
- package/dist/index.d.ts +229 -1
- package/dist/index.js +175 -33
- package/package.json +13 -9
package/README.md
CHANGED
|
@@ -6,8 +6,9 @@
|
|
|
6
6
|
|
|
7
7
|
[](https://codecov.io/gh/jaredwray/cacheable)
|
|
8
8
|
[](https://github.com/jaredwray/cacheable/actions/workflows/tests.yml)
|
|
9
|
-
[](https://www.npmjs.com/package/@cacheable/node-cache)
|
|
10
|
+
[](https://www.npmjs.com/package/@cacheable/node-cache)
|
|
11
|
+
[](https://github.com/jaredwray/cacheable/blob/main/LICENSE)
|
|
11
12
|
|
|
12
13
|
`@cacheable/node-cache` is compatible with the [node-cache](https://www.npmjs.com/package/node-cache) package with regular maintenance and additional functionality (async/await and storage adapters). The only thing not implemented is the `enableLegacyCallbacks` option and functions. If you need them we are happy to take a PR to add them.
|
|
13
14
|
|
|
@@ -18,7 +19,7 @@
|
|
|
18
19
|
|
|
19
20
|
Note: `NodeCache` is ready and available for use. `NodeCacheStore` is in progress and will be available soon. Please do not use it until it is released.
|
|
20
21
|
|
|
21
|
-
|
|
22
|
+
# Table of Contents
|
|
22
23
|
* [Getting Started](#getting-started)
|
|
23
24
|
* [Basic Usage](#basic-usage)
|
|
24
25
|
* [Advanced Usage](#advanced-usage)
|
|
@@ -27,13 +28,13 @@ Note: `NodeCache` is ready and available for use. `NodeCacheStore` is in progres
|
|
|
27
28
|
* [How to Contribute](#how-to-contribute)
|
|
28
29
|
* [License and Copyright](#license-and-copyright)
|
|
29
30
|
|
|
30
|
-
|
|
31
|
+
# Getting Started
|
|
31
32
|
|
|
32
33
|
```bash
|
|
33
34
|
npm install @cacheable/node-cache --save
|
|
34
35
|
```
|
|
35
36
|
|
|
36
|
-
|
|
37
|
+
# Basic Usage
|
|
37
38
|
|
|
38
39
|
```javascript
|
|
39
40
|
import {NodeCache} from '@cacheable/node-cache';
|
|
@@ -43,7 +44,7 @@ cache.set('foo', 'bar');
|
|
|
43
44
|
cache.get('foo'); // 'bar'
|
|
44
45
|
```
|
|
45
46
|
|
|
46
|
-
|
|
47
|
+
# Advanced Usage
|
|
47
48
|
|
|
48
49
|
```javascript
|
|
49
50
|
import {NodeStorageCache} from '@cacheable/node-cache';
|
|
@@ -61,7 +62,7 @@ await cache.get('foo'); // 'bar'
|
|
|
61
62
|
cache.getStats(); // {hits: 1, misses: 1, keys: 1, ksize: 2, vsize: 3}
|
|
62
63
|
```
|
|
63
64
|
|
|
64
|
-
|
|
65
|
+
# NodeCacheStore
|
|
65
66
|
|
|
66
67
|
The `NodeCacheStore` is a class that extends the `NodeCache` and adds the ability to use storage adapters. This is based on the `cacheable` engine and allows you to do layer 1 and layer 2 caching. The storage adapters are based on the [Keyv](https://keyv.org) package. This allows you to use any of the storage adapters that are available.
|
|
67
68
|
|
|
@@ -73,7 +74,7 @@ cache.set('foo', 'bar');
|
|
|
73
74
|
cache.get('foo'); // 'bar'
|
|
74
75
|
```
|
|
75
76
|
|
|
76
|
-
|
|
77
|
+
## NodeCacheStoreOptions
|
|
77
78
|
|
|
78
79
|
When initializing the cache you can pass in the options below:
|
|
79
80
|
|
|
@@ -94,7 +95,7 @@ cache.set('foo', 'bar', '1h'); // 1 hour
|
|
|
94
95
|
cache.set('longfoo', 'bar', '1d'); // 1 day
|
|
95
96
|
```
|
|
96
97
|
|
|
97
|
-
|
|
98
|
+
## Node Cache Store API
|
|
98
99
|
|
|
99
100
|
* `set(key: string | number, value: any, ttl?: number): Promise<boolean>` - Set a key value pair with an optional ttl (in milliseconds). Will return true on success. If the ttl is not set it will default to 0 (no ttl)
|
|
100
101
|
* `mset(data: Array<NodeCacheItem>): Promise<boolean>` - Set multiple key value pairs at once
|
|
@@ -112,9 +113,9 @@ cache.set('longfoo', 'bar', '1d'); // 1 day
|
|
|
112
113
|
* `secondary`: `Keyv` - The secondary storage adapter
|
|
113
114
|
* `maxKeys`: `number` - If this is set it will throw and error if you try to set more keys than the max
|
|
114
115
|
|
|
115
|
-
|
|
116
|
+
# API
|
|
116
117
|
|
|
117
|
-
|
|
118
|
+
## `constructor(options?: NodeCacheOptions)`
|
|
118
119
|
|
|
119
120
|
Create a new cache instance. You can pass in options to set the configuration:
|
|
120
121
|
|
|
@@ -143,7 +144,7 @@ cache.on('expired', (key, value) => {
|
|
|
143
144
|
});
|
|
144
145
|
```
|
|
145
146
|
|
|
146
|
-
|
|
147
|
+
## `.set(key: string | number, value: any, ttl?: number): boolean`
|
|
147
148
|
|
|
148
149
|
Set a key value pair with an optional ttl (in seconds). Will return true on success. If the ttl is not set it will default to 0 (no ttl).
|
|
149
150
|
|
|
@@ -151,7 +152,7 @@ Set a key value pair with an optional ttl (in seconds). Will return true on succ
|
|
|
151
152
|
cache.set('foo', 'bar', 10); // true
|
|
152
153
|
```
|
|
153
154
|
|
|
154
|
-
|
|
155
|
+
## `.mset(data: Array<NodeCacheItem>): boolean`
|
|
155
156
|
|
|
156
157
|
Set multiple key value pairs at once. This will take an array of objects with the key, value, and optional ttl.
|
|
157
158
|
|
|
@@ -169,7 +170,7 @@ export type NodeCacheItem = {
|
|
|
169
170
|
};
|
|
170
171
|
```
|
|
171
172
|
|
|
172
|
-
|
|
173
|
+
## `.get(key: string | number): any`
|
|
173
174
|
|
|
174
175
|
Get a value from the cache by key. If the key does not exist it will return `undefined`.
|
|
175
176
|
|
|
@@ -177,7 +178,7 @@ Get a value from the cache by key. If the key does not exist it will return `und
|
|
|
177
178
|
cache.get('foo'); // 'bar'
|
|
178
179
|
```
|
|
179
180
|
|
|
180
|
-
|
|
181
|
+
## `mget(keys: Array<string | number>): Record<string, unknown>`
|
|
181
182
|
|
|
182
183
|
Get multiple values from the cache by keys. This will return an object with the keys and values.
|
|
183
184
|
|
|
@@ -189,7 +190,7 @@ cache.set('my2', obj2);
|
|
|
189
190
|
cache.mget(['my', 'my2']); // { my: { my: 'value', my2: 'value2' }, my2: { special: 'value3', life: 'value4' } }
|
|
190
191
|
```
|
|
191
192
|
|
|
192
|
-
|
|
193
|
+
## `take(key: string | number): any`
|
|
193
194
|
|
|
194
195
|
Get a value from the cache by key and delete it. If the key does not exist it will return `undefined`.
|
|
195
196
|
|
|
@@ -199,7 +200,7 @@ cache.take('foo'); // 'bar'
|
|
|
199
200
|
cache.get('foo'); // undefined
|
|
200
201
|
```
|
|
201
202
|
|
|
202
|
-
|
|
203
|
+
## `del(key: string | number | Array<string | number>): number`
|
|
203
204
|
|
|
204
205
|
Delete a key from the cache. Will return the number of deleted entries and never fail. You can also pass in an array of keys to delete multiple keys. All examples assume that you have initialized the cache like `const cache = new NodeCache();`.
|
|
205
206
|
|
|
@@ -213,7 +214,7 @@ passing in an array of keys:
|
|
|
213
214
|
cache.del(['foo', 'bar']); // true
|
|
214
215
|
```
|
|
215
216
|
|
|
216
|
-
|
|
217
|
+
## `.mdel(keys: Array<string | number>): number`
|
|
217
218
|
|
|
218
219
|
Delete multiple keys from the cache. Will return the number of deleted entries and never fail.
|
|
219
220
|
|
|
@@ -221,7 +222,7 @@ Delete multiple keys from the cache. Will return the number of deleted entries a
|
|
|
221
222
|
cache.mdel(['foo', 'bar']); // true
|
|
222
223
|
```
|
|
223
224
|
|
|
224
|
-
|
|
225
|
+
## `.ttl(key: string | number, ttl?: number): boolean`
|
|
225
226
|
|
|
226
227
|
Redefine the ttl of a key. Returns true if the key has been found and changed. Otherwise returns false. If the ttl-argument isn't passed the default-TTL will be used.
|
|
227
228
|
|
|
@@ -229,7 +230,7 @@ Redefine the ttl of a key. Returns true if the key has been found and changed. O
|
|
|
229
230
|
cache.ttl('foo', 10); // true
|
|
230
231
|
```
|
|
231
232
|
|
|
232
|
-
|
|
233
|
+
## `getTtl(key: string | number): number | undefined`
|
|
233
234
|
|
|
234
235
|
Get the ttl expiration from `Date.now()` of a key. If the key does not exist it will return `undefined`.
|
|
235
236
|
|
|
@@ -237,7 +238,7 @@ Get the ttl expiration from `Date.now()` of a key. If the key does not exist it
|
|
|
237
238
|
cache.getTtl('foo'); // 1725993344859
|
|
238
239
|
```
|
|
239
240
|
|
|
240
|
-
|
|
241
|
+
## `has(key: string | number): boolean`
|
|
241
242
|
|
|
242
243
|
Check if a key exists in the cache.
|
|
243
244
|
|
|
@@ -246,7 +247,7 @@ cache.set('foo', 'bar');
|
|
|
246
247
|
cache.has('foo'); // true
|
|
247
248
|
```
|
|
248
249
|
|
|
249
|
-
|
|
250
|
+
## `keys(): Array<string>`
|
|
250
251
|
|
|
251
252
|
Get all keys from the cache.
|
|
252
253
|
|
|
@@ -254,7 +255,7 @@ Get all keys from the cache.
|
|
|
254
255
|
cache.keys(); // ['foo', 'bar']
|
|
255
256
|
```
|
|
256
257
|
|
|
257
|
-
|
|
258
|
+
## `getStats(): NodeCacheStats`
|
|
258
259
|
|
|
259
260
|
Get the stats of the cache.
|
|
260
261
|
|
|
@@ -262,7 +263,7 @@ Get the stats of the cache.
|
|
|
262
263
|
cache.getStats(); // {hits: 1, misses: 1, keys: 1, ksize: 2, vsize: 3}
|
|
263
264
|
```
|
|
264
265
|
|
|
265
|
-
|
|
266
|
+
## `flushAll(): void`
|
|
266
267
|
|
|
267
268
|
Flush the cache. Will remove all keys and reset the stats.
|
|
268
269
|
|
|
@@ -272,7 +273,7 @@ cache.keys(); // []
|
|
|
272
273
|
cache.getStats(); // {hits: 0, misses: 0, keys: 0, ksize: 0, vsize: 0}
|
|
273
274
|
```
|
|
274
275
|
|
|
275
|
-
|
|
276
|
+
## `flushStats(): void`
|
|
276
277
|
|
|
277
278
|
Flush the stats. Will reset the stats but keep the keys.
|
|
278
279
|
|
|
@@ -283,7 +284,7 @@ cache.getStats(); // {hits: 0, misses: 0, keys: 0, ksize: 0, vsize: 0}
|
|
|
283
284
|
cache.keys(); // ['foo']
|
|
284
285
|
```
|
|
285
286
|
|
|
286
|
-
|
|
287
|
+
## `close(): void`
|
|
287
288
|
|
|
288
289
|
this will stop the interval that is running for the `checkperiod` and `deleteOnExpire` options.
|
|
289
290
|
|
|
@@ -291,7 +292,7 @@ this will stop the interval that is running for the `checkperiod` and `deleteOnE
|
|
|
291
292
|
cache.close();
|
|
292
293
|
```
|
|
293
294
|
|
|
294
|
-
|
|
295
|
+
## `on(event: string, callback: Function): void`
|
|
295
296
|
|
|
296
297
|
Listen to events. Here are the events that you can listen to:
|
|
297
298
|
* `set` - when a key is set and it will pass in the `key` and `value`.
|
|
@@ -306,9 +307,9 @@ cache.on('set', (key, value) => {
|
|
|
306
307
|
});
|
|
307
308
|
```
|
|
308
309
|
|
|
309
|
-
|
|
310
|
+
# How to Contribute
|
|
310
311
|
|
|
311
312
|
You can contribute by forking the repo and submitting a pull request. Please make sure to add tests and update the documentation. To learn more about how to contribute go to our main README [https://github.com/jaredwray/cacheable](https://github.com/jaredwray/cacheable). This will talk about how to `Open a Pull Request`, `Ask a Question`, or `Post an Issue`.
|
|
312
313
|
|
|
313
|
-
|
|
314
|
+
# License and Copyright
|
|
314
315
|
[MIT © Jared Wray](./LICENSE)
|
package/dist/index.cjs
CHANGED
|
@@ -60,36 +60,87 @@ var NodeCacheStore = class {
|
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
|
+
/**
|
|
64
|
+
* Cacheable instance.
|
|
65
|
+
* @returns {Cacheable}
|
|
66
|
+
* @readonly
|
|
67
|
+
*/
|
|
63
68
|
get cache() {
|
|
64
69
|
return this._cache;
|
|
65
70
|
}
|
|
71
|
+
/**
|
|
72
|
+
* Time to live in milliseconds.
|
|
73
|
+
* @returns {number | string | undefined}
|
|
74
|
+
* @readonly
|
|
75
|
+
*/
|
|
66
76
|
get ttl() {
|
|
67
77
|
return this._cache.ttl;
|
|
68
78
|
}
|
|
79
|
+
/**
|
|
80
|
+
* Time to live in milliseconds.
|
|
81
|
+
* @param {number | string | undefined} ttl
|
|
82
|
+
*/
|
|
69
83
|
set ttl(ttl) {
|
|
70
84
|
this._cache.ttl = ttl;
|
|
71
85
|
}
|
|
86
|
+
/**
|
|
87
|
+
* Primary cache store.
|
|
88
|
+
* @returns {Keyv}
|
|
89
|
+
* @readonly
|
|
90
|
+
*/
|
|
72
91
|
get primary() {
|
|
73
92
|
return this._cache.primary;
|
|
74
93
|
}
|
|
94
|
+
/**
|
|
95
|
+
* Primary cache store.
|
|
96
|
+
* @param {Keyv} primary
|
|
97
|
+
*/
|
|
75
98
|
set primary(primary) {
|
|
76
99
|
this._cache.primary = primary;
|
|
77
100
|
}
|
|
101
|
+
/**
|
|
102
|
+
* Secondary cache store. Learn more about the secondary cache store in the
|
|
103
|
+
* [cacheable](https://github.com/jaredwray/cacheable/tree/main/packages/cacheable#storage-tiering-and-caching) documentation.
|
|
104
|
+
* @returns {Keyv | undefined}
|
|
105
|
+
*/
|
|
78
106
|
get secondary() {
|
|
79
107
|
return this._cache.secondary;
|
|
80
108
|
}
|
|
109
|
+
/**
|
|
110
|
+
* Secondary cache store. Learn more about the secondary cache store in the
|
|
111
|
+
* [cacheable](https://github.com/jaredwray/cacheable/tree/main/packages/cacheable#storage-tiering-and-caching) documentation.
|
|
112
|
+
* @param {Keyv | undefined} secondary
|
|
113
|
+
*/
|
|
81
114
|
set secondary(secondary) {
|
|
82
115
|
this._cache.secondary = secondary;
|
|
83
116
|
}
|
|
117
|
+
/**
|
|
118
|
+
* Maximum number of keys to store in the cache. if this is set to a value greater than 0,
|
|
119
|
+
* the cache will keep track of the number of keys and will not store more than the specified number of keys.
|
|
120
|
+
* @returns {number}
|
|
121
|
+
* @readonly
|
|
122
|
+
*/
|
|
84
123
|
get maxKeys() {
|
|
85
124
|
return this._maxKeys;
|
|
86
125
|
}
|
|
126
|
+
/**
|
|
127
|
+
* Maximum number of keys to store in the cache. if this is set to a value greater than 0,
|
|
128
|
+
* the cache will keep track of the number of keys and will not store more than the specified number of keys.
|
|
129
|
+
* @param {number} maxKeys
|
|
130
|
+
*/
|
|
87
131
|
set maxKeys(maxKeys) {
|
|
88
132
|
this._maxKeys = maxKeys;
|
|
89
133
|
if (this._maxKeys > 0) {
|
|
90
134
|
this._cache.stats.enabled = true;
|
|
91
135
|
}
|
|
92
136
|
}
|
|
137
|
+
/**
|
|
138
|
+
* Set a key/value pair in the cache.
|
|
139
|
+
* @param {string | number} key
|
|
140
|
+
* @param {any} value
|
|
141
|
+
* @param {number} [ttl]
|
|
142
|
+
* @returns {boolean}
|
|
143
|
+
*/
|
|
93
144
|
async set(key, value, ttl) {
|
|
94
145
|
if (this._maxKeys > 0) {
|
|
95
146
|
if (this._cache.stats.count >= this._maxKeys) {
|
|
@@ -99,6 +150,11 @@ var NodeCacheStore = class {
|
|
|
99
150
|
await this._cache.set(key.toString(), value, ttl);
|
|
100
151
|
return true;
|
|
101
152
|
}
|
|
153
|
+
/**
|
|
154
|
+
* Set multiple key/value pairs in the cache.
|
|
155
|
+
* @param {NodeCacheItem[]} list
|
|
156
|
+
* @returns {void}
|
|
157
|
+
*/
|
|
102
158
|
async mset(list) {
|
|
103
159
|
const items = new Array();
|
|
104
160
|
for (const item of list) {
|
|
@@ -106,9 +162,19 @@ var NodeCacheStore = class {
|
|
|
106
162
|
}
|
|
107
163
|
await this._cache.setMany(items);
|
|
108
164
|
}
|
|
165
|
+
/**
|
|
166
|
+
* Get a value from the cache.
|
|
167
|
+
* @param {string | number} key
|
|
168
|
+
* @returns {any | undefined}
|
|
169
|
+
*/
|
|
109
170
|
async get(key) {
|
|
110
171
|
return this._cache.get(key.toString());
|
|
111
172
|
}
|
|
173
|
+
/**
|
|
174
|
+
* Get multiple values from the cache.
|
|
175
|
+
* @param {Array<string | number>} keys
|
|
176
|
+
* @returns {Record<string, any | undefined>}
|
|
177
|
+
*/
|
|
112
178
|
async mget(keys) {
|
|
113
179
|
const result = {};
|
|
114
180
|
for (const key of keys) {
|
|
@@ -116,15 +182,34 @@ var NodeCacheStore = class {
|
|
|
116
182
|
}
|
|
117
183
|
return result;
|
|
118
184
|
}
|
|
185
|
+
/**
|
|
186
|
+
* Delete a key from the cache.
|
|
187
|
+
* @param {string | number} key
|
|
188
|
+
* @returns {boolean}
|
|
189
|
+
*/
|
|
119
190
|
async del(key) {
|
|
120
191
|
return this._cache.delete(key.toString());
|
|
121
192
|
}
|
|
193
|
+
/**
|
|
194
|
+
* Delete multiple keys from the cache.
|
|
195
|
+
* @param {Array<string | number>} keys
|
|
196
|
+
* @returns {boolean}
|
|
197
|
+
*/
|
|
122
198
|
async mdel(keys) {
|
|
123
199
|
return this._cache.deleteMany(keys.map((key) => key.toString()));
|
|
124
200
|
}
|
|
201
|
+
/**
|
|
202
|
+
* Clear the cache.
|
|
203
|
+
* @returns {void}
|
|
204
|
+
*/
|
|
125
205
|
async clear() {
|
|
126
206
|
return this._cache.clear();
|
|
127
207
|
}
|
|
208
|
+
/**
|
|
209
|
+
* Check if a key exists in the cache.
|
|
210
|
+
* @param {string | number} key
|
|
211
|
+
* @returns {boolean}
|
|
212
|
+
*/
|
|
128
213
|
async setTtl(key, ttl) {
|
|
129
214
|
const item = await this._cache.get(key.toString());
|
|
130
215
|
if (item) {
|
|
@@ -133,9 +218,18 @@ var NodeCacheStore = class {
|
|
|
133
218
|
}
|
|
134
219
|
return false;
|
|
135
220
|
}
|
|
221
|
+
/**
|
|
222
|
+
* Check if a key exists in the cache. If it does exist it will get the value and delete the item from the cache.
|
|
223
|
+
* @param {string | number} key
|
|
224
|
+
* @returns {any | undefined}
|
|
225
|
+
*/
|
|
136
226
|
async take(key) {
|
|
137
227
|
return this._cache.take(key.toString());
|
|
138
228
|
}
|
|
229
|
+
/**
|
|
230
|
+
* Disconnect from the cache.
|
|
231
|
+
* @returns {void}
|
|
232
|
+
*/
|
|
139
233
|
async disconnect() {
|
|
140
234
|
await this._cache.disconnect();
|
|
141
235
|
}
|
|
@@ -169,7 +263,13 @@ var NodeCache = class extends import_eventemitter3.default {
|
|
|
169
263
|
}
|
|
170
264
|
this.startInterval();
|
|
171
265
|
}
|
|
172
|
-
|
|
266
|
+
/**
|
|
267
|
+
* Sets a key value pair. It is possible to define a ttl (in seconds). Returns true on success.
|
|
268
|
+
* @param {string | number} key - it will convert the key to a string
|
|
269
|
+
* @param {any} value
|
|
270
|
+
* @param {number} [ttl] - this is in seconds and undefined will use the default ttl
|
|
271
|
+
* @returns {boolean}
|
|
272
|
+
*/
|
|
173
273
|
set(key, value, ttl) {
|
|
174
274
|
if (typeof key !== "string" && typeof key !== "number") {
|
|
175
275
|
throw this.createError("The key argument has to be of type `string` or `number`. Found: `__key`" /* EKEYTYPE */, key);
|
|
@@ -196,7 +296,11 @@ var NodeCache = class extends import_eventemitter3.default {
|
|
|
196
296
|
this._stats.setCount(this.store.size);
|
|
197
297
|
return true;
|
|
198
298
|
}
|
|
199
|
-
|
|
299
|
+
/**
|
|
300
|
+
* Sets multiple key val pairs. It is possible to define a ttl (seconds). Returns true on success.
|
|
301
|
+
* @param {NodeCacheItem[]} data an array of key value pairs with optional ttl
|
|
302
|
+
* @returns {boolean}
|
|
303
|
+
*/
|
|
200
304
|
mset(data) {
|
|
201
305
|
if (!Array.isArray(data)) {
|
|
202
306
|
throw this.createError("The keys argument has to be an array." /* EKEYSTYPE */);
|
|
@@ -206,7 +310,11 @@ var NodeCache = class extends import_eventemitter3.default {
|
|
|
206
310
|
}
|
|
207
311
|
return true;
|
|
208
312
|
}
|
|
209
|
-
|
|
313
|
+
/**
|
|
314
|
+
* Gets a saved value from the cache. Returns a undefined if not found or expired. If the value was found it returns the value.
|
|
315
|
+
* @param {string | number} key if the key is a number it will convert it to a string
|
|
316
|
+
* @returns {T} the value or undefined
|
|
317
|
+
*/
|
|
210
318
|
get(key) {
|
|
211
319
|
const result = this.store.get(this.formatKey(key));
|
|
212
320
|
if (result) {
|
|
@@ -234,10 +342,12 @@ var NodeCache = class extends import_eventemitter3.default {
|
|
|
234
342
|
this._stats.incrementMisses();
|
|
235
343
|
return void 0;
|
|
236
344
|
}
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
345
|
+
/**
|
|
346
|
+
* Gets multiple saved values from the cache. Returns an empty object {} if not found or expired.
|
|
347
|
+
* If the value was found it returns an object with the key value pair.
|
|
348
|
+
* @param {Array<string | number} keys an array of keys
|
|
349
|
+
* @returns {Record<string, unknown>} an object with the key as a property and the value as the value
|
|
350
|
+
*/
|
|
241
351
|
mget(keys) {
|
|
242
352
|
const result = {};
|
|
243
353
|
for (const key of keys) {
|
|
@@ -248,11 +358,12 @@ var NodeCache = class extends import_eventemitter3.default {
|
|
|
248
358
|
}
|
|
249
359
|
return result;
|
|
250
360
|
}
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
361
|
+
/**
|
|
362
|
+
* Get the cached value and remove the key from the cache. Equivalent to calling get(key) + del(key).
|
|
363
|
+
* Useful for implementing single use mechanism such as OTP, where once a value is read it will become obsolete.
|
|
364
|
+
* @param {string | number} key
|
|
365
|
+
* @returns {T | undefined} the value or undefined
|
|
366
|
+
*/
|
|
256
367
|
take(key) {
|
|
257
368
|
const result = this.get(key);
|
|
258
369
|
if (result) {
|
|
@@ -264,7 +375,11 @@ var NodeCache = class extends import_eventemitter3.default {
|
|
|
264
375
|
}
|
|
265
376
|
return void 0;
|
|
266
377
|
}
|
|
267
|
-
|
|
378
|
+
/**
|
|
379
|
+
* Delete a key. Returns the number of deleted entries. A delete will never fail.
|
|
380
|
+
* @param {string | number | Array<string | number>} key if the key is a number it will convert it to a string. if an array is passed it will delete all keys in the array.
|
|
381
|
+
* @returns {number} if it was successful it will return the count that was deleted
|
|
382
|
+
*/
|
|
268
383
|
del(key) {
|
|
269
384
|
if (Array.isArray(key)) {
|
|
270
385
|
return this.mdel(key);
|
|
@@ -281,7 +396,11 @@ var NodeCache = class extends import_eventemitter3.default {
|
|
|
281
396
|
}
|
|
282
397
|
return 0;
|
|
283
398
|
}
|
|
284
|
-
|
|
399
|
+
/**
|
|
400
|
+
* Delete all keys in Array that exist. Returns the number of deleted entries.
|
|
401
|
+
* @param {Array<string | number>} keys an array of keys
|
|
402
|
+
* @returns {number} the count of deleted keys
|
|
403
|
+
*/
|
|
285
404
|
mdel(keys) {
|
|
286
405
|
let result = 0;
|
|
287
406
|
for (const key of keys) {
|
|
@@ -289,8 +408,13 @@ var NodeCache = class extends import_eventemitter3.default {
|
|
|
289
408
|
}
|
|
290
409
|
return result;
|
|
291
410
|
}
|
|
292
|
-
|
|
293
|
-
|
|
411
|
+
/**
|
|
412
|
+
* Redefine the ttl of a key. Returns true if the key has been found and changed.
|
|
413
|
+
* Otherwise returns false. If the ttl-argument isn't passed the default-TTL will be used.
|
|
414
|
+
* @param {string | number} key if the key is a number it will convert it to a string
|
|
415
|
+
* @param {number} [ttl] the ttl in seconds
|
|
416
|
+
* @returns {boolean} true if the key has been found and changed. Otherwise returns false.
|
|
417
|
+
*/
|
|
294
418
|
ttl(key, ttl) {
|
|
295
419
|
const result = this.store.get(this.formatKey(key));
|
|
296
420
|
if (result) {
|
|
@@ -301,13 +425,12 @@ var NodeCache = class extends import_eventemitter3.default {
|
|
|
301
425
|
}
|
|
302
426
|
return false;
|
|
303
427
|
}
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
*/
|
|
428
|
+
/**
|
|
429
|
+
* Receive the ttl of a key.
|
|
430
|
+
* @param {string | number} key if the key is a number it will convert it to a string
|
|
431
|
+
* @returns {number | undefined} 0 if this key has no ttl, undefined if this key is not in the cache,
|
|
432
|
+
* a timestamp in ms representing the time at which this key will expire
|
|
433
|
+
*/
|
|
311
434
|
getTtl(key) {
|
|
312
435
|
const result = this.store.get(this.formatKey(key));
|
|
313
436
|
if (result) {
|
|
@@ -318,10 +441,10 @@ var NodeCache = class extends import_eventemitter3.default {
|
|
|
318
441
|
}
|
|
319
442
|
return void 0;
|
|
320
443
|
}
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
444
|
+
/**
|
|
445
|
+
* Returns an array of all existing keys. [ "all", "my", "keys", "foo", "bar" ]
|
|
446
|
+
* @returns {string[]} an array of all keys
|
|
447
|
+
*/
|
|
325
448
|
keys() {
|
|
326
449
|
const result = [];
|
|
327
450
|
for (const key of this.store.keys()) {
|
|
@@ -329,11 +452,18 @@ var NodeCache = class extends import_eventemitter3.default {
|
|
|
329
452
|
}
|
|
330
453
|
return result;
|
|
331
454
|
}
|
|
332
|
-
|
|
455
|
+
/**
|
|
456
|
+
* Returns boolean indicating if the key is cached.
|
|
457
|
+
* @param {string | number} key if the key is a number it will convert it to a string
|
|
458
|
+
* @returns {boolean} true if the key is cached
|
|
459
|
+
*/
|
|
333
460
|
has(key) {
|
|
334
461
|
return this.store.has(this.formatKey(key));
|
|
335
462
|
}
|
|
336
|
-
|
|
463
|
+
/**
|
|
464
|
+
* Gets the stats of the cache
|
|
465
|
+
* @returns {NodeCacheStats} the stats of the cache
|
|
466
|
+
*/
|
|
337
467
|
getStats() {
|
|
338
468
|
const stats = {
|
|
339
469
|
keys: this._stats.count,
|
|
@@ -344,22 +474,34 @@ var NodeCache = class extends import_eventemitter3.default {
|
|
|
344
474
|
};
|
|
345
475
|
return stats;
|
|
346
476
|
}
|
|
347
|
-
|
|
477
|
+
/**
|
|
478
|
+
* Flush the whole data.
|
|
479
|
+
* @returns {void}
|
|
480
|
+
*/
|
|
348
481
|
flushAll() {
|
|
349
482
|
this.store.clear();
|
|
350
483
|
this.flushStats();
|
|
351
484
|
this.emit("flush");
|
|
352
485
|
}
|
|
353
|
-
|
|
486
|
+
/**
|
|
487
|
+
* Flush the stats.
|
|
488
|
+
* @returns {void}
|
|
489
|
+
*/
|
|
354
490
|
flushStats() {
|
|
355
491
|
this._stats = new import_cacheable2.CacheableStats({ enabled: true });
|
|
356
492
|
this.emit("flush_stats");
|
|
357
493
|
}
|
|
358
|
-
|
|
494
|
+
/**
|
|
495
|
+
* Close the cache. This will clear the interval timeout which is set on check period option.
|
|
496
|
+
* @returns {void}
|
|
497
|
+
*/
|
|
359
498
|
close() {
|
|
360
499
|
this.stopInterval();
|
|
361
500
|
}
|
|
362
|
-
|
|
501
|
+
/**
|
|
502
|
+
* Get the interval id
|
|
503
|
+
* @returns {number | NodeJS.Timeout} the interval id
|
|
504
|
+
*/
|
|
363
505
|
getIntervalId() {
|
|
364
506
|
return this.intervalId;
|
|
365
507
|
}
|