cache-manager 7.0.0 → 7.1.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
@@ -22,7 +22,7 @@ A special thanks to [Tim Phan](https://github.com/timphandev) who took `cache-ma
22
22
 
23
23
  # Migration from v6 to v7
24
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`:
25
+ `v7` has only one breaking change which is changing 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
26
 
27
27
  ```ts
28
28
  import { createCache } from 'cache-manager';
@@ -483,7 +483,7 @@ Returns the list of Keyv instances. This can be used to get the list of stores a
483
483
  const cache = createCache({cacheId: 'my-cache-id'});
484
484
  cache.cacheId(); // => 'my-cache-id'
485
485
  ```
486
- See unit tests in [`test/cache-id.test.ts`](./test/get.test.ts) for more information.
486
+ See unit tests in [`test/cache-id.test.ts`](./test/cache-id.test.ts) for more information.
487
487
 
488
488
  # Events
489
489
  ## set
package/dist/index.cjs CHANGED
@@ -194,11 +194,35 @@ var createCache = (options) => {
194
194
  return result;
195
195
  };
196
196
  const mget = async (keys) => {
197
- const result = [];
198
- for (const key of keys) {
199
- const data = await get(key);
200
- result.push(data);
197
+ let result = keys.map(() => void 0);
198
+ if (nonBlocking) {
199
+ try {
200
+ result = await Promise.race(stores.map(async (store) => store.getMany(keys)));
201
+ } catch (error) {
202
+ eventEmitter.emit("mget", { keys, error });
203
+ }
204
+ } else {
205
+ for (const store of stores) {
206
+ try {
207
+ const missingValues = result.map((value, index) => value === void 0 ? { originalIndex: index, key: keys[index] } : void 0).filter((v) => v !== void 0);
208
+ if (missingValues.length === 0) {
209
+ break;
210
+ }
211
+ const missingKeys = missingValues.map((v) => v.key);
212
+ const cacheValue = await store.getMany(missingKeys);
213
+ for (const [index, value] of cacheValue.entries()) {
214
+ if (value === void 0) {
215
+ continue;
216
+ }
217
+ const { originalIndex } = missingValues[index];
218
+ result[originalIndex] = value;
219
+ }
220
+ } catch (error) {
221
+ eventEmitter.emit("mget", { keys, error });
222
+ }
223
+ }
201
224
  }
225
+ eventEmitter.emit("mget", { keys, values: result });
202
226
  return result;
203
227
  };
204
228
  const ttl = async (key) => {
@@ -246,13 +270,10 @@ var createCache = (options) => {
246
270
  return Promise.reject(error);
247
271
  }
248
272
  };
249
- const mset = async (stores2, list) => {
250
- const items = list.map(({ key, value, ttl: ttl2 }) => ({ key, value, ttl: ttl2 }));
273
+ const mset = async (stores2, rawList) => {
274
+ const list = rawList.map(({ key, value, ttl: ttl2 }) => ({ key, value, ttl: ttl2 ?? options?.ttl }));
251
275
  try {
252
- const promises = [];
253
- for (const item of list) {
254
- promises.push(...stores2.map(async (store) => store.set(item.key, item.value, item.ttl)));
255
- }
276
+ const promises = stores2.map(async (store) => store.setMany(list));
256
277
  if (nonBlocking) {
257
278
  Promise.all(promises);
258
279
  eventEmitter.emit("mset", { list });
package/dist/index.d.cts CHANGED
@@ -74,11 +74,29 @@ type Cache = {
74
74
  wrap<T>(key: string, fnc: () => T | Promise<T>, options: WrapOptionsRaw<T>): Promise<StoredDataRaw<T>>;
75
75
  };
76
76
  type Events = {
77
+ get: <T>(data: {
78
+ key: string;
79
+ value?: T;
80
+ error?: unknown;
81
+ }) => void;
82
+ mget: <T>(data: {
83
+ keys: string[];
84
+ value?: T[];
85
+ error?: unknown;
86
+ }) => void;
77
87
  set: <T>(data: {
78
88
  key: string;
79
89
  value: T;
80
90
  error?: unknown;
81
91
  }) => void;
92
+ mset: <T>(data: {
93
+ list: Array<{
94
+ key: string;
95
+ value: T;
96
+ ttl?: number;
97
+ }>;
98
+ error?: unknown;
99
+ }) => void;
82
100
  del: (data: {
83
101
  key: string;
84
102
  error?: unknown;
package/dist/index.d.ts CHANGED
@@ -74,11 +74,29 @@ type Cache = {
74
74
  wrap<T>(key: string, fnc: () => T | Promise<T>, options: WrapOptionsRaw<T>): Promise<StoredDataRaw<T>>;
75
75
  };
76
76
  type Events = {
77
+ get: <T>(data: {
78
+ key: string;
79
+ value?: T;
80
+ error?: unknown;
81
+ }) => void;
82
+ mget: <T>(data: {
83
+ keys: string[];
84
+ value?: T[];
85
+ error?: unknown;
86
+ }) => void;
77
87
  set: <T>(data: {
78
88
  key: string;
79
89
  value: T;
80
90
  error?: unknown;
81
91
  }) => void;
92
+ mset: <T>(data: {
93
+ list: Array<{
94
+ key: string;
95
+ value: T;
96
+ ttl?: number;
97
+ }>;
98
+ error?: unknown;
99
+ }) => void;
82
100
  del: (data: {
83
101
  key: string;
84
102
  error?: unknown;
package/dist/index.js CHANGED
@@ -159,11 +159,35 @@ var createCache = (options) => {
159
159
  return result;
160
160
  };
161
161
  const mget = async (keys) => {
162
- const result = [];
163
- for (const key of keys) {
164
- const data = await get(key);
165
- result.push(data);
162
+ let result = keys.map(() => void 0);
163
+ if (nonBlocking) {
164
+ try {
165
+ result = await Promise.race(stores.map(async (store) => store.getMany(keys)));
166
+ } catch (error) {
167
+ eventEmitter.emit("mget", { keys, error });
168
+ }
169
+ } else {
170
+ for (const store of stores) {
171
+ try {
172
+ const missingValues = result.map((value, index) => value === void 0 ? { originalIndex: index, key: keys[index] } : void 0).filter((v) => v !== void 0);
173
+ if (missingValues.length === 0) {
174
+ break;
175
+ }
176
+ const missingKeys = missingValues.map((v) => v.key);
177
+ const cacheValue = await store.getMany(missingKeys);
178
+ for (const [index, value] of cacheValue.entries()) {
179
+ if (value === void 0) {
180
+ continue;
181
+ }
182
+ const { originalIndex } = missingValues[index];
183
+ result[originalIndex] = value;
184
+ }
185
+ } catch (error) {
186
+ eventEmitter.emit("mget", { keys, error });
187
+ }
188
+ }
166
189
  }
190
+ eventEmitter.emit("mget", { keys, values: result });
167
191
  return result;
168
192
  };
169
193
  const ttl = async (key) => {
@@ -211,13 +235,10 @@ var createCache = (options) => {
211
235
  return Promise.reject(error);
212
236
  }
213
237
  };
214
- const mset = async (stores2, list) => {
215
- const items = list.map(({ key, value, ttl: ttl2 }) => ({ key, value, ttl: ttl2 }));
238
+ const mset = async (stores2, rawList) => {
239
+ const list = rawList.map(({ key, value, ttl: ttl2 }) => ({ key, value, ttl: ttl2 ?? options?.ttl }));
216
240
  try {
217
- const promises = [];
218
- for (const item of list) {
219
- promises.push(...stores2.map(async (store) => store.set(item.key, item.value, item.ttl)));
220
- }
241
+ const promises = stores2.map(async (store) => store.setMany(list));
221
242
  if (nonBlocking) {
222
243
  Promise.all(promises);
223
244
  eventEmitter.emit("mset", { list });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cache-manager",
3
- "version": "7.0.0",
3
+ "version": "7.1.0",
4
4
  "description": "Cache Manager for Node.js",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -50,21 +50,21 @@
50
50
  ],
51
51
  "license": "MIT",
52
52
  "dependencies": {
53
- "keyv": "^5.3.3"
53
+ "keyv": "^5.3.4"
54
54
  },
55
55
  "devDependencies": {
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",
56
+ "@faker-js/faker": "^9.9.0",
57
+ "@keyv/redis": "^4.6.0",
58
+ "@types/node": "^24.0.14",
59
+ "@vitest/coverage-v8": "^3.2.4",
60
+ "@vitest/spy": "^3.2.4",
61
61
  "cache-manager-redis-yet": "^5.1.5",
62
62
  "rimraf": "^6.0.1",
63
63
  "tsup": "^8.5.0",
64
64
  "typescript": "^5.8.3",
65
- "vitest": "^3.2.2",
66
- "xo": "^1.0.5",
67
- "cacheable": "^1.9.0"
65
+ "vitest": "^3.2.4",
66
+ "xo": "^1.2.0",
67
+ "cacheable": "^1.10.3"
68
68
  },
69
69
  "xo": {
70
70
  "rules": {
@@ -75,7 +75,7 @@
75
75
  "scripts": {
76
76
  "clean": "rimraf ./dist ./coverage ./node_modules",
77
77
  "test": "xo --fix && vitest run --coverage",
78
- "test:ci": "xo && vitest run",
78
+ "test:ci": "xo && vitest run --coverage",
79
79
  "build": "rimraf ./dist && tsup src/index.ts --format cjs,esm --dts --clean",
80
80
  "prepublish": "pnpm run build"
81
81
  }