cache-manager 5.3.2 → 5.4.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
@@ -158,7 +158,7 @@ See unit tests in [`test/multi-caching.test.ts`](./test/multi-caching.test.ts) f
158
158
  ### Refresh cache keys in background
159
159
 
160
160
  Both the `caching` and `multicaching` modules support a mechanism to refresh expiring cache keys in background when using the `wrap` function.
161
- This is done by adding a `refreshThreshold` attribute while creating the caching store.
161
+ This is done by adding a `refreshThreshold` attribute while creating the caching store or passing it to the `wrap` function.
162
162
 
163
163
  If `refreshThreshold` is set and after retrieving a value from cache the TTL will be checked.
164
164
  If the remaining TTL is less than `refreshThreshold`, the system will update the value asynchronously,
package/dist/caching.d.ts CHANGED
@@ -31,7 +31,7 @@ export type Cache<S extends Store = Store> = {
31
31
  get: <T>(key: string) => Promise<T | undefined>;
32
32
  del: (key: string) => Promise<void>;
33
33
  reset: () => Promise<void>;
34
- wrap<T>(key: string, fn: () => Promise<T>, ttl?: WrapTTL<T>): Promise<T>;
34
+ wrap<T>(key: string, fn: () => Promise<T>, ttl?: WrapTTL<T>, refreshThreshold?: Milliseconds): Promise<T>;
35
35
  store: S;
36
36
  };
37
37
  export declare function caching(name: 'memory', args?: MemoryConfig): Promise<MemoryCache>;
package/dist/caching.js CHANGED
@@ -33,7 +33,8 @@ function createCache(store, args) {
33
33
  * const result = await cache.wrap('key', () => Promise.resolve(1));
34
34
  *
35
35
  */
36
- wrap: async (key, fn, ttl) => {
36
+ wrap: async (key, fn, ttl, refreshThreshold) => {
37
+ const refreshThresholdConfig = refreshThreshold || args?.refreshThreshold || 0;
37
38
  return (0, promise_coalesce_1.coalesceAsync)(key, async () => {
38
39
  const value = await store.get(key);
39
40
  if (value === undefined) {
@@ -42,10 +43,10 @@ function createCache(store, args) {
42
43
  await store.set(key, result, cacheTTL);
43
44
  return result;
44
45
  }
45
- else if (args?.refreshThreshold) {
46
+ else if (refreshThresholdConfig) {
46
47
  const cacheTTL = typeof ttl === 'function' ? ttl(value) : ttl;
47
48
  const remainingTtl = await store.ttl(key);
48
- if (remainingTtl !== -1 && remainingTtl < args.refreshThreshold) {
49
+ if (remainingTtl !== -1 && remainingTtl < refreshThresholdConfig) {
49
50
  (0, promise_coalesce_1.coalesceAsync)(`+++${key}`, fn).then((result) => store.set(key, result, cacheTTL));
50
51
  }
51
52
  }
@@ -24,7 +24,7 @@ function multiCaching(caches) {
24
24
  del: async (key) => {
25
25
  await Promise.all(caches.map((cache) => cache.del(key)));
26
26
  },
27
- async wrap(key, fn, ttl) {
27
+ async wrap(key, fn, ttl, refreshThreshold) {
28
28
  let value;
29
29
  let i = 0;
30
30
  for (; i < caches.length; i++) {
@@ -44,7 +44,7 @@ function multiCaching(caches) {
44
44
  else {
45
45
  const cacheTTL = typeof ttl === 'function' ? ttl(value) : ttl;
46
46
  Promise.all(caches.slice(0, i).map((cache) => cache.set(key, value, cacheTTL))).then();
47
- caches[i].wrap(key, fn, ttl).then(); // call wrap for store for internal refreshThreshold logic, see: src/caching.ts caching.wrap
47
+ caches[i].wrap(key, fn, ttl, refreshThreshold).then(); // call wrap for store for internal refreshThreshold logic, see: src/caching.ts caching.wrap
48
48
  }
49
49
  return value;
50
50
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cache-manager",
3
- "version": "5.3.2",
3
+ "version": "5.4.0",
4
4
  "description": "Cache module for Node.js",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -54,19 +54,19 @@
54
54
  "devDependencies": {
55
55
  "@faker-js/faker": "8.3.1",
56
56
  "@types/lodash.clonedeep": "4.5.9",
57
- "@types/node": "20.10.5",
58
- "@typescript-eslint/eslint-plugin": "6.15.0",
59
- "@typescript-eslint/parser": "6.15.0",
60
- "@vitest/coverage-v8": "1.1.0",
57
+ "@types/node": "20.11.5",
58
+ "@typescript-eslint/eslint-plugin": "6.19.0",
59
+ "@typescript-eslint/parser": "6.19.0",
60
+ "@vitest/coverage-v8": "1.2.1",
61
61
  "dotenv-cli": "7.3.0",
62
62
  "eslint": "8.56.0",
63
63
  "eslint-config-prettier": "9.1.0",
64
- "eslint-plugin-prettier": "5.0.1",
64
+ "eslint-plugin-prettier": "5.1.3",
65
65
  "lint-staged": "15.2.0",
66
- "prettier": "3.1.1",
66
+ "prettier": "3.2.4",
67
67
  "rimraf": "^5.0.5",
68
68
  "typescript": "5.3.3",
69
- "vitest": "1.1.0"
69
+ "vitest": "1.2.1"
70
70
  },
71
71
  "lint-staged": {
72
72
  "*.{ts,js,mts,mjs}": "eslint --cache --fix",