cache-manager 6.4.0 → 6.4.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 +10 -0
- package/dist/index.cjs +13 -4
- package/dist/index.d.cts +12 -3
- package/dist/index.d.ts +12 -3
- package/dist/index.js +13 -4
- package/package.json +10 -10
package/README.md
CHANGED
|
@@ -322,10 +322,16 @@ See unit tests in [`test/clear.test.ts`](./test/clear.test.ts) for more informat
|
|
|
322
322
|
## wrap
|
|
323
323
|
`wrap(key, fn: async () => value, [ttl], [refreshThreshold]): Promise<value>`
|
|
324
324
|
|
|
325
|
+
Alternatively, with optional parameters as options object supporting a `raw` parameter:
|
|
326
|
+
|
|
327
|
+
`wrap(key, fn: async () => value, { ttl?: number, refreshThreshold?: number, raw?: true }): Promise<value>`
|
|
328
|
+
|
|
325
329
|
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
330
|
|
|
327
331
|
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
332
|
|
|
333
|
+
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] }`.
|
|
334
|
+
|
|
329
335
|
```typescript
|
|
330
336
|
await cache.wrap('key', () => 1, 5000, 3000)
|
|
331
337
|
// call function then save the result to cache
|
|
@@ -335,6 +341,10 @@ await cache.wrap('key', () => 2, 5000, 3000)
|
|
|
335
341
|
// return data from cache, function will not be called again
|
|
336
342
|
// => 1
|
|
337
343
|
|
|
344
|
+
await cache.wrap('key', () => 2, { ttl: 5000, refreshThreshold: 3000, raw: true })
|
|
345
|
+
// returns raw data including expiration timestamp
|
|
346
|
+
// => { value: 1, expires: [timestamp] }
|
|
347
|
+
|
|
338
348
|
// wait 3 seconds
|
|
339
349
|
await sleep(3000)
|
|
340
350
|
|
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,
|
|
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
|
-
|
|
330
|
-
|
|
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<
|
|
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<
|
|
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,
|
|
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
|
-
|
|
295
|
-
|
|
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.
|
|
3
|
+
"version": "6.4.1",
|
|
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.
|
|
53
|
+
"keyv": "^5.3.1"
|
|
54
54
|
},
|
|
55
55
|
"devDependencies": {
|
|
56
|
-
"@faker-js/faker": "^9.
|
|
57
|
-
"@keyv/redis": "^4.
|
|
58
|
-
"@types/node": "^22.
|
|
59
|
-
"@vitest/coverage-v8": "^3.0.
|
|
56
|
+
"@faker-js/faker": "^9.5.1",
|
|
57
|
+
"@keyv/redis": "^4.3.1",
|
|
58
|
+
"@types/node": "^22.13.9",
|
|
59
|
+
"@vitest/coverage-v8": "^3.0.7",
|
|
60
60
|
"cache-manager-redis-yet": "^5.1.5",
|
|
61
61
|
"rimraf": "^6.0.1",
|
|
62
|
-
"tsup": "^8.
|
|
63
|
-
"typescript": "^5.
|
|
64
|
-
"vitest": "^3.0.
|
|
62
|
+
"tsup": "^8.4.0",
|
|
63
|
+
"typescript": "^5.8.2",
|
|
64
|
+
"vitest": "^3.0.7",
|
|
65
65
|
"xo": "^0.60.0",
|
|
66
|
-
"cacheable": "^1.8.
|
|
66
|
+
"cacheable": "^1.8.9"
|
|
67
67
|
},
|
|
68
68
|
"xo": {
|
|
69
69
|
"rules": {
|