You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Remove maxKeys limit feature from NodeCacheStore (#1605)
* feat!: remove maxKeys from NodeCacheStore
BREAKING CHANGE: Remove maxKeys option from NodeCacheStore as it does not
make sense for a store backed by external services (Redis, MongoDB, etc.)
where the backend manages its own capacity. The maxKeys option remains
available on the in-memory NodeCache class.
Bumps @cacheable/node-cache to v3.0.0.
https://claude.ai/code/session_01SY98RRSBdLQBanZEsGmqv4
* Update README.md
* removing stats
* Update README.md
---------
Co-authored-by: Claude <noreply@anthropic.com>
Copy file name to clipboardExpand all lines: packages/node-cache/README.md
+96-50Lines changed: 96 additions & 50 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,11 +22,12 @@
22
22
# Table of Contents
23
23
*[Getting Started](#getting-started)
24
24
*[Basic Usage](#basic-usage)
25
-
*[Breaking Changes from v1 to v2](#breaking-changes-from-v1-to-v2)
26
25
*[NodeCache Performance](#nodecache-performance)
27
26
*[NodeCache API](#nodecache-api)
28
27
*[NodeCacheStore](#nodecachestore)
29
28
*[NodeCacheStore API](#nodecachestore-api)
29
+
*[Migrating to v2](#migrating-to-v2)
30
+
*[Migrating to v3](#migrating-to-v3)
30
31
*[How to Contribute](#how-to-contribute)
31
32
*[License and Copyright](#license-and-copyright)
32
33
@@ -47,7 +48,7 @@ cache.get('foo'); // 'bar'
47
48
48
49
cache.set('foo', 'bar', 10); // 10 seconds
49
50
50
-
cache.del('foo'); //true
51
+
cache.del('foo'); //1
51
52
52
53
cache.set('bar', 'baz', '35m'); // 35 minutes using shorthand
53
54
```
@@ -72,34 +73,6 @@ cache.set('foo', 'bar');
72
73
cache.get('foo'); // 'bar'
73
74
```
74
75
75
-
# Breaking Changes from v1 to v2
76
-
77
-
The main `NodeCache` class API has not changed and remains fully compatible. The primary internal change is that it now uses Keyv as the underlying store.
78
-
79
-
## NodeCacheStore Changes
80
-
81
-
### Removed `cache` Property
82
-
-**V1**: `nodeCache.cache` returned a `Cacheable` instance
83
-
-**V2**: Use `nodeCache.store` which returns a `Keyv` instance
84
-
85
-
### Removed Storage Tiering (primary/secondary)
86
-
-**V1**: Supported `primary` and `secondary` store options for multi-tier caching
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();`.
212
185
213
186
```javascript
214
-
cache.del('foo'); //true
187
+
cache.del('foo'); //1
215
188
```
216
189
217
190
passing in an array of keys:
218
191
219
192
```javascript
220
-
cache.del(['foo', 'bar']); //true
193
+
cache.del(['foo', 'bar']); //2
221
194
```
222
195
223
196
## `mdel(keys: Array<string | number>): number`
224
197
225
198
Delete multiple keys from the cache. Will return the number of deleted entries and never fail.
## `ttl(key: string | number, ttl?: number | string): boolean`
232
205
233
206
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.
234
207
@@ -258,7 +231,7 @@ cache.has('foo'); // true
258
231
Get all keys from the cache.
259
232
260
233
```javascript
261
-
awaitcache.keys(); // ['foo', 'bar']
234
+
cache.keys(); // ['foo', 'bar']
262
235
```
263
236
264
237
## `getStats(): NodeCacheStats`
@@ -275,7 +248,7 @@ Flush the cache. Will remove all keys and reset the stats.
Close the cache. This will stop the interval timeout which is set on the `checkperiod` option.
284
+
285
+
```javascript
286
+
cache.close();
287
+
```
288
+
289
+
## `store: Map<string, NodeCacheItem<T>>`
290
+
291
+
The internal store is a public readonly `Map` that holds all cached items. Each item includes the key, value, and TTL expiration timestamp.
292
+
293
+
## `getIntervalId(): number | NodeJS.Timeout`
294
+
295
+
Get the interval ID for the expiration checker.
296
+
297
+
## `startInterval(): void`
298
+
299
+
Start the interval for checking expired keys based on the `checkperiod` option.
300
+
301
+
## `stopInterval(): void`
302
+
303
+
Stop the interval for checking expired keys.
304
+
308
305
# NodeCacheStore
309
306
310
307
`NodeCacheStore` has a similar API to `NodeCache` but it is using `async / await` as it uses [Keyv](https://keyv.org) under the hood. This means that you can use any storage adapter that is 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).
@@ -338,8 +335,6 @@ When initializing the cache you can pass in the options below:
338
335
exporttypeNodeCacheStoreOptions= {
339
336
ttl?: number | string; // The standard ttl as number in milliseconds for every generated cache element. 0 = unlimited. Supports shorthand like '1h' for 1 hour.
340
337
store?: Keyv; // The storage adapter (defaults to in-memory Keyv)
341
-
maxKeys?: number; // Default is 0 (unlimited). If this is set it will return false when trying to set more keys than the max.
342
-
stats?: boolean; // Default is true, if this is set to false it will not track stats internally
*`set(key: string | number, value: any, ttl?: number | string): Promise<boolean>` - Set a key value pair with an optional ttl (in milliseconds or shorthand string). Will return true on success, false if maxKeys limit is reached. If the ttl is not set it will default to the instance ttl or no expiration.
357
-
*`mset(data: Array<NodeCacheItem>): Promise<void>` - Set multiple key value pairs at once
351
+
*`set(key: string | number, value: any, ttl?: number | string): Promise<boolean>` - Set a key value pair with an optional ttl (in milliseconds or shorthand string). Will return true on success. If the ttl is not set it will default to the instance ttl or no expiration.
352
+
*`mset(data: Array<PartialNodeCacheItem>): Promise<void>` - Set multiple key value pairs at once
358
353
*`get<T>(key: string | number): Promise<T | undefined>` - Get a value from the cache by key
359
354
*`mget<T>(keys: Array<string | number>): Promise<Record<string, T | undefined>>` - Get multiple values from the cache by keys
360
355
*`take<T>(key: string | number): Promise<T | undefined>` - Get a value from the cache by key and delete it
361
356
*`del(key: string | number): Promise<boolean>` - Delete a key
*`setTtl(key: string | number, ttl?: number): Promise<boolean>` - Set the ttl of an existing key
359
+
*`setTtl(key: string | number, ttl?: number | string): Promise<boolean>` - Set the ttl of an existing key
365
360
*`disconnect(): Promise<void>` - Disconnect the storage adapter
366
361
*`ttl`: `number | string | undefined` - The standard ttl for every generated cache element. `undefined` = unlimited
367
362
*`store`: `Keyv` - The storage adapter (read-only)
368
-
*`maxKeys`: `number` - If this is set it will return false when trying to set more keys than the max
363
+
364
+
365
+
# Migrating to v2
366
+
367
+
The main `NodeCache` class API has not changed and remains fully compatible. The primary internal change is that it now uses Keyv as the underlying store.
368
+
369
+
## NodeCacheStore Changes
370
+
371
+
### Removed `cache` Property
372
+
-**V1**: `nodeCache.cache` returned a `Cacheable` instance
373
+
-**V2**: Use `nodeCache.store` which returns a `Keyv` instance
374
+
375
+
### Removed Storage Tiering (primary/secondary)
376
+
-**V1**: Supported `primary` and `secondary` store options for multi-tier caching
If you need storage tiering functionality, use the `cacheable` package instead which supports primary and secondary stores.
389
+
390
+
### Internal Dependency Change
391
+
- V2 uses `@cacheable/utils` instead of the `cacheable` package for a lighter footprint
392
+
393
+
# Migrating to v3
394
+
395
+
## Removed `maxKeys` from NodeCacheStore
396
+
397
+
The `maxKeys` option has been removed from `NodeCacheStore`. It does not make sense for a store backed by external services (Redis, MongoDB, etc.) where the backend manages its own capacity.
398
+
399
+
The `maxKeys` option remains available on the in-memory `NodeCache` class.
400
+
401
+
**Migration:**
402
+
```javascript
403
+
// V2 - maxKeys was accepted but not meaningful for external stores
404
+
constcache=newNodeCacheStore({ maxKeys:100 });
405
+
406
+
// V3 - remove maxKeys from NodeCacheStore options
407
+
constcache=newNodeCacheStore();
408
+
```
409
+
410
+
If you need key limits with an external store, configure the limit at the storage layer instead.
411
+
412
+
## Removed `stats` from NodeCacheStore
413
+
414
+
The `stats` option and internal stats tracking have been removed from `NodeCacheStore`. The stats were collected internally but never exposed via a public API, making them effectively unused.
0 commit comments