cache-manager 6.4.2 → 7.0.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/README.md CHANGED
@@ -20,9 +20,18 @@ We moved to using [Keyv](https://keyv.org/) which are more actively maintained a
20
20
 
21
21
  A special thanks to [Tim Phan](https://github.com/timphandev) who took `cache-manager` v5 and ported it to [Keyv](https://keyv.org/) which is the foundation of v6. 🎉 Another special thanks to [Doug Ayers](https://github.com/douglascayers) who wrote `promise-coalesce` which was used in v5 and now embedded in v6.
22
22
 
23
- If you are looking for older documentation you can find it here:
24
- * [v5 Documentation](https://github.com/jaredwray/cacheable/blob/main/packages/cache-manager/READMEv5.md)
25
- * [v4 Documentation](https://github.com/jaredwray/cacheable/blob/main/packages/cache-manager/READMEv4.md)
23
+ # Migration from v6 to v7
24
+
25
+ `v7` has only one breaking change which is changine the return type from `null` to `undefined` when there is no data to return. This is to align with the [Keyv](https://keyv.org) API and to make it more consistent with the rest of the methods. Below is an example of how to migrate from `v6` to `v7`:
26
+
27
+ ```ts
28
+ import { createCache } from 'cache-manager';
29
+
30
+ const cache = createCache();
31
+ const result = await cache.get('key');
32
+ // result will be undefined if the key is not found or expired
33
+ console.log(result); // undefined
34
+ ```
26
35
 
27
36
  # Migration from v5 to v6
28
37
 
@@ -38,25 +47,53 @@ const memoryCache = createCache(memoryStore({
38
47
  }));
39
48
  ```
40
49
 
41
- In `v6` you can use any storage adapter that Keyv supports. Below is an example of how to migrate from `v5` to `v6` using `CacheableMemory` from Cacheable:
50
+ In `v6` you can use any storage adapter that Keyv supports. Below is an example of using the in memory store with `Keyv`:
42
51
 
43
52
  ```ts
44
53
  import { createCache } from 'cache-manager';
45
- import { Keyv } from 'keyv';
54
+
55
+ const cache = createCache();
56
+ ```
57
+
58
+ If you would like to do multiple stores you can do the following:
59
+
60
+ ```ts
61
+ import { createCache } from 'cache-manager';
62
+ import { createKeyv } from 'cacheable';
63
+ import { createKeyv as createKeyvRedis } from '@keyv/redis';
64
+
65
+ const memoryStore = createKeyv();
66
+ const redisStore = createKeyvRedis('redis://user:pass@localhost:6379');
46
67
 
47
68
  const cache = createCache({
48
- stores: [new Keyv()],
69
+ stores: [memoryStore, redisStore],
70
+ });
71
+ ```
72
+
73
+ When doing in memory caching and getting errors on `symbol` or if the object is coming back wrong like on `Uint8Array` you will want to set the `serialization` and `deserialization` options in Keyv to `undefined` as it will try to do json serialization.
74
+
75
+ ```ts
76
+ import { createCache } from "cache-manager";
77
+ import { Keyv } from "keyv";
78
+
79
+ const keyv = new Keyv();
80
+ keyv.serialize = undefined;
81
+ keyv.deserialize = undefined;
82
+
83
+ const memoryCache = createCache({
84
+ stores: [keyv],
49
85
  });
50
86
  ```
87
+ The other option is to set the serialization to something that is not `JSON.stringify`. You can read more about it here: https://keyv.org/docs/keyv/#custom-serializers
51
88
 
52
89
  If you would like a more robust in memory storage adapter you can use `CacheableMemory` from Cacheable. Below is an example of how to migrate from `v5` to `v6` using `CacheableMemory`:
53
90
 
54
91
  ```ts
55
92
  import { createCache } from 'cache-manager';
56
- import { KeyvCacheableMemory } from 'cacheable';
93
+ import { createKeyv } from 'cacheable';
57
94
 
58
95
  const cache = createCache({
59
- stores: [new KeyvCacheableMemory({ ttl: 60000, lruSize: 5000 })],
96
+ stores: [createKeyv({ ttl: 60000, lruSize: 5000 })],
60
97
  });
61
98
  ```
62
99
 
@@ -64,6 +101,10 @@ To learn more about `CacheableMemory` please visit: http://cacheable.org/docs/ca
64
101
 
65
102
  If you are still wanting to use the legacy storage adapters you can use the `KeyvAdapter` to wrap the storage adapter. Below is an example of how to migrate from `v5` to `v6` using `cache-manager-redis-yet` by going to [Using Legacy Storage Adapters](#using-legacy-storage-adapters).
66
103
 
104
+ If you are looking for older documentation you can find it here:
105
+ * [v5 Documentation](https://github.com/jaredwray/cacheable/blob/main/packages/cache-manager/READMEv5.md)
106
+ * [v4 Documentation](https://github.com/jaredwray/cacheable/blob/main/packages/cache-manager/READMEv4.md)
107
+
67
108
 
68
109
  ## Table of Contents
69
110
  * [Installation](#installation)
@@ -296,7 +337,7 @@ await cache.mget(['key-1', 'key-2', 'key-3'])
296
337
  Gets the expiration time of a key in milliseconds. Returns a null if not found or expired.
297
338
 
298
339
  ```ts
299
- await cache.set('key', 'value', 1000); // expires after 5 seconds
340
+ await cache.set('key', 'value', 1000); // expires after 1 second
300
341
 
301
342
  await cache.ttl('key'); // => the expiration time in milliseconds
302
343
 
@@ -533,9 +574,8 @@ import { Keyv } from 'keyv';
533
574
  import { redisStore } from 'cache-manager-redis-yet';
534
575
 
535
576
  const adapter = new KeyvAdapter( await redisStore() );
536
- const cache = createCache({
537
- stores: [new Keyv({ store: adapter })],
538
- });
577
+ const keyv = new Keyv({ store: adapter });
578
+ const cache = createCache({ stores: [keyv]});
539
579
  ```
540
580
 
541
581
  This adapter will allow you to add in any storage adapter. If there are issues it needs to follow `CacheManagerStore` interface.
package/dist/index.cjs CHANGED
@@ -123,7 +123,8 @@ var KeyvAdapter = class {
123
123
  return void 0;
124
124
  }
125
125
  async set(key, value, ttl) {
126
- return this._cache.set(key, value, ttl).then(() => true);
126
+ await this._cache.set(key, value, ttl);
127
+ return true;
127
128
  }
128
129
  async delete(key) {
129
130
  await this._cache.del(key);
@@ -159,16 +160,19 @@ var KeyvAdapter = class {
159
160
  // src/index.ts
160
161
  var createCache = (options) => {
161
162
  const eventEmitter = new import_node_events.default();
162
- const stores = options?.stores?.length ? options.stores : [new import_keyv.Keyv()];
163
+ const keyv = new import_keyv.Keyv();
164
+ keyv.serialize = void 0;
165
+ keyv.deserialize = void 0;
166
+ const stores = options?.stores?.length ? options.stores : [keyv];
163
167
  const nonBlocking = options?.nonBlocking ?? false;
164
168
  const _cacheId = options?.cacheId ?? Math.random().toString(36).slice(2);
165
169
  const get = async (key) => {
166
- let result = null;
170
+ let result;
167
171
  if (nonBlocking) {
168
172
  try {
169
173
  result = await Promise.race(stores.map(async (store) => store.get(key)));
170
174
  if (result === void 0) {
171
- return null;
175
+ return void 0;
172
176
  }
173
177
  } catch (error) {
174
178
  eventEmitter.emit("get", { key, error });
@@ -198,12 +202,12 @@ var createCache = (options) => {
198
202
  return result;
199
203
  };
200
204
  const ttl = async (key) => {
201
- let result = null;
205
+ let result;
202
206
  if (nonBlocking) {
203
207
  try {
204
208
  result = await Promise.race(stores.map(async (store) => store.get(key, { raw: true })));
205
209
  if (result === void 0) {
206
- return null;
210
+ return void 0;
207
211
  }
208
212
  } catch (error) {
209
213
  eventEmitter.emit("ttl", { key, error });
@@ -225,7 +229,7 @@ var createCache = (options) => {
225
229
  if (result?.expires) {
226
230
  return result.expires;
227
231
  }
228
- return null;
232
+ return void 0;
229
233
  };
230
234
  const set = async (stores2, key, value, ttl2) => {
231
235
  try {
@@ -247,7 +251,7 @@ var createCache = (options) => {
247
251
  try {
248
252
  const promises = [];
249
253
  for (const item of list) {
250
- promises.push(stores2.map(async (store) => store.set(item.key, item.value, item.ttl)));
254
+ promises.push(...stores2.map(async (store) => store.set(item.key, item.value, item.ttl)));
251
255
  }
252
256
  if (nonBlocking) {
253
257
  Promise.all(promises);
@@ -281,7 +285,7 @@ var createCache = (options) => {
281
285
  try {
282
286
  const promises = [];
283
287
  for (const key of keys) {
284
- promises.push(stores.map(async (store) => store.delete(key)));
288
+ promises.push(...stores.map(async (store) => store.delete(key)));
285
289
  }
286
290
  if (nonBlocking) {
287
291
  Promise.all(promises);
package/dist/index.d.cts CHANGED
@@ -48,9 +48,9 @@ type WrapOptionsRaw<T> = WrapOptions<T> & {
48
48
  raw: true;
49
49
  };
50
50
  type Cache = {
51
- get: <T>(key: string) => Promise<T | null>;
52
- mget: <T>(keys: string[]) => Promise<Array<T | null>>;
53
- ttl: (key: string) => Promise<number | null>;
51
+ get: <T>(key: string) => Promise<T | undefined>;
52
+ mget: <T>(keys: string[]) => Promise<Array<T | undefined>>;
53
+ ttl: (key: string) => Promise<number | undefined>;
54
54
  set: <T>(key: string, value: T, ttl?: number) => Promise<T>;
55
55
  mset: <T>(list: Array<{
56
56
  key: string;
package/dist/index.d.ts CHANGED
@@ -48,9 +48,9 @@ type WrapOptionsRaw<T> = WrapOptions<T> & {
48
48
  raw: true;
49
49
  };
50
50
  type Cache = {
51
- get: <T>(key: string) => Promise<T | null>;
52
- mget: <T>(keys: string[]) => Promise<Array<T | null>>;
53
- ttl: (key: string) => Promise<number | null>;
51
+ get: <T>(key: string) => Promise<T | undefined>;
52
+ mget: <T>(keys: string[]) => Promise<Array<T | undefined>>;
53
+ ttl: (key: string) => Promise<number | undefined>;
54
54
  set: <T>(key: string, value: T, ttl?: number) => Promise<T>;
55
55
  mset: <T>(list: Array<{
56
56
  key: string;
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/index.ts
2
- import EventEmitter from "node:events";
2
+ import EventEmitter from "events";
3
3
  import { Keyv } from "keyv";
4
4
 
5
5
  // src/coalesce-async.ts
@@ -88,7 +88,8 @@ var KeyvAdapter = class {
88
88
  return void 0;
89
89
  }
90
90
  async set(key, value, ttl) {
91
- return this._cache.set(key, value, ttl).then(() => true);
91
+ await this._cache.set(key, value, ttl);
92
+ return true;
92
93
  }
93
94
  async delete(key) {
94
95
  await this._cache.del(key);
@@ -124,16 +125,19 @@ var KeyvAdapter = class {
124
125
  // src/index.ts
125
126
  var createCache = (options) => {
126
127
  const eventEmitter = new EventEmitter();
127
- const stores = options?.stores?.length ? options.stores : [new Keyv()];
128
+ const keyv = new Keyv();
129
+ keyv.serialize = void 0;
130
+ keyv.deserialize = void 0;
131
+ const stores = options?.stores?.length ? options.stores : [keyv];
128
132
  const nonBlocking = options?.nonBlocking ?? false;
129
133
  const _cacheId = options?.cacheId ?? Math.random().toString(36).slice(2);
130
134
  const get = async (key) => {
131
- let result = null;
135
+ let result;
132
136
  if (nonBlocking) {
133
137
  try {
134
138
  result = await Promise.race(stores.map(async (store) => store.get(key)));
135
139
  if (result === void 0) {
136
- return null;
140
+ return void 0;
137
141
  }
138
142
  } catch (error) {
139
143
  eventEmitter.emit("get", { key, error });
@@ -163,12 +167,12 @@ var createCache = (options) => {
163
167
  return result;
164
168
  };
165
169
  const ttl = async (key) => {
166
- let result = null;
170
+ let result;
167
171
  if (nonBlocking) {
168
172
  try {
169
173
  result = await Promise.race(stores.map(async (store) => store.get(key, { raw: true })));
170
174
  if (result === void 0) {
171
- return null;
175
+ return void 0;
172
176
  }
173
177
  } catch (error) {
174
178
  eventEmitter.emit("ttl", { key, error });
@@ -190,7 +194,7 @@ var createCache = (options) => {
190
194
  if (result?.expires) {
191
195
  return result.expires;
192
196
  }
193
- return null;
197
+ return void 0;
194
198
  };
195
199
  const set = async (stores2, key, value, ttl2) => {
196
200
  try {
@@ -212,7 +216,7 @@ var createCache = (options) => {
212
216
  try {
213
217
  const promises = [];
214
218
  for (const item of list) {
215
- promises.push(stores2.map(async (store) => store.set(item.key, item.value, item.ttl)));
219
+ promises.push(...stores2.map(async (store) => store.set(item.key, item.value, item.ttl)));
216
220
  }
217
221
  if (nonBlocking) {
218
222
  Promise.all(promises);
@@ -246,7 +250,7 @@ var createCache = (options) => {
246
250
  try {
247
251
  const promises = [];
248
252
  for (const key of keys) {
249
- promises.push(stores.map(async (store) => store.delete(key)));
253
+ promises.push(...stores.map(async (store) => store.delete(key)));
250
254
  }
251
255
  if (nonBlocking) {
252
256
  Promise.all(promises);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cache-manager",
3
- "version": "6.4.2",
3
+ "version": "7.0.0",
4
4
  "description": "Cache Manager for Node.js",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -50,20 +50,21 @@
50
50
  ],
51
51
  "license": "MIT",
52
52
  "dependencies": {
53
- "keyv": "^5.3.2"
53
+ "keyv": "^5.3.3"
54
54
  },
55
55
  "devDependencies": {
56
- "@faker-js/faker": "^9.6.0",
57
- "@keyv/redis": "^4.3.2",
58
- "@types/node": "^22.14.0",
59
- "@vitest/coverage-v8": "^3.1.1",
56
+ "@faker-js/faker": "^9.8.0",
57
+ "@keyv/redis": "^4.4.0",
58
+ "@types/node": "^22.15.30",
59
+ "@vitest/coverage-v8": "^3.2.2",
60
+ "@vitest/spy": "^3.2.2",
60
61
  "cache-manager-redis-yet": "^5.1.5",
61
62
  "rimraf": "^6.0.1",
62
- "tsup": "^8.4.0",
63
- "typescript": "^5.8.2",
64
- "vitest": "^3.1.1",
65
- "xo": "^0.60.0",
66
- "cacheable": "^1.8.10"
63
+ "tsup": "^8.5.0",
64
+ "typescript": "^5.8.3",
65
+ "vitest": "^3.2.2",
66
+ "xo": "^1.0.5",
67
+ "cacheable": "^1.9.0"
67
68
  },
68
69
  "xo": {
69
70
  "rules": {