Skip to content

Commit

Permalink
on wrap function - re-evaluate ttl function on fresh value when trigg…
Browse files Browse the repository at this point in the history
…ered by refreshThreshold
  • Loading branch information
yaron-yamin-glbe committed Jan 2, 2025
1 parent 5ec1858 commit 0e44b9b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
8 changes: 4 additions & 4 deletions packages/cache-manager/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ export const createCache = (options?: CreateCacheOptions): Cache => {
let value: T | undefined;
let i = 0;
let remainingTtl: number | undefined;
const resolveTtl = (result: T) => runIfFn(ttl, result) ?? options?.ttl;

for (; i < stores.length; i++) {
try {
Expand All @@ -273,18 +274,17 @@ export const createCache = (options?: CreateCacheOptions): Cache => {

if (value === undefined) {
const result = await fnc();
await set(stores, key, result, runIfFn(ttl, result) ?? options?.ttl);
await set(stores, key, result, resolveTtl(result));
return result;
}

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

if (shouldRefresh) {
coalesceAsync(`+++${key}`, fnc)
.then(async result => {
try {
await set(options?.refreshAllStores ? stores : stores.slice(0, i + 1), key, result, ms);
await set(options?.refreshAllStores ? stores : stores.slice(0, i + 1), key, result, resolveTtl(result));
eventEmitter.emit('refresh', {key, value: result});
} catch (error) {
eventEmitter.emit('refresh', {key, value, error});
Expand All @@ -297,7 +297,7 @@ export const createCache = (options?: CreateCacheOptions): Cache => {
}

if (!shouldRefresh && i > 0) {
await set(stores.slice(0, i), key, value, ms);
await set(stores.slice(0, i), key, value, resolveTtl(value));
}

return value;
Expand Down
12 changes: 12 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,18 @@ describe('wrap', () => {
expect(await cache.wrap(data.key, async () => 5, undefined, 500)).toEqual(4);
});

it('should re-evaluate ttl function on fresh value when triggered by refreshThreshold', async () => {
const config = {ttl: 2000, refreshThreshold: 1000};
const getTtlFunction = vi.fn(() => config.ttl);
let value = 10;

expect(await cache.wrap(data.key, async () => ++value, getTtlFunction, config.refreshThreshold)).toEqual(11); // 1st call should be cached
expect(getTtlFunction).toHaveBeenNthCalledWith(1, 11); // Ttl func called 1st time when cache empty
await sleep(1001);
expect(await cache.wrap(data.key, async () => ++value, getTtlFunction, config.refreshThreshold)).toEqual(11); // Trigger background refresh. stale value returned
expect(getTtlFunction).toHaveBeenNthCalledWith(2, 12); // Ttl func called 2nd time triggered by refreshThreshold on fresh item
});

it('store get failed', async () => {
const getValue = vi.fn(() => data.value);
keyv.get = () => {
Expand Down

0 comments on commit 0e44b9b

Please sign in to comment.