@cacheable/node-cache 1.5.6 → 1.5.7

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 CHANGED
@@ -13,18 +13,18 @@
13
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.
14
14
 
15
15
  * Fully Compatible with `node-cache` using `{NodeCache}`
16
+ * Faster than the original `node-cache` package 🚀
16
17
  * Async/Await functionality with `{NodeCacheStore}`
17
18
  * Storage Adapters via [Keyv](https://keyv.org) with `{NodeCacheStore}`
18
19
  * Maintained and Updated Regularly! 🎉
19
20
 
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.
21
-
22
21
  # Table of Contents
23
22
  * [Getting Started](#getting-started)
24
23
  * [Basic Usage](#basic-usage)
25
- * [Advanced Usage](#advanced-usage)
24
+ * [NodeCache Performance](#nodecache-performance)
25
+ * [NodeCache API](#nodecache-api)
26
26
  * [NodeCacheStore](#nodecachestore)
27
- * [API](#api)
27
+ * [NodeCacheStore API](#nodecachestore-api)
28
28
  * [How to Contribute](#how-to-contribute)
29
29
  * [License and Copyright](#license-and-copyright)
30
30
 
@@ -50,7 +50,7 @@ cache.del('foo'); // true
50
50
  cache.set('bar', 'baz', '35m'); // 35 minutes using shorthand
51
51
  ```
52
52
 
53
- # NodeCache Not Default Export
53
+ The `NodeCache` is not the default export, so you need to import it like this:
54
54
 
55
55
  ```javascript
56
56
  import {NodeCache} from '@cacheable/node-cache';
@@ -60,77 +60,16 @@ cache.set('foo', 'bar');
60
60
  cache.get('foo'); // 'bar'
61
61
  ```
62
62
 
63
- # Advanced Usage
64
-
65
- ```javascript
66
- import {NodeStorageCache} from '@cacheable/node-cache';
67
- import {Keyv} from 'keyv';
68
- import {KeyvRedis} from '@keyv/redis';
69
-
70
- const storage = new Keyv({store: new KeyvRedis('redis://user:pass@localhost:6379')});
71
- const cache = new NodeStorageCache(storage);
72
-
73
- // with storage you have the same functionality as the NodeCache but will be using async/await
74
- await cache.set('foo', 'bar');
75
- await cache.get('foo'); // 'bar'
76
-
77
- // if you call getStats() this will now only be for the single instance of the adapter as it is in memory
78
- cache.getStats(); // {hits: 1, misses: 1, keys: 1, ksize: 2, vsize: 3}
79
- ```
80
-
81
- # NodeCacheStore
82
-
83
- 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.
84
-
85
- ```javascript
86
- import {NodeCacheStore} from '@cacheable/node-cache';
63
+ # NodeCache Performance
87
64
 
88
- const cache = new NodeCacheStore();
89
- cache.set('foo', 'bar');
90
- cache.get('foo'); // 'bar'
91
- ```
65
+ The performance is comparable if not faster to the original `node-cache` package, but with additional features and improvements.
92
66
 
93
- ## NodeCacheStoreOptions
67
+ | name | summary | ops/sec | time/op | margin | samples |
68
+ |-----------------------------------|:---------:|----------:|----------:|:--------:|----------:|
69
+ | Cacheable NodeCache - set / get | 🥇 | 117K | 9µs | ±1.01% | 111K |
70
+ | Node Cache - set / get | -4.6% | 112K | 9µs | ±1.31% | 106K |
94
71
 
95
- When initializing the cache you can pass in the options below:
96
-
97
- ```javascript
98
- export type NodeCacheStoreOptions = {
99
- ttl?: number; // The standard ttl as number in milliseconds for every generated cache element. 0 = unlimited
100
- primary?: Keyv; // The primary storage adapter
101
- secondary?: Keyv; // The secondary storage adapter
102
- maxKeys?: number; // Default is 0 (unlimited). If this is set it will throw and error if you try to set more keys than the max.
103
- stats?: boolean; // Default is true, if this is set to false it will not track stats
104
- };
105
- ```
106
-
107
- Note: the `ttl` is now in milliseconds and not seconds like `stdTTL` in `NodeCache`. You can learn more about using shorthand also in the [cacheable documentation](https://github.com/jaredwray/cacheable/blob/main/packages/cacheable/README.md#shorthand-for-time-to-live-ttl). as it is fulling supported. Here is an example:
108
-
109
- ```javascript
110
- const cache = new NodeCacheStore({ttl: 60000 }); // 1 minute as it defaults to milliseconds
111
- cache.set('foo', 'bar', '1h'); // 1 hour
112
- cache.set('longfoo', 'bar', '1d'); // 1 day
113
- ```
114
-
115
- ## Node Cache Store API
116
-
117
- * `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)
118
- * `mset(data: Array<NodeCacheItem>): Promise<boolean>` - Set multiple key value pairs at once
119
- * `get<T>(key: string | number): Promise<T>` - Get a value from the cache by key
120
- * `mget(keys: Array<string | number>): Promise<Record<string, unknown>>` - Get multiple values from the cache by keys
121
- * `take<T>(key: string | number): Promise<T>` - Get a value from the cache by key and delete it
122
- * `del(key: string | number): Promise<boolean>` - Delete a key
123
- * `mdel(keys: Array<string | number>): Promise<boolean>` - Delete multiple keys
124
- * `clear(): Promise<void>` - Clear the cache
125
- * `setTtl(key: string | number, ttl: number): Promise<boolean>` - Set the ttl of a key
126
- * `disconnect(): Promise<void>` - Disconnect the storage adapters
127
- * `stats`: `NodeCacheStats` - Get the stats of the cache
128
- * `ttl`: `number` | `string` - The standard ttl as number in seconds for every generated cache element. `< 0` or `undefined` = unlimited
129
- * `primary`: `Keyv` - The primary storage adapter
130
- * `secondary`: `Keyv` - The secondary storage adapter
131
- * `maxKeys`: `number` - If this is set it will throw and error if you try to set more keys than the max
132
-
133
- # API
72
+ # NodeCache API
134
73
 
135
74
  ## `constructor(options?: NodeCacheOptions)`
136
75
 
@@ -161,7 +100,7 @@ cache.on('expired', (key, value) => {
161
100
  });
162
101
  ```
163
102
 
164
- ## `.set(key: string | number, value: any, ttl?: number): boolean`
103
+ ## `set(key: string | number, value: any, ttl?: number): boolean`
165
104
 
166
105
  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).
167
106
 
@@ -169,7 +108,7 @@ Set a key value pair with an optional ttl (in seconds). Will return true on succ
169
108
  cache.set('foo', 'bar', 10); // true
170
109
  ```
171
110
 
172
- ## `.mset(data: Array<NodeCacheItem>): boolean`
111
+ ## `mset(data: Array<NodeCacheItem>): boolean`
173
112
 
174
113
  Set multiple key value pairs at once. This will take an array of objects with the key, value, and optional ttl.
175
114
 
@@ -187,7 +126,7 @@ export type NodeCacheItem = {
187
126
  };
188
127
  ```
189
128
 
190
- ## `.get(key: string | number): any`
129
+ ## `get<T>(key: string | number): T | undefined`
191
130
 
192
131
  Get a value from the cache by key. If the key does not exist it will return `undefined`.
193
132
 
@@ -195,7 +134,7 @@ Get a value from the cache by key. If the key does not exist it will return `und
195
134
  cache.get('foo'); // 'bar'
196
135
  ```
197
136
 
198
- ## `mget(keys: Array<string | number>): Record<string, unknown>`
137
+ ## `mget<T>(keys: Array<string | number>): Record<string, T | undefined>`
199
138
 
200
139
  Get multiple values from the cache by keys. This will return an object with the keys and values.
201
140
 
@@ -207,7 +146,7 @@ cache.set('my2', obj2);
207
146
  cache.mget(['my', 'my2']); // { my: { my: 'value', my2: 'value2' }, my2: { special: 'value3', life: 'value4' } }
208
147
  ```
209
148
 
210
- ## `take(key: string | number): any`
149
+ ## `take<T>(key: string | number): T | undefined`
211
150
 
212
151
  Get a value from the cache by key and delete it. If the key does not exist it will return `undefined`.
213
152
 
@@ -231,7 +170,7 @@ passing in an array of keys:
231
170
  cache.del(['foo', 'bar']); // true
232
171
  ```
233
172
 
234
- ## `.mdel(keys: Array<string | number>): number`
173
+ ## `mdel(keys: Array<string | number>): number`
235
174
 
236
175
  Delete multiple keys from the cache. Will return the number of deleted entries and never fail.
237
176
 
@@ -239,7 +178,7 @@ Delete multiple keys from the cache. Will return the number of deleted entries a
239
178
  cache.mdel(['foo', 'bar']); // true
240
179
  ```
241
180
 
242
- ## `.ttl(key: string | number, ttl?: number): boolean`
181
+ ## `ttl(key: string | number, ttl?: number): boolean`
243
182
 
244
183
  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.
245
184
 
@@ -264,12 +203,12 @@ cache.set('foo', 'bar');
264
203
  cache.has('foo'); // true
265
204
  ```
266
205
 
267
- ## `keys(): Array<string>`
206
+ ## `keys(): string[]`
268
207
 
269
208
  Get all keys from the cache.
270
209
 
271
210
  ```javascript
272
- cache.keys(); // ['foo', 'bar']
211
+ await cache.keys(); // ['foo', 'bar']
273
212
  ```
274
213
 
275
214
  ## `getStats(): NodeCacheStats`
@@ -286,7 +225,7 @@ Flush the cache. Will remove all keys and reset the stats.
286
225
 
287
226
  ```javascript
288
227
  cache.flushAll();
289
- cache.keys(); // []
228
+ await cache.keys(); // []
290
229
  cache.getStats(); // {hits: 0, misses: 0, keys: 0, ksize: 0, vsize: 0}
291
230
  ```
292
231
 
@@ -295,18 +234,10 @@ cache.getStats(); // {hits: 0, misses: 0, keys: 0, ksize: 0, vsize: 0}
295
234
  Flush the stats. Will reset the stats but keep the keys.
296
235
 
297
236
  ```javascript
298
- cache.set('foo', 'bar');
237
+ await cache.set('foo', 'bar');
299
238
  cache.flushStats();
300
239
  cache.getStats(); // {hits: 0, misses: 0, keys: 0, ksize: 0, vsize: 0}
301
- cache.keys(); // ['foo']
302
- ```
303
-
304
- ## `close(): void`
305
-
306
- this will stop the interval that is running for the `checkperiod` and `deleteOnExpire` options.
307
-
308
- ```javascript
309
- cache.close();
240
+ await cache.keys(); // ['foo']
310
241
  ```
311
242
 
312
243
  ## `on(event: string, callback: Function): void`
@@ -324,9 +255,78 @@ cache.on('set', (key, value) => {
324
255
  });
325
256
  ```
326
257
 
258
+ # NodeCacheStore
259
+
260
+ `NodeCacheStore` has a similar API to `NodeCache` but it is using `async / await` as it uses the `Keyv` storage adapters under the hood. This means that you can use all the storage adapters that are available in `Keyv` and it will work seamlessly with the `NodeCacheStore`. To learn more about the `Keyv` storage adapters you can check out the [Keyv documentation](https://keyv.org).
261
+
262
+ ```javascript
263
+ import {NodeCacheStore} from '@cacheable/node-cache';
264
+
265
+ const cache = new NodeCacheStore();
266
+ await cache.set('foo', 'bar');
267
+ await cache.get('foo'); // 'bar'
268
+ ```
269
+
270
+ Here is an example of how to use the `NodeCacheStore` with a primary and secondary storage adapter:
271
+
272
+ ```javascript
273
+ import {NodeStorageCache} from '@cacheable/node-cache';
274
+ import {Keyv} from 'keyv';
275
+ import {KeyvRedis} from '@keyv/redis';
276
+
277
+ const primary = new Keyv(); // In-memory storage as primary
278
+ const secondary = new Keyv({store: new KeyvRedis('redis://user:pass@localhost:6379')});
279
+ const cache = new NodeStorageCache({primary, secondary});
280
+
281
+ // with storage you have the same functionality as the NodeCache but will be using async/await
282
+ await cache.set('foo', 'bar');
283
+ await cache.get('foo'); // 'bar'
284
+
285
+ // if you call getStats() this will now only be for the single instance of the adapter as it is in memory
286
+ cache.getStats(); // {hits: 1, misses: 1, keys: 1, ksize: 2, vsize: 3}
287
+ ```
288
+
289
+ When initializing the cache you can pass in the options below:
290
+
291
+ ```javascript
292
+ export type NodeCacheStoreOptions = {
293
+ ttl?: number; // The standard ttl as number in milliseconds for every generated cache element. 0 = unlimited
294
+ primary?: Keyv; // The primary storage adapter
295
+ secondary?: Keyv; // The secondary storage adapter
296
+ maxKeys?: number; // Default is 0 (unlimited). If this is set it will throw and error if you try to set more keys than the max.
297
+ stats?: boolean; // Default is true, if this is set to false it will not track stats
298
+ };
299
+ ```
300
+
301
+ Note: the `ttl` is now in milliseconds and not seconds like `stdTTL` in `NodeCache`. You can learn more about using shorthand also in the [cacheable documentation](https://github.com/jaredwray/cacheable/blob/main/packages/cacheable/README.md#shorthand-for-time-to-live-ttl) as it is fully supported. Here is an example:
302
+
303
+ ```javascript
304
+ const cache = new NodeCacheStore({ttl: 60000 }); // 1 minute as it defaults to milliseconds
305
+ await cache.set('foo', 'bar', '1h'); // 1 hour
306
+ await cache.set('longfoo', 'bar', '1d'); // 1 day
307
+ ```
308
+
309
+ ## NodeCacheStore API
310
+
311
+ * `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)
312
+ * `mset(data: Array<NodeCacheItem>): Promise<boolean>` - Set multiple key value pairs at once
313
+ * `get<T>(key: string | number): Promise<T>` - Get a value from the cache by key
314
+ * `mget(keys: Array<string | number>): Promise<Record<string, unknown>>` - Get multiple values from the cache by keys
315
+ * `take<T>(key: string | number): Promise<T>` - Get a value from the cache by key and delete it
316
+ * `del(key: string | number): Promise<boolean>` - Delete a key
317
+ * `mdel(keys: Array<string | number>): Promise<boolean>` - Delete multiple keys
318
+ * `clear(): Promise<void>` - Clear the cache
319
+ * `setTtl(key: string | number, ttl: number): Promise<boolean>` - Set the ttl of a key
320
+ * `disconnect(): Promise<void>` - Disconnect the storage adapters
321
+ * `stats`: `NodeCacheStats` - Get the stats of the cache
322
+ * `ttl`: `number` | `string` - The standard ttl as number in seconds for every generated cache element. `< 0` or `undefined` = unlimited
323
+ * `primary`: `Keyv` - The primary storage adapter
324
+ * `secondary`: `Keyv` - The secondary storage adapter
325
+ * `maxKeys`: `number` - If this is set it will throw and error if you try to set more keys than the max
326
+
327
327
  # How to Contribute
328
328
 
329
329
  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`.
330
330
 
331
331
  # License and Copyright
332
- [MIT © Jared Wray](./LICENSE)
332
+ [MIT © Jared Wray](./LICENSE)
package/dist/index.cjs CHANGED
@@ -215,7 +215,7 @@ var NodeCacheStore = class extends import_hookified.Hookified {
215
215
  /**
216
216
  * Check if a key exists in the cache. If it does exist it will get the value and delete the item from the cache.
217
217
  * @param {string | number} key
218
- * @returns {any | undefined}
218
+ * @returns {T | undefined}
219
219
  */
220
220
  async take(key) {
221
221
  return this._cache.take(key.toString());
package/dist/index.d.cts CHANGED
@@ -134,7 +134,7 @@ declare class NodeCacheStore extends Hookified {
134
134
  /**
135
135
  * Check if a key exists in the cache. If it does exist it will get the value and delete the item from the cache.
136
136
  * @param {string | number} key
137
- * @returns {any | undefined}
137
+ * @returns {T | undefined}
138
138
  */
139
139
  take<T>(key: string | number): Promise<T | undefined>;
140
140
  /**
package/dist/index.d.ts CHANGED
@@ -134,7 +134,7 @@ declare class NodeCacheStore extends Hookified {
134
134
  /**
135
135
  * Check if a key exists in the cache. If it does exist it will get the value and delete the item from the cache.
136
136
  * @param {string | number} key
137
- * @returns {any | undefined}
137
+ * @returns {T | undefined}
138
138
  */
139
139
  take<T>(key: string | number): Promise<T | undefined>;
140
140
  /**
package/dist/index.js CHANGED
@@ -188,7 +188,7 @@ var NodeCacheStore = class extends Hookified {
188
188
  /**
189
189
  * Check if a key exists in the cache. If it does exist it will get the value and delete the item from the cache.
190
190
  * @param {string | number} key
191
- * @returns {any | undefined}
191
+ * @returns {T | undefined}
192
192
  */
193
193
  async take(key) {
194
194
  return this._cache.take(key.toString());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cacheable/node-cache",
3
- "version": "1.5.6",
3
+ "version": "1.5.7",
4
4
  "description": "Simple and Maintained fast NodeJS internal caching",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -31,6 +31,7 @@
31
31
  "cacheable-node"
32
32
  ],
33
33
  "devDependencies": {
34
+ "@types/eslint": "^9.6.1",
34
35
  "@types/node": "^22.15.30",
35
36
  "@vitest/coverage-v8": "^3.2.2",
36
37
  "rimraf": "^6.0.1",