Skip to content

Commit

Permalink
cache-manager: wrap() method - support refreshThreshold argument as n…
Browse files Browse the repository at this point in the history
…umber or Function - (value:T) => number. similar to how ttl is supported
  • Loading branch information
yaronyam committed Jan 12, 2025
1 parent 9eae729 commit c8413d6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/cache-manager/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export type Cache = {
key: string,
fnc: () => T | Promise<T>,
ttl?: number | ((value: T) => number),
refreshThreshold?: number
refreshThreshold?: number | ((value: T) => number)
) => Promise<T>;
on: <E extends keyof Events>(
event: E,
Expand Down Expand Up @@ -252,7 +252,7 @@ export const createCache = (options?: CreateCacheOptions): Cache => {
key: string,
fnc: () => T | Promise<T>,
ttl?: number | ((value: T) => number),
refreshThreshold?: number,
refreshThreshold?: number | ((value: T) => number),
): Promise<T> => coalesceAsync(`${_cacheId}::${key}`, async () => {
let value: T | undefined;
let i = 0;
Expand Down Expand Up @@ -281,7 +281,7 @@ export const createCache = (options?: CreateCacheOptions): Cache => {
return result;
}

const shouldRefresh = lt(remainingTtl, refreshThreshold ?? options?.refreshThreshold);
const shouldRefresh = lt(remainingTtl, runIfFn(refreshThreshold, value) ?? options?.refreshThreshold);

Check failure on line 284 in packages/cache-manager/src/index.ts

View workflow job for this annotation

GitHub Actions / test (20)

Multiple spaces found before '??'.

Check failure on line 284 in packages/cache-manager/src/index.ts

View workflow job for this annotation

GitHub Actions / test (20)

Multiple spaces found before '??'.

Check failure on line 284 in packages/cache-manager/src/index.ts

View workflow job for this annotation

GitHub Actions / test (22)

Multiple spaces found before '??'.

if (shouldRefresh) {
coalesceAsync(`+++${_cacheId}__${key}`, fnc)
Expand Down
19 changes: 19 additions & 0 deletions packages/cache-manager/test/wrap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,25 @@ describe('wrap', () => {
expect(await cache.wrap(data.key, async () => 5, undefined, 500)).toEqual(4);
});

it('should allow refreshThreshold function on wrap function', async () => {
const config = {ttl: (v:number) => v * 1000, refreshThreshold: (v:number) => v * 500 };

Check failure on line 89 in packages/cache-manager/test/wrap.test.ts

View workflow job for this annotation

GitHub Actions / test (20)

Expected a space after the ':'.

Check failure on line 89 in packages/cache-manager/test/wrap.test.ts

View workflow job for this annotation

GitHub Actions / test (20)

Expected a space after the ':'.

Check failure on line 89 in packages/cache-manager/test/wrap.test.ts

View workflow job for this annotation

GitHub Actions / test (20)

There should be no space before '}'.

Check failure on line 89 in packages/cache-manager/test/wrap.test.ts

View workflow job for this annotation

GitHub Actions / test (20)

Expected a space after the ':'.

Check failure on line 89 in packages/cache-manager/test/wrap.test.ts

View workflow job for this annotation

GitHub Actions / test (20)

Expected a space after the ':'.

Check failure on line 89 in packages/cache-manager/test/wrap.test.ts

View workflow job for this annotation

GitHub Actions / test (20)

There should be no space before '}'.

Check failure on line 89 in packages/cache-manager/test/wrap.test.ts

View workflow job for this annotation

GitHub Actions / test (22)

Expected a space after the ':'.

Check failure on line 89 in packages/cache-manager/test/wrap.test.ts

View workflow job for this annotation

GitHub Actions / test (22)

Expected a space after the ':'.

Check failure on line 89 in packages/cache-manager/test/wrap.test.ts

View workflow job for this annotation

GitHub Actions / test (22)

There should be no space before '}'.

// 1st call should be cached
expect(await cache.wrap(data.key, async () => 1, config.ttl, config.refreshThreshold)).toEqual(1);
await sleep(501);
// Background refresh, but stale value returned
expect(await cache.wrap(data.key, async () => 2, config.ttl, config.refreshThreshold)).toEqual(1);
// New value in cache
expect(await cache.wrap(data.key, async () => 2, config.ttl, config.refreshThreshold)).toEqual(2);
await sleep(1001);
// No background refresh with the new override params
expect(await cache.wrap(data.key, async () => 3, undefined, 500)).toEqual(2);
await sleep(500);
// Background refresh, but stale value returned
expect(await cache.wrap(data.key, async () => 4, undefined, 500)).toEqual(2);
expect(await cache.wrap(data.key, async () => 5, undefined, 500)).toEqual(4);
});

it('should support nested calls of other caches - no mutual state', async () => {
const getValueA = vi.fn(() => 'A');
const getValueB = vi.fn(() => 'B');
Expand Down

0 comments on commit c8413d6

Please sign in to comment.