Skip to content

Commit bf8c696

Browse files
jaredwrayclaude
andauthored
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>
1 parent effcbe3 commit bf8c696

4 files changed

Lines changed: 102 additions & 148 deletions

File tree

packages/node-cache/README.md

Lines changed: 96 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@
2222
# Table of Contents
2323
* [Getting Started](#getting-started)
2424
* [Basic Usage](#basic-usage)
25-
* [Breaking Changes from v1 to v2](#breaking-changes-from-v1-to-v2)
2625
* [NodeCache Performance](#nodecache-performance)
2726
* [NodeCache API](#nodecache-api)
2827
* [NodeCacheStore](#nodecachestore)
2928
* [NodeCacheStore API](#nodecachestore-api)
29+
* [Migrating to v2](#migrating-to-v2)
30+
* [Migrating to v3](#migrating-to-v3)
3031
* [How to Contribute](#how-to-contribute)
3132
* [License and Copyright](#license-and-copyright)
3233

@@ -47,7 +48,7 @@ cache.get('foo'); // 'bar'
4748

4849
cache.set('foo', 'bar', 10); // 10 seconds
4950

50-
cache.del('foo'); // true
51+
cache.del('foo'); // 1
5152

5253
cache.set('bar', 'baz', '35m'); // 35 minutes using shorthand
5354
```
@@ -72,34 +73,6 @@ cache.set('foo', 'bar');
7273
cache.get('foo'); // 'bar'
7374
```
7475

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
87-
- **V2**: Uses single `store` option only
88-
89-
**Migration:**
90-
```javascript
91-
// V1
92-
const cache = new NodeCacheStore({ primary: keyv1, secondary: keyv2 });
93-
94-
// V2 - use single store
95-
const cache = new NodeCacheStore({ store: keyv });
96-
```
97-
98-
If you need storage tiering functionality, use the `cacheable` package instead which supports primary and secondary stores.
99-
100-
### Internal Dependency Change
101-
- V2 uses `@cacheable/utils` instead of the `cacheable` package for a lighter footprint
102-
10376
# NodeCache Performance
10477

10578
The performance is comparable if not faster to the original `node-cache` package, but with additional features and improvements.
@@ -117,7 +90,7 @@ Create a new cache instance. You can pass in options to set the configuration:
11790

11891
```javascript
11992
export type NodeCacheOptions = {
120-
stdTTL?: number;
93+
stdTTL?: number | string;
12194
checkperiod?: number;
12295
useClones?: boolean;
12396
deleteOnExpire?: boolean;
@@ -150,27 +123,27 @@ cache.on('expired', (key, value) => {
150123
});
151124
```
152125

153-
## `set(key: string | number, value: any, ttl?: number): boolean`
126+
## `set(key: string | number, value: any, ttl?: number | string): boolean`
154127

155128
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).
156129

157130
```javascript
158131
cache.set('foo', 'bar', 10); // true
159132
```
160133

161-
## `mset(data: Array<NodeCacheItem>): boolean`
134+
## `mset(data: Array<PartialNodeCacheItem>): boolean`
162135

163136
Set multiple key value pairs at once. This will take an array of objects with the key, value, and optional ttl.
164137

165138
```javascript
166139
cache.mset([{key: 'foo', value: 'bar', ttl: 10}, {key: 'bar', value: 'baz'}]); // true
167140
```
168141

169-
the `NodeCacheItem` is defined as:
142+
the `PartialNodeCacheItem` is defined as:
170143

171144
```javascript
172-
export type NodeCacheItem = {
173-
key: string;
145+
export type PartialNodeCacheItem = {
146+
key: string | number;
174147
value: any;
175148
ttl?: number;
176149
};
@@ -211,24 +184,24 @@ cache.get('foo'); // undefined
211184
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();`.
212185

213186
```javascript
214-
cache.del('foo'); // true
187+
cache.del('foo'); // 1
215188
```
216189

217190
passing in an array of keys:
218191

219192
```javascript
220-
cache.del(['foo', 'bar']); // true
193+
cache.del(['foo', 'bar']); // 2
221194
```
222195

223196
## `mdel(keys: Array<string | number>): number`
224197

225198
Delete multiple keys from the cache. Will return the number of deleted entries and never fail.
226199

227200
```javascript
228-
cache.mdel(['foo', 'bar']); // true
201+
cache.mdel(['foo', 'bar']); // 2
229202
```
230203

231-
## `ttl(key: string | number, ttl?: number): boolean`
204+
## `ttl(key: string | number, ttl?: number | string): boolean`
232205

233206
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.
234207

@@ -258,7 +231,7 @@ cache.has('foo'); // true
258231
Get all keys from the cache.
259232

260233
```javascript
261-
await cache.keys(); // ['foo', 'bar']
234+
cache.keys(); // ['foo', 'bar']
262235
```
263236

264237
## `getStats(): NodeCacheStats`
@@ -275,7 +248,7 @@ Flush the cache. Will remove all keys and reset the stats.
275248

276249
```javascript
277250
cache.flushAll();
278-
await cache.keys(); // []
251+
cache.keys(); // []
279252
cache.getStats(); // {hits: 0, misses: 0, keys: 0, ksize: 0, vsize: 0}
280253
```
281254

@@ -284,10 +257,10 @@ cache.getStats(); // {hits: 0, misses: 0, keys: 0, ksize: 0, vsize: 0}
284257
Flush the stats. Will reset the stats but keep the keys.
285258

286259
```javascript
287-
await cache.set('foo', 'bar');
260+
cache.set('foo', 'bar');
288261
cache.flushStats();
289262
cache.getStats(); // {hits: 0, misses: 0, keys: 0, ksize: 0, vsize: 0}
290-
await cache.keys(); // ['foo']
263+
cache.keys(); // ['foo']
291264
```
292265

293266
## `on(event: string, callback: Function): void`
@@ -305,6 +278,30 @@ cache.on('set', (key, value) => {
305278
});
306279
```
307280

281+
## `close(): void`
282+
283+
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+
308305
# NodeCacheStore
309306

310307
`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:
338335
export type NodeCacheStoreOptions = {
339336
ttl?: number | string; // The standard ttl as number in milliseconds for every generated cache element. 0 = unlimited. Supports shorthand like '1h' for 1 hour.
340337
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
343338
};
344339
```
345340

@@ -353,19 +348,70 @@ await cache.set('longfoo', 'bar', '1d'); // 1 day
353348

354349
## NodeCacheStore API
355350

356-
* `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
358353
* `get<T>(key: string | number): Promise<T | undefined>` - Get a value from the cache by key
359354
* `mget<T>(keys: Array<string | number>): Promise<Record<string, T | undefined>>` - Get multiple values from the cache by keys
360355
* `take<T>(key: string | number): Promise<T | undefined>` - Get a value from the cache by key and delete it
361356
* `del(key: string | number): Promise<boolean>` - Delete a key
362357
* `mdel(keys: Array<string | number>): Promise<boolean>` - Delete multiple keys
363358
* `clear(): Promise<void>` - Clear the cache
364-
* `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
365360
* `disconnect(): Promise<void>` - Disconnect the storage adapter
366361
* `ttl`: `number | string | undefined` - The standard ttl for every generated cache element. `undefined` = unlimited
367362
* `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
377+
- **V2**: Uses single `store` option only
378+
379+
**Migration:**
380+
```javascript
381+
// V1
382+
const cache = new NodeCacheStore({ primary: keyv1, secondary: keyv2 });
383+
384+
// V2 - use single store
385+
const cache = new NodeCacheStore({ store: keyv });
386+
```
387+
388+
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+
const cache = new NodeCacheStore({ maxKeys: 100 });
405+
406+
// V3 - remove maxKeys from NodeCacheStore options
407+
const cache = new NodeCacheStore();
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.
369415

370416
# How to Contribute
371417

packages/node-cache/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@cacheable/node-cache",
3-
"version": "2.0.2",
3+
"version": "3.0.0",
44
"description": "Simple and Maintained fast NodeJS internal caching",
55
"type": "module",
66
"main": "./dist/index.js",

0 commit comments

Comments
 (0)