Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/graced-entry-background-factory.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'bentocache': patch
---

Pass `gracedEntry` to factories that refresh a stale value in the background (`timeout: 0`, the default). It was always `undefined` there, so a background refresh couldn't tell it had a stale value to fall back on.
2 changes: 1 addition & 1 deletion packages/bentocache/src/cache/factory_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export class FactoryRunner {
* And immediately return the fallback value
*/
if (options.shouldSwr(hasGracedValue)) {
this.#runFactory({ key, factory, options, lockReleaser, isBackground: true })
this.#runFactory({ key, factory, options, lockReleaser, isBackground: true, gracedValue })
throw new errors.E_FACTORY_SOFT_TIMEOUT(key)
}

Expand Down
29 changes: 29 additions & 0 deletions packages/bentocache/tests/cache/factory_context.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,33 @@ test.group('Factory Context', () => {

assert.deepEqual(r1, 'bar')
})

test('can access graced entry when refreshing in the background', async ({ assert }) => {
const { cache } = new CacheFactory()
.withL1L2Config()
.merge({ grace: '2m', timeout: 0 })
.create()

await cache.getOrSet({
key: 'foo',
ttl: 100,
factory: () => 'bar',
})

await sleep(100)

let gracedEntry: { value: any } | undefined
const r1 = await cache.getOrSet({
key: 'foo',
factory: (ctx) => {
gracedEntry = ctx.gracedEntry
return 'baz'
},
})

await sleep(50)

assert.deepEqual(r1, 'bar')
assert.deepEqual(gracedEntry, { value: 'bar' })
})
})