cache-manager 6.2.0 → 6.3.1

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
@@ -34,6 +34,7 @@ If you are looking for older documentation you can find it here:
34
34
  * [.mset](#mset)
35
35
  * [.get](#get)
36
36
  * [.mget](#mget)
37
+ * [.ttl](#ttl)
37
38
  * [.del](#del)
38
39
  * [.mdel](#mdel)
39
40
  * [.clear](#clear)
@@ -167,6 +168,9 @@ const cache = createCache({ stores: [keyv] });
167
168
  - **refreshThreshold**?: number - Default refreshThreshold in milliseconds.
168
169
 
169
170
  If the remaining TTL is less than **refreshThreshold**, the system will update the value asynchronously in background.
171
+ - **refreshAllStores**?: boolean - Default false
172
+
173
+ 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
174
 
171
175
  - **nonBlocking**?: boolean - Default false
172
176
 
@@ -236,6 +240,20 @@ await cache.mget(['key-1', 'key-2', 'key-3'])
236
240
  // => ['value 1', 'value 2', null]
237
241
  ```
238
242
 
243
+ ## ttl
244
+ `ttl(key): Promise<number | null>`
245
+
246
+ Gets the expiration time of a key in milliseconds. Returns a null if not found or expired.
247
+
248
+ ```ts
249
+ await cache.set('key', 'value', 1000); // expires after 5 seconds
250
+
251
+ await cache.ttl('key'); // => the expiration time in milliseconds
252
+
253
+ await cache.get('foo'); // => null
254
+ ```
255
+ See unit tests in [`test/ttl.test.ts`](./test/ttl.test.ts) for more information.
256
+
239
257
  ## del
240
258
  `del(key): Promise<true>`
241
259
 
package/dist/index.cjs CHANGED
@@ -191,14 +191,44 @@ var createCache = (options) => {
191
191
  }
192
192
  return result;
193
193
  };
194
- const set = async (stores2, key, value, ttl) => {
194
+ const ttl = async (key) => {
195
+ let result = null;
196
+ if (nonBlocking) {
197
+ try {
198
+ result = await Promise.race(stores.map(async (store) => store.get(key, { raw: true })));
199
+ if (result === void 0) {
200
+ return null;
201
+ }
202
+ } catch (error) {
203
+ eventEmitter.emit("ttl", { key, error });
204
+ }
205
+ } else {
206
+ for (const store of stores) {
207
+ try {
208
+ const cacheValue = await store.get(key, { raw: true });
209
+ if (cacheValue !== void 0) {
210
+ result = cacheValue;
211
+ eventEmitter.emit("ttl", { key, value: result });
212
+ break;
213
+ }
214
+ } catch (error) {
215
+ eventEmitter.emit("ttl", { key, error });
216
+ }
217
+ }
218
+ }
219
+ if (result?.expires) {
220
+ return result.expires;
221
+ }
222
+ return null;
223
+ };
224
+ const set = async (stores2, key, value, ttl2) => {
195
225
  try {
196
226
  if (nonBlocking) {
197
- Promise.all(stores2.map(async (store) => store.set(key, value, ttl ?? options?.ttl)));
227
+ Promise.all(stores2.map(async (store) => store.set(key, value, ttl2 ?? options?.ttl)));
198
228
  eventEmitter.emit("set", { key, value });
199
229
  return value;
200
230
  }
201
- await Promise.all(stores2.map(async (store) => store.set(key, value, ttl ?? options?.ttl)));
231
+ await Promise.all(stores2.map(async (store) => store.set(key, value, ttl2 ?? options?.ttl)));
202
232
  eventEmitter.emit("set", { key, value });
203
233
  return value;
204
234
  } catch (error) {
@@ -207,7 +237,7 @@ var createCache = (options) => {
207
237
  }
208
238
  };
209
239
  const mset = async (stores2, list) => {
210
- const items = list.map(({ key, value, ttl }) => ({ key, value, ttl }));
240
+ const items = list.map(({ key, value, ttl: ttl2 }) => ({ key, value, ttl: ttl2 }));
211
241
  try {
212
242
  const promises = [];
213
243
  for (const item of list) {
@@ -275,7 +305,7 @@ var createCache = (options) => {
275
305
  return Promise.reject(error);
276
306
  }
277
307
  };
278
- const wrap = async (key, fnc, ttl, refreshThreshold) => coalesceAsync(key, async () => {
308
+ const wrap = async (key, fnc, ttl2, refreshThreshold) => coalesceAsync(key, async () => {
279
309
  let value;
280
310
  let i = 0;
281
311
  let remainingTtl;
@@ -294,15 +324,15 @@ var createCache = (options) => {
294
324
  }
295
325
  if (value === void 0) {
296
326
  const result = await fnc();
297
- await set(stores, key, result, runIfFn(ttl, result) ?? options?.ttl);
327
+ await set(stores, key, result, runIfFn(ttl2, result) ?? options?.ttl);
298
328
  return result;
299
329
  }
300
- const ms = runIfFn(ttl, value) ?? options?.ttl;
330
+ const ms = runIfFn(ttl2, value) ?? options?.ttl;
301
331
  const shouldRefresh = lt(remainingTtl, refreshThreshold ?? options?.refreshThreshold);
302
332
  if (shouldRefresh) {
303
333
  coalesceAsync(`+++${key}`, fnc).then(async (result) => {
304
334
  try {
305
- await set(stores.slice(0, i + 1), key, result, ms);
335
+ await set(options?.refreshAllStores ? stores : stores.slice(0, i + 1), key, result, ms);
306
336
  eventEmitter.emit("refresh", { key, value: result });
307
337
  } catch (error) {
308
338
  eventEmitter.emit("refresh", { key, value, error });
@@ -328,7 +358,8 @@ var createCache = (options) => {
328
358
  return {
329
359
  get,
330
360
  mget,
331
- set: async (key, value, ttl) => set(stores, key, value, ttl),
361
+ ttl,
362
+ set: async (key, value, ttl2) => set(stores, key, value, ttl2),
332
363
  mset: async (list) => mset(stores, list),
333
364
  del,
334
365
  mdel,
package/dist/index.d.cts CHANGED
@@ -36,8 +36,31 @@ type CreateCacheOptions = {
36
36
  stores?: Keyv[];
37
37
  ttl?: number;
38
38
  refreshThreshold?: number;
39
+ refreshAllStores?: boolean;
39
40
  nonBlocking?: boolean;
40
41
  };
42
+ type Cache = {
43
+ get: <T>(key: string) => Promise<T | null>;
44
+ mget: <T>(keys: string[]) => Promise<[T]>;
45
+ ttl: (key: string) => Promise<number | null>;
46
+ set: <T>(key: string, value: T, ttl?: number) => Promise<T>;
47
+ mset: <T>(list: Array<{
48
+ key: string;
49
+ value: T;
50
+ ttl?: number;
51
+ }>) => Promise<Array<{
52
+ key: string;
53
+ value: T;
54
+ ttl?: number;
55
+ }>>;
56
+ del: (key: string) => Promise<boolean>;
57
+ mdel: (keys: string[]) => Promise<boolean>;
58
+ clear: () => Promise<boolean>;
59
+ wrap: <T>(key: string, fnc: () => T | Promise<T>, ttl?: number | ((value: T) => number), refreshThreshold?: number) => Promise<T>;
60
+ on: <E extends keyof Events>(event: E, listener: Events[E]) => EventEmitter;
61
+ off: <E extends keyof Events>(event: E, listener: Events[E]) => EventEmitter;
62
+ disconnect: () => Promise<undefined>;
63
+ };
41
64
  type Events = {
42
65
  set: <T>(data: {
43
66
  key: string;
@@ -55,26 +78,6 @@ type Events = {
55
78
  error?: unknown;
56
79
  }) => void;
57
80
  };
58
- declare const createCache: (options?: CreateCacheOptions) => {
59
- get: <T>(key: string) => Promise<T | null>;
60
- mget: <T>(keys: string[]) => Promise<[T]>;
61
- set: <T>(key: string, value: T, ttl?: number) => Promise<T>;
62
- mset: <T>(list: Array<{
63
- key: string;
64
- value: T;
65
- ttl?: number;
66
- }>) => Promise<{
67
- key: string;
68
- value: T;
69
- ttl?: number;
70
- }[]>;
71
- del: (key: string) => Promise<boolean>;
72
- mdel: (keys: string[]) => Promise<boolean>;
73
- clear: () => Promise<boolean>;
74
- wrap: <T>(key: string, fnc: () => T | Promise<T>, ttl?: number | ((value: T) => number), refreshThreshold?: number) => Promise<T>;
75
- on: <E extends keyof Events>(event: E, listener: Events[E]) => EventEmitter<[never]>;
76
- off: <E extends keyof Events>(event: E, listener: Events[E]) => EventEmitter<[never]>;
77
- disconnect: () => Promise<undefined>;
78
- };
81
+ declare const createCache: (options?: CreateCacheOptions) => Cache;
79
82
 
80
- export { type CacheManagerStore, type CreateCacheOptions, type Events, KeyvAdapter, createCache };
83
+ export { type Cache, type CacheManagerStore, type CreateCacheOptions, type Events, KeyvAdapter, createCache };
package/dist/index.d.ts CHANGED
@@ -36,8 +36,31 @@ type CreateCacheOptions = {
36
36
  stores?: Keyv[];
37
37
  ttl?: number;
38
38
  refreshThreshold?: number;
39
+ refreshAllStores?: boolean;
39
40
  nonBlocking?: boolean;
40
41
  };
42
+ type Cache = {
43
+ get: <T>(key: string) => Promise<T | null>;
44
+ mget: <T>(keys: string[]) => Promise<[T]>;
45
+ ttl: (key: string) => Promise<number | null>;
46
+ set: <T>(key: string, value: T, ttl?: number) => Promise<T>;
47
+ mset: <T>(list: Array<{
48
+ key: string;
49
+ value: T;
50
+ ttl?: number;
51
+ }>) => Promise<Array<{
52
+ key: string;
53
+ value: T;
54
+ ttl?: number;
55
+ }>>;
56
+ del: (key: string) => Promise<boolean>;
57
+ mdel: (keys: string[]) => Promise<boolean>;
58
+ clear: () => Promise<boolean>;
59
+ wrap: <T>(key: string, fnc: () => T | Promise<T>, ttl?: number | ((value: T) => number), refreshThreshold?: number) => Promise<T>;
60
+ on: <E extends keyof Events>(event: E, listener: Events[E]) => EventEmitter;
61
+ off: <E extends keyof Events>(event: E, listener: Events[E]) => EventEmitter;
62
+ disconnect: () => Promise<undefined>;
63
+ };
41
64
  type Events = {
42
65
  set: <T>(data: {
43
66
  key: string;
@@ -55,26 +78,6 @@ type Events = {
55
78
  error?: unknown;
56
79
  }) => void;
57
80
  };
58
- declare const createCache: (options?: CreateCacheOptions) => {
59
- get: <T>(key: string) => Promise<T | null>;
60
- mget: <T>(keys: string[]) => Promise<[T]>;
61
- set: <T>(key: string, value: T, ttl?: number) => Promise<T>;
62
- mset: <T>(list: Array<{
63
- key: string;
64
- value: T;
65
- ttl?: number;
66
- }>) => Promise<{
67
- key: string;
68
- value: T;
69
- ttl?: number;
70
- }[]>;
71
- del: (key: string) => Promise<boolean>;
72
- mdel: (keys: string[]) => Promise<boolean>;
73
- clear: () => Promise<boolean>;
74
- wrap: <T>(key: string, fnc: () => T | Promise<T>, ttl?: number | ((value: T) => number), refreshThreshold?: number) => Promise<T>;
75
- on: <E extends keyof Events>(event: E, listener: Events[E]) => EventEmitter<[never]>;
76
- off: <E extends keyof Events>(event: E, listener: Events[E]) => EventEmitter<[never]>;
77
- disconnect: () => Promise<undefined>;
78
- };
81
+ declare const createCache: (options?: CreateCacheOptions) => Cache;
79
82
 
80
- export { type CacheManagerStore, type CreateCacheOptions, type Events, KeyvAdapter, createCache };
83
+ export { type Cache, type CacheManagerStore, type CreateCacheOptions, type Events, KeyvAdapter, createCache };
package/dist/index.js CHANGED
@@ -156,14 +156,44 @@ var createCache = (options) => {
156
156
  }
157
157
  return result;
158
158
  };
159
- const set = async (stores2, key, value, ttl) => {
159
+ const ttl = async (key) => {
160
+ let result = null;
161
+ if (nonBlocking) {
162
+ try {
163
+ result = await Promise.race(stores.map(async (store) => store.get(key, { raw: true })));
164
+ if (result === void 0) {
165
+ return null;
166
+ }
167
+ } catch (error) {
168
+ eventEmitter.emit("ttl", { key, error });
169
+ }
170
+ } else {
171
+ for (const store of stores) {
172
+ try {
173
+ const cacheValue = await store.get(key, { raw: true });
174
+ if (cacheValue !== void 0) {
175
+ result = cacheValue;
176
+ eventEmitter.emit("ttl", { key, value: result });
177
+ break;
178
+ }
179
+ } catch (error) {
180
+ eventEmitter.emit("ttl", { key, error });
181
+ }
182
+ }
183
+ }
184
+ if (result?.expires) {
185
+ return result.expires;
186
+ }
187
+ return null;
188
+ };
189
+ const set = async (stores2, key, value, ttl2) => {
160
190
  try {
161
191
  if (nonBlocking) {
162
- Promise.all(stores2.map(async (store) => store.set(key, value, ttl ?? options?.ttl)));
192
+ Promise.all(stores2.map(async (store) => store.set(key, value, ttl2 ?? options?.ttl)));
163
193
  eventEmitter.emit("set", { key, value });
164
194
  return value;
165
195
  }
166
- await Promise.all(stores2.map(async (store) => store.set(key, value, ttl ?? options?.ttl)));
196
+ await Promise.all(stores2.map(async (store) => store.set(key, value, ttl2 ?? options?.ttl)));
167
197
  eventEmitter.emit("set", { key, value });
168
198
  return value;
169
199
  } catch (error) {
@@ -172,7 +202,7 @@ var createCache = (options) => {
172
202
  }
173
203
  };
174
204
  const mset = async (stores2, list) => {
175
- const items = list.map(({ key, value, ttl }) => ({ key, value, ttl }));
205
+ const items = list.map(({ key, value, ttl: ttl2 }) => ({ key, value, ttl: ttl2 }));
176
206
  try {
177
207
  const promises = [];
178
208
  for (const item of list) {
@@ -240,7 +270,7 @@ var createCache = (options) => {
240
270
  return Promise.reject(error);
241
271
  }
242
272
  };
243
- const wrap = async (key, fnc, ttl, refreshThreshold) => coalesceAsync(key, async () => {
273
+ const wrap = async (key, fnc, ttl2, refreshThreshold) => coalesceAsync(key, async () => {
244
274
  let value;
245
275
  let i = 0;
246
276
  let remainingTtl;
@@ -259,15 +289,15 @@ var createCache = (options) => {
259
289
  }
260
290
  if (value === void 0) {
261
291
  const result = await fnc();
262
- await set(stores, key, result, runIfFn(ttl, result) ?? options?.ttl);
292
+ await set(stores, key, result, runIfFn(ttl2, result) ?? options?.ttl);
263
293
  return result;
264
294
  }
265
- const ms = runIfFn(ttl, value) ?? options?.ttl;
295
+ const ms = runIfFn(ttl2, value) ?? options?.ttl;
266
296
  const shouldRefresh = lt(remainingTtl, refreshThreshold ?? options?.refreshThreshold);
267
297
  if (shouldRefresh) {
268
298
  coalesceAsync(`+++${key}`, fnc).then(async (result) => {
269
299
  try {
270
- await set(stores.slice(0, i + 1), key, result, ms);
300
+ await set(options?.refreshAllStores ? stores : stores.slice(0, i + 1), key, result, ms);
271
301
  eventEmitter.emit("refresh", { key, value: result });
272
302
  } catch (error) {
273
303
  eventEmitter.emit("refresh", { key, value, error });
@@ -293,7 +323,8 @@ var createCache = (options) => {
293
323
  return {
294
324
  get,
295
325
  mget,
296
- set: async (key, value, ttl) => set(stores, key, value, ttl),
326
+ ttl,
327
+ set: async (key, value, ttl2) => set(stores, key, value, ttl2),
297
328
  mset: async (list) => mset(stores, list),
298
329
  del,
299
330
  mdel,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cache-manager",
3
- "version": "6.2.0",
3
+ "version": "6.3.1",
4
4
  "description": "Cache Manager for Node.js",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -58,12 +58,12 @@
58
58
  "@types/node": "^22.8.1",
59
59
  "@vitest/coverage-v8": "^2.1.3",
60
60
  "cache-manager-redis-yet": "^5.1.5",
61
- "cacheable": "^1.8.3",
62
61
  "rimraf": "^6.0.1",
63
62
  "tsup": "^8.3.5",
64
63
  "typescript": "^5.6.3",
65
64
  "vitest": "^2.1.3",
66
- "xo": "^0.59.3"
65
+ "xo": "^0.59.3",
66
+ "cacheable": "^1.8.5"
67
67
  },
68
68
  "xo": {
69
69
  "rules": {
@@ -75,6 +75,7 @@
75
75
  "clean": "rimraf ./dist ./coverage ./node_modules",
76
76
  "test": "xo --fix && vitest run --coverage",
77
77
  "test:ci": "xo && vitest run",
78
- "build": "rimraf ./dist && tsup src/index.ts --format cjs,esm --dts --clean"
78
+ "build": "rimraf ./dist && tsup src/index.ts --format cjs,esm --dts --clean",
79
+ "prepublish": "pnpm run build"
79
80
  }
80
81
  }