Skip to content

Commit c8413d6

Browse files
committed
cache-manager: wrap() method - support refreshThreshold argument as number or Function - (value:T) => number. similar to how ttl is supported
1 parent 9eae729 commit c8413d6

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

packages/cache-manager/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export type Cache = {
4141
key: string,
4242
fnc: () => T | Promise<T>,
4343
ttl?: number | ((value: T) => number),
44-
refreshThreshold?: number
44+
refreshThreshold?: number | ((value: T) => number)
4545
) => Promise<T>;
4646
on: <E extends keyof Events>(
4747
event: E,
@@ -252,7 +252,7 @@ export const createCache = (options?: CreateCacheOptions): Cache => {
252252
key: string,
253253
fnc: () => T | Promise<T>,
254254
ttl?: number | ((value: T) => number),
255-
refreshThreshold?: number,
255+
refreshThreshold?: number | ((value: T) => number),
256256
): Promise<T> => coalesceAsync(`${_cacheId}::${key}`, async () => {
257257
let value: T | undefined;
258258
let i = 0;
@@ -281,7 +281,7 @@ export const createCache = (options?: CreateCacheOptions): Cache => {
281281
return result;
282282
}
283283

284-
const shouldRefresh = lt(remainingTtl, refreshThreshold ?? options?.refreshThreshold);
284+
const shouldRefresh = lt(remainingTtl, runIfFn(refreshThreshold, value) ?? options?.refreshThreshold);
285285

286286
if (shouldRefresh) {
287287
coalesceAsync(`+++${_cacheId}__${key}`, fnc)

packages/cache-manager/test/wrap.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,25 @@ describe('wrap', () => {
8585
expect(await cache.wrap(data.key, async () => 5, undefined, 500)).toEqual(4);
8686
});
8787

88+
it('should allow refreshThreshold function on wrap function', async () => {
89+
const config = {ttl: (v:number) => v * 1000, refreshThreshold: (v:number) => v * 500 };
90+
91+
// 1st call should be cached
92+
expect(await cache.wrap(data.key, async () => 1, config.ttl, config.refreshThreshold)).toEqual(1);
93+
await sleep(501);
94+
// Background refresh, but stale value returned
95+
expect(await cache.wrap(data.key, async () => 2, config.ttl, config.refreshThreshold)).toEqual(1);
96+
// New value in cache
97+
expect(await cache.wrap(data.key, async () => 2, config.ttl, config.refreshThreshold)).toEqual(2);
98+
await sleep(1001);
99+
// No background refresh with the new override params
100+
expect(await cache.wrap(data.key, async () => 3, undefined, 500)).toEqual(2);
101+
await sleep(500);
102+
// Background refresh, but stale value returned
103+
expect(await cache.wrap(data.key, async () => 4, undefined, 500)).toEqual(2);
104+
expect(await cache.wrap(data.key, async () => 5, undefined, 500)).toEqual(4);
105+
});
106+
88107
it('should support nested calls of other caches - no mutual state', async () => {
89108
const getValueA = vi.fn(() => 'A');
90109
const getValueB = vi.fn(() => 'B');

0 commit comments

Comments
 (0)