cache-manager 6.4.0 → 6.4.2

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
@@ -24,6 +24,47 @@ 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)
25
25
  * [v4 Documentation](https://github.com/jaredwray/cacheable/blob/main/packages/cache-manager/READMEv4.md)
26
26
 
27
+ # Migration from v5 to v6
28
+
29
+ `v6` is a major update and has breaking changes primarily around the storage adapters. We have moved to using [Keyv](https://keyv.org/) which are more actively maintained and have a larger community. Below are the changes you need to make to migrate from `v5` to `v6`. In `v5` the `memoryStore` was used to create a memory store, in `v6` you can use any storage adapter that Keyv supports. Below is an example of how to migrate from `v5` to `v6`:
30
+
31
+ ```ts
32
+ import { createCache, memoryStore } from 'cache-manager';
33
+
34
+ // Create memory cache synchronously
35
+ const memoryCache = createCache(memoryStore({
36
+ max: 100,
37
+ ttl: 10 * 1000 /*milliseconds*/,
38
+ }));
39
+ ```
40
+
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:
42
+
43
+ ```ts
44
+ import { createCache } from 'cache-manager';
45
+ import { Keyv } from 'keyv';
46
+
47
+ const cache = createCache({
48
+ stores: [new Keyv()],
49
+ });
50
+ ```
51
+
52
+ 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
+
54
+ ```ts
55
+ import { createCache } from 'cache-manager';
56
+ import { KeyvCacheableMemory } from 'cacheable';
57
+
58
+ const cache = createCache({
59
+ stores: [new KeyvCacheableMemory({ ttl: 60000, lruSize: 5000 })],
60
+ });
61
+ ```
62
+
63
+ To learn more about `CacheableMemory` please visit: http://cacheable.org/docs/cacheable/#cacheablememory---in-memory-cache
64
+
65
+ 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
+
67
+
27
68
  ## Table of Contents
28
69
  * [Installation](#installation)
29
70
  * [Quick start](#quick-start)
@@ -322,10 +363,16 @@ See unit tests in [`test/clear.test.ts`](./test/clear.test.ts) for more informat
322
363
  ## wrap
323
364
  `wrap(key, fn: async () => value, [ttl], [refreshThreshold]): Promise<value>`
324
365
 
366
+ Alternatively, with optional parameters as options object supporting a `raw` parameter:
367
+
368
+ `wrap(key, fn: async () => value, { ttl?: number, refreshThreshold?: number, raw?: true }): Promise<value>`
369
+
325
370
  Wraps a function in cache. The first time the function is run, its results are stored in cache so subsequent calls retrieve from cache instead of calling the function.
326
371
 
327
372
  If `refreshThreshold` is set and the remaining TTL is less than `refreshThreshold`, the system will update the value asynchronously. In the meantime, the system will return the old value until expiration. You can also provide a function that will return the refreshThreshold based on the value `(value:T) => number`.
328
373
 
374
+ If the object format for the optional parameters is used, an additional `raw` parameter can be applied, changing the function return type to raw data including expiration timestamp as `{ value: [data], expires: [timestamp] }`.
375
+
329
376
  ```typescript
330
377
  await cache.wrap('key', () => 1, 5000, 3000)
331
378
  // call function then save the result to cache
@@ -335,6 +382,10 @@ await cache.wrap('key', () => 2, 5000, 3000)
335
382
  // return data from cache, function will not be called again
336
383
  // => 1
337
384
 
385
+ await cache.wrap('key', () => 2, { ttl: 5000, refreshThreshold: 3000, raw: true })
386
+ // returns raw data including expiration timestamp
387
+ // => { value: 1, expires: [timestamp] }
388
+
338
389
  // wait 3 seconds
339
390
  await sleep(3000)
340
391
 
package/dist/index.cjs CHANGED
@@ -92,6 +92,11 @@ async function coalesceAsync(key, fnc) {
92
92
  return enqueue(key);
93
93
  }
94
94
 
95
+ // src/is-object.ts
96
+ function isObject(value) {
97
+ return value !== null && typeof value === "object" && !Array.isArray(value);
98
+ }
99
+
95
100
  // src/run-if-fn.ts
96
101
  function runIfFn(valueOrFunction, ...arguments_) {
97
102
  return typeof valueOrFunction === "function" ? valueOrFunction(...arguments_) : valueOrFunction;
@@ -306,16 +311,19 @@ var createCache = (options) => {
306
311
  return Promise.reject(error);
307
312
  }
308
313
  };
309
- const wrap = async (key, fnc, ttl2, refreshThreshold) => coalesceAsync(`${_cacheId}::${key}`, async () => {
314
+ const wrap = async (key, fnc, ttlOrOptions, refreshThresholdParameter) => coalesceAsync(`${_cacheId}::${key}`, async () => {
310
315
  let value;
316
+ let rawData;
311
317
  let i = 0;
312
318
  let remainingTtl;
319
+ const { ttl: ttl2, refreshThreshold, raw } = isObject(ttlOrOptions) ? ttlOrOptions : { ttl: ttlOrOptions, refreshThreshold: refreshThresholdParameter };
313
320
  const resolveTtl = (result) => runIfFn(ttl2, result) ?? options?.ttl;
314
321
  for (; i < stores.length; i++) {
315
322
  try {
316
323
  const data = await stores[i].get(key, { raw: true });
317
324
  if (data !== void 0) {
318
325
  value = data.value;
326
+ rawData = data;
319
327
  if (typeof data.expires === "number") {
320
328
  remainingTtl = Math.max(0, data.expires - Date.now());
321
329
  }
@@ -326,8 +334,9 @@ var createCache = (options) => {
326
334
  }
327
335
  if (value === void 0) {
328
336
  const result = await fnc();
329
- await set(stores, key, result, resolveTtl(result));
330
- return result;
337
+ const ttl3 = resolveTtl(result);
338
+ await set(stores, key, result, ttl3);
339
+ return raw ? { value: result, expires: Date.now() + ttl3 } : result;
331
340
  }
332
341
  const shouldRefresh = lt(remainingTtl, runIfFn(refreshThreshold, value) ?? options?.refreshThreshold);
333
342
  if (shouldRefresh) {
@@ -345,7 +354,7 @@ var createCache = (options) => {
345
354
  if (!shouldRefresh && i > 0) {
346
355
  await set(stores.slice(0, i), key, value, resolveTtl(value));
347
356
  }
348
- return value;
357
+ return raw ? rawData : value;
349
358
  });
350
359
  const on = (event, listener) => eventEmitter.addListener(event, listener);
351
360
  const off = (event, listener) => eventEmitter.removeListener(event, listener);
package/dist/index.d.cts CHANGED
@@ -1,5 +1,5 @@
1
1
  import EventEmitter from 'node:events';
2
- import { KeyvStoreAdapter, StoredData, Keyv } from 'keyv';
2
+ import { KeyvStoreAdapter, StoredData, Keyv, StoredDataRaw } from 'keyv';
3
3
 
4
4
  type CacheManagerStore = {
5
5
  name: string;
@@ -40,9 +40,16 @@ type CreateCacheOptions = {
40
40
  nonBlocking?: boolean;
41
41
  cacheId?: string;
42
42
  };
43
+ type WrapOptions<T> = {
44
+ ttl?: number | ((value: T) => number);
45
+ refreshThreshold?: number | ((value: T) => number);
46
+ };
47
+ type WrapOptionsRaw<T> = WrapOptions<T> & {
48
+ raw: true;
49
+ };
43
50
  type Cache = {
44
51
  get: <T>(key: string) => Promise<T | null>;
45
- mget: <T>(keys: string[]) => Promise<[T]>;
52
+ mget: <T>(keys: string[]) => Promise<Array<T | null>>;
46
53
  ttl: (key: string) => Promise<number | null>;
47
54
  set: <T>(key: string, value: T, ttl?: number) => Promise<T>;
48
55
  mset: <T>(list: Array<{
@@ -57,12 +64,14 @@ type Cache = {
57
64
  del: (key: string) => Promise<boolean>;
58
65
  mdel: (keys: string[]) => Promise<boolean>;
59
66
  clear: () => Promise<boolean>;
60
- wrap: <T>(key: string, fnc: () => T | Promise<T>, ttl?: number | ((value: T) => number), refreshThreshold?: number | ((value: T) => number)) => Promise<T>;
61
67
  on: <E extends keyof Events>(event: E, listener: Events[E]) => EventEmitter;
62
68
  off: <E extends keyof Events>(event: E, listener: Events[E]) => EventEmitter;
63
69
  disconnect: () => Promise<undefined>;
64
70
  cacheId: () => string;
65
71
  stores: Keyv[];
72
+ wrap<T>(key: string, fnc: () => T | Promise<T>, ttl?: number | ((value: T) => number), refreshThreshold?: number | ((value: T) => number)): Promise<T>;
73
+ wrap<T>(key: string, fnc: () => T | Promise<T>, options: WrapOptions<T>): Promise<T>;
74
+ wrap<T>(key: string, fnc: () => T | Promise<T>, options: WrapOptionsRaw<T>): Promise<StoredDataRaw<T>>;
66
75
  };
67
76
  type Events = {
68
77
  set: <T>(data: {
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import EventEmitter from 'node:events';
2
- import { KeyvStoreAdapter, StoredData, Keyv } from 'keyv';
2
+ import { KeyvStoreAdapter, StoredData, Keyv, StoredDataRaw } from 'keyv';
3
3
 
4
4
  type CacheManagerStore = {
5
5
  name: string;
@@ -40,9 +40,16 @@ type CreateCacheOptions = {
40
40
  nonBlocking?: boolean;
41
41
  cacheId?: string;
42
42
  };
43
+ type WrapOptions<T> = {
44
+ ttl?: number | ((value: T) => number);
45
+ refreshThreshold?: number | ((value: T) => number);
46
+ };
47
+ type WrapOptionsRaw<T> = WrapOptions<T> & {
48
+ raw: true;
49
+ };
43
50
  type Cache = {
44
51
  get: <T>(key: string) => Promise<T | null>;
45
- mget: <T>(keys: string[]) => Promise<[T]>;
52
+ mget: <T>(keys: string[]) => Promise<Array<T | null>>;
46
53
  ttl: (key: string) => Promise<number | null>;
47
54
  set: <T>(key: string, value: T, ttl?: number) => Promise<T>;
48
55
  mset: <T>(list: Array<{
@@ -57,12 +64,14 @@ type Cache = {
57
64
  del: (key: string) => Promise<boolean>;
58
65
  mdel: (keys: string[]) => Promise<boolean>;
59
66
  clear: () => Promise<boolean>;
60
- wrap: <T>(key: string, fnc: () => T | Promise<T>, ttl?: number | ((value: T) => number), refreshThreshold?: number | ((value: T) => number)) => Promise<T>;
61
67
  on: <E extends keyof Events>(event: E, listener: Events[E]) => EventEmitter;
62
68
  off: <E extends keyof Events>(event: E, listener: Events[E]) => EventEmitter;
63
69
  disconnect: () => Promise<undefined>;
64
70
  cacheId: () => string;
65
71
  stores: Keyv[];
72
+ wrap<T>(key: string, fnc: () => T | Promise<T>, ttl?: number | ((value: T) => number), refreshThreshold?: number | ((value: T) => number)): Promise<T>;
73
+ wrap<T>(key: string, fnc: () => T | Promise<T>, options: WrapOptions<T>): Promise<T>;
74
+ wrap<T>(key: string, fnc: () => T | Promise<T>, options: WrapOptionsRaw<T>): Promise<StoredDataRaw<T>>;
66
75
  };
67
76
  type Events = {
68
77
  set: <T>(data: {
package/dist/index.js CHANGED
@@ -57,6 +57,11 @@ async function coalesceAsync(key, fnc) {
57
57
  return enqueue(key);
58
58
  }
59
59
 
60
+ // src/is-object.ts
61
+ function isObject(value) {
62
+ return value !== null && typeof value === "object" && !Array.isArray(value);
63
+ }
64
+
60
65
  // src/run-if-fn.ts
61
66
  function runIfFn(valueOrFunction, ...arguments_) {
62
67
  return typeof valueOrFunction === "function" ? valueOrFunction(...arguments_) : valueOrFunction;
@@ -271,16 +276,19 @@ var createCache = (options) => {
271
276
  return Promise.reject(error);
272
277
  }
273
278
  };
274
- const wrap = async (key, fnc, ttl2, refreshThreshold) => coalesceAsync(`${_cacheId}::${key}`, async () => {
279
+ const wrap = async (key, fnc, ttlOrOptions, refreshThresholdParameter) => coalesceAsync(`${_cacheId}::${key}`, async () => {
275
280
  let value;
281
+ let rawData;
276
282
  let i = 0;
277
283
  let remainingTtl;
284
+ const { ttl: ttl2, refreshThreshold, raw } = isObject(ttlOrOptions) ? ttlOrOptions : { ttl: ttlOrOptions, refreshThreshold: refreshThresholdParameter };
278
285
  const resolveTtl = (result) => runIfFn(ttl2, result) ?? options?.ttl;
279
286
  for (; i < stores.length; i++) {
280
287
  try {
281
288
  const data = await stores[i].get(key, { raw: true });
282
289
  if (data !== void 0) {
283
290
  value = data.value;
291
+ rawData = data;
284
292
  if (typeof data.expires === "number") {
285
293
  remainingTtl = Math.max(0, data.expires - Date.now());
286
294
  }
@@ -291,8 +299,9 @@ var createCache = (options) => {
291
299
  }
292
300
  if (value === void 0) {
293
301
  const result = await fnc();
294
- await set(stores, key, result, resolveTtl(result));
295
- return result;
302
+ const ttl3 = resolveTtl(result);
303
+ await set(stores, key, result, ttl3);
304
+ return raw ? { value: result, expires: Date.now() + ttl3 } : result;
296
305
  }
297
306
  const shouldRefresh = lt(remainingTtl, runIfFn(refreshThreshold, value) ?? options?.refreshThreshold);
298
307
  if (shouldRefresh) {
@@ -310,7 +319,7 @@ var createCache = (options) => {
310
319
  if (!shouldRefresh && i > 0) {
311
320
  await set(stores.slice(0, i), key, value, resolveTtl(value));
312
321
  }
313
- return value;
322
+ return raw ? rawData : value;
314
323
  });
315
324
  const on = (event, listener) => eventEmitter.addListener(event, listener);
316
325
  const off = (event, listener) => eventEmitter.removeListener(event, listener);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cache-manager",
3
- "version": "6.4.0",
3
+ "version": "6.4.2",
4
4
  "description": "Cache Manager for Node.js",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -50,20 +50,20 @@
50
50
  ],
51
51
  "license": "MIT",
52
52
  "dependencies": {
53
- "keyv": "^5.2.3"
53
+ "keyv": "^5.3.2"
54
54
  },
55
55
  "devDependencies": {
56
- "@faker-js/faker": "^9.4.0",
57
- "@keyv/redis": "^4.2.0",
58
- "@types/node": "^22.10.9",
59
- "@vitest/coverage-v8": "^3.0.4",
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",
60
60
  "cache-manager-redis-yet": "^5.1.5",
61
61
  "rimraf": "^6.0.1",
62
- "tsup": "^8.3.5",
63
- "typescript": "^5.7.3",
64
- "vitest": "^3.0.4",
62
+ "tsup": "^8.4.0",
63
+ "typescript": "^5.8.2",
64
+ "vitest": "^3.1.1",
65
65
  "xo": "^0.60.0",
66
- "cacheable": "^1.8.8"
66
+ "cacheable": "^1.8.10"
67
67
  },
68
68
  "xo": {
69
69
  "rules": {