cache-manager 6.1.3 → 6.3.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
@@ -1,6 +1,6 @@
1
1
  [<img align="center" src="https://cacheable.org/symbol.svg" alt="Cacheable" />](https://github.com/jaredwray/cacheable)
2
2
 
3
- # cache-manager
3
+ # cache-manager
4
4
  [![codecov](https://codecov.io/gh/jaredwray/cacheable/graph/badge.svg?token=lWZ9OBQ7GM)](https://codecov.io/gh/jaredwray/cacheable)
5
5
  [![tests](https://github.com/jaredwray/cacheable/actions/workflows/tests.yml/badge.svg)](https://github.com/jaredwray/cacheable/actions/workflows/tests.yml)
6
6
  [![npm](https://img.shields.io/npm/dm/cache-manager)](https://npmjs.com/package/cache-manager)
@@ -18,7 +18,7 @@ A cache module for NodeJS that allows easy wrapping of functions in cache, tiere
18
18
 
19
19
  We moved to using [Keyv](https://keyv.org/) which are more actively maintained and have a larger community.
20
20
 
21
- A special thanks to [Tim Phan](https://github.com/timphandev) who tooke `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.
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
23
  If you are looking for older documentation you can find it here:
24
24
  * [v5 Documentation](https://github.com/jaredwray/cacheable/blob/main/packages/cache-manager/READMEv5.md)
@@ -167,6 +167,9 @@ const cache = createCache({ stores: [keyv] });
167
167
  - **refreshThreshold**?: number - Default refreshThreshold in milliseconds.
168
168
 
169
169
  If the remaining TTL is less than **refreshThreshold**, the system will update the value asynchronously in background.
170
+ - **refreshAllStores**?: boolean - Default false
171
+
172
+ If set to true, the system will update the value of all stores when the refreshThreshold is met. Otherwise, it will only update from the top to the store that triggered the refresh.
170
173
 
171
174
  - **nonBlocking**?: boolean - Default false
172
175
 
@@ -332,6 +335,18 @@ await cache.wrap('error', () => {
332
335
 
333
336
  See unit tests in [`test/wrap.test.ts`](./test/wrap.test.ts) for more information.
334
337
 
338
+ ## disconnect
339
+
340
+ `disconnect(key): Promise<void>`
341
+
342
+ Will disconnect from the relevant store(s). It is highly recomended to use this when using a [Keyv](https://keyv.org/) storage adapter that requires a disconnect. For each storage adapter, the use case for when to use disconnect is different. An example is that `@keyv/redis` should be used only when you are done with the cache.
343
+
344
+ ```ts
345
+ await cache.disconnect();
346
+ ```
347
+
348
+ See unit tests in [`test/disconnect.test.ts`](./test/disconnect.test.ts) for more information.
349
+
335
350
  # Events
336
351
  ## set
337
352
  Fired when a key has been added or changed.
package/dist/index.cjs CHANGED
@@ -146,6 +146,9 @@ var KeyvAdapter = class {
146
146
  this._cache.on?.(event, listener);
147
147
  return this;
148
148
  }
149
+ async disconnect() {
150
+ await this._cache.disconnect?.();
151
+ }
149
152
  };
150
153
 
151
154
  // src/index.ts
@@ -210,6 +213,11 @@ var createCache = (options) => {
210
213
  for (const item of list) {
211
214
  promises.push(stores2.map(async (store) => store.set(item.key, item.value, item.ttl)));
212
215
  }
216
+ if (nonBlocking) {
217
+ Promise.all(promises);
218
+ eventEmitter.emit("mset", { list });
219
+ return list;
220
+ }
213
221
  await Promise.all(promises);
214
222
  eventEmitter.emit("mset", { list });
215
223
  return list;
@@ -294,7 +302,7 @@ var createCache = (options) => {
294
302
  if (shouldRefresh) {
295
303
  coalesceAsync(`+++${key}`, fnc).then(async (result) => {
296
304
  try {
297
- await set(stores.slice(0, i + 1), key, result, ms);
305
+ await set(options?.refreshAllStores ? stores : stores.slice(0, i + 1), key, result, ms);
298
306
  eventEmitter.emit("refresh", { key, value: result });
299
307
  } catch (error) {
300
308
  eventEmitter.emit("refresh", { key, value, error });
@@ -310,6 +318,13 @@ var createCache = (options) => {
310
318
  });
311
319
  const on = (event, listener) => eventEmitter.addListener(event, listener);
312
320
  const off = (event, listener) => eventEmitter.removeListener(event, listener);
321
+ const disconnect = async () => {
322
+ try {
323
+ await Promise.all(stores.map(async (store) => store.disconnect()));
324
+ } catch (error) {
325
+ return Promise.reject(error);
326
+ }
327
+ };
313
328
  return {
314
329
  get,
315
330
  mget,
@@ -320,7 +335,8 @@ var createCache = (options) => {
320
335
  clear,
321
336
  wrap,
322
337
  on,
323
- off
338
+ off,
339
+ disconnect
324
340
  };
325
341
  };
326
342
  // Annotate the CommonJS export names for ESM import in node:
package/dist/index.d.cts CHANGED
@@ -14,6 +14,7 @@ type CacheManagerStore = {
14
14
  keys(): Promise<string[]>;
15
15
  reset?(): Promise<void>;
16
16
  on?(event: string, listener: (...arguments_: any[]) => void): void;
17
+ disconnect?(): Promise<void>;
17
18
  };
18
19
  declare class KeyvAdapter implements KeyvStoreAdapter {
19
20
  opts: any;
@@ -28,12 +29,14 @@ declare class KeyvAdapter implements KeyvStoreAdapter {
28
29
  getMany?<T>(keys: string[]): Promise<Array<StoredData<T | undefined>>>;
29
30
  deleteMany?(key: string[]): Promise<boolean>;
30
31
  on(event: string, listener: (...arguments_: any[]) => void): this;
32
+ disconnect?(): Promise<void>;
31
33
  }
32
34
 
33
35
  type CreateCacheOptions = {
34
36
  stores?: Keyv[];
35
37
  ttl?: number;
36
38
  refreshThreshold?: number;
39
+ refreshAllStores?: boolean;
37
40
  nonBlocking?: boolean;
38
41
  };
39
42
  type Events = {
@@ -72,6 +75,7 @@ declare const createCache: (options?: CreateCacheOptions) => {
72
75
  wrap: <T>(key: string, fnc: () => T | Promise<T>, ttl?: number | ((value: T) => number), refreshThreshold?: number) => Promise<T>;
73
76
  on: <E extends keyof Events>(event: E, listener: Events[E]) => EventEmitter<[never]>;
74
77
  off: <E extends keyof Events>(event: E, listener: Events[E]) => EventEmitter<[never]>;
78
+ disconnect: () => Promise<undefined>;
75
79
  };
76
80
 
77
81
  export { type CacheManagerStore, type CreateCacheOptions, type Events, KeyvAdapter, createCache };
package/dist/index.d.ts CHANGED
@@ -14,6 +14,7 @@ type CacheManagerStore = {
14
14
  keys(): Promise<string[]>;
15
15
  reset?(): Promise<void>;
16
16
  on?(event: string, listener: (...arguments_: any[]) => void): void;
17
+ disconnect?(): Promise<void>;
17
18
  };
18
19
  declare class KeyvAdapter implements KeyvStoreAdapter {
19
20
  opts: any;
@@ -28,12 +29,14 @@ declare class KeyvAdapter implements KeyvStoreAdapter {
28
29
  getMany?<T>(keys: string[]): Promise<Array<StoredData<T | undefined>>>;
29
30
  deleteMany?(key: string[]): Promise<boolean>;
30
31
  on(event: string, listener: (...arguments_: any[]) => void): this;
32
+ disconnect?(): Promise<void>;
31
33
  }
32
34
 
33
35
  type CreateCacheOptions = {
34
36
  stores?: Keyv[];
35
37
  ttl?: number;
36
38
  refreshThreshold?: number;
39
+ refreshAllStores?: boolean;
37
40
  nonBlocking?: boolean;
38
41
  };
39
42
  type Events = {
@@ -72,6 +75,7 @@ declare const createCache: (options?: CreateCacheOptions) => {
72
75
  wrap: <T>(key: string, fnc: () => T | Promise<T>, ttl?: number | ((value: T) => number), refreshThreshold?: number) => Promise<T>;
73
76
  on: <E extends keyof Events>(event: E, listener: Events[E]) => EventEmitter<[never]>;
74
77
  off: <E extends keyof Events>(event: E, listener: Events[E]) => EventEmitter<[never]>;
78
+ disconnect: () => Promise<undefined>;
75
79
  };
76
80
 
77
81
  export { type CacheManagerStore, type CreateCacheOptions, type Events, KeyvAdapter, createCache };
package/dist/index.js CHANGED
@@ -111,6 +111,9 @@ var KeyvAdapter = class {
111
111
  this._cache.on?.(event, listener);
112
112
  return this;
113
113
  }
114
+ async disconnect() {
115
+ await this._cache.disconnect?.();
116
+ }
114
117
  };
115
118
 
116
119
  // src/index.ts
@@ -175,6 +178,11 @@ var createCache = (options) => {
175
178
  for (const item of list) {
176
179
  promises.push(stores2.map(async (store) => store.set(item.key, item.value, item.ttl)));
177
180
  }
181
+ if (nonBlocking) {
182
+ Promise.all(promises);
183
+ eventEmitter.emit("mset", { list });
184
+ return list;
185
+ }
178
186
  await Promise.all(promises);
179
187
  eventEmitter.emit("mset", { list });
180
188
  return list;
@@ -259,7 +267,7 @@ var createCache = (options) => {
259
267
  if (shouldRefresh) {
260
268
  coalesceAsync(`+++${key}`, fnc).then(async (result) => {
261
269
  try {
262
- await set(stores.slice(0, i + 1), key, result, ms);
270
+ await set(options?.refreshAllStores ? stores : stores.slice(0, i + 1), key, result, ms);
263
271
  eventEmitter.emit("refresh", { key, value: result });
264
272
  } catch (error) {
265
273
  eventEmitter.emit("refresh", { key, value, error });
@@ -275,6 +283,13 @@ var createCache = (options) => {
275
283
  });
276
284
  const on = (event, listener) => eventEmitter.addListener(event, listener);
277
285
  const off = (event, listener) => eventEmitter.removeListener(event, listener);
286
+ const disconnect = async () => {
287
+ try {
288
+ await Promise.all(stores.map(async (store) => store.disconnect()));
289
+ } catch (error) {
290
+ return Promise.reject(error);
291
+ }
292
+ };
278
293
  return {
279
294
  get,
280
295
  mget,
@@ -285,7 +300,8 @@ var createCache = (options) => {
285
300
  clear,
286
301
  wrap,
287
302
  on,
288
- off
303
+ off,
304
+ disconnect
289
305
  };
290
306
  };
291
307
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cache-manager",
3
- "version": "6.1.3",
3
+ "version": "6.3.0",
4
4
  "description": "Cache Manager for Node.js",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",