Skip to content

Commit 3b6658f

Browse files
committed
fix: publish to bus only if l2 write is successful
1 parent acba770 commit 3b6658f

File tree

5 files changed

+51
-48
lines changed

5 files changed

+51
-48
lines changed

packages/bentocache/src/cache/cache_stack.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,19 @@ export class CacheStack extends BaseDriver {
142142
/**
143143
* Store the serialized value in the remote cache
144144
*/
145+
let l2Success = false
145146
if (this.l2 && options.skipL2Write !== true) {
146147
const l2Item = this.options.serializeL1 ? l1Item : this.options.serializer.serialize(rawItem)
147-
await this.l2?.set(key, l2Item as any, options)
148+
l2Success = await this.l2?.set(key, l2Item as any, options)
149+
}
150+
151+
/**
152+
* Publish only if the remote cache write was successful.
153+
*/
154+
if ((this.l2 && l2Success) || !this.l2) {
155+
await this.publish({ type: CacheBusMessageType.Set, keys: [key] }, options)
148156
}
149157

150-
await this.publish({ type: CacheBusMessageType.Set, keys: [key] }, options)
151158
this.emit(cacheEvents.written(key, value, this.name))
152159
return true
153160
}

packages/bentocache/tests/cache/two_tier.spec.ts

+29
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { sleep } from '@julr/utils/misc'
33
import { MemoryTransport } from '@boringnode/bus/transports/memory'
44

55
import { RedisDriver } from '../../src/drivers/redis.js'
6+
import { ChaosBus } from '../helpers/chaos/chaos_bus.js'
67
import { NullDriver } from '../helpers/null/null_driver.js'
78
import { ChaosCache } from '../helpers/chaos/chaos_cache.js'
89
import { CacheFactory } from '../../factories/cache_factory.js'
@@ -773,4 +774,32 @@ test.group('Cache', () => {
773774

774775
assert.isUndefined(r1)
775776
})
777+
778+
test('should not publish to bus if l2 could not be written', async ({ assert }) => {
779+
const redis1 = new ChaosCache(new RedisDriver({ connection: REDIS_CREDENTIALS }))
780+
const redis2 = new ChaosCache(new RedisDriver({ connection: REDIS_CREDENTIALS }))
781+
const bus1 = new ChaosBus(new MemoryTransport())
782+
const bus2 = new ChaosBus(new MemoryTransport())
783+
784+
const [cache1] = new CacheFactory()
785+
.merge({ l2Driver: redis1, busDriver: bus1 })
786+
.withL1L2Config()
787+
.create()
788+
const [cache2] = new CacheFactory()
789+
.merge({ l2Driver: redis2, busDriver: bus2 })
790+
.withL1L2Config()
791+
.create()
792+
793+
await cache1.set({ key: 'foo', value: 'bar' })
794+
const r1 = await cache2.get({ key: 'foo' })
795+
796+
redis1.alwaysThrow()
797+
redis2.alwaysThrow()
798+
799+
await cache1.set({ key: 'foo', value: 'baz' })
800+
const r2 = await cache2.get({ key: 'foo' })
801+
802+
assert.deepEqual(r1, 'bar')
803+
assert.deepEqual(r2, 'bar')
804+
})
776805
})

pnpm-lock.yaml

+2-37
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

simulator/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"@julr/utils": "^1.8.0",
4545
"@primevue/themes": "^4.2.5",
4646
"@vinejs/vine": "^3.0.0",
47-
"bentocache": "^1.2.0",
47+
"bentocache": "workspace:*",
4848
"edge.js": "^6.2.1",
4949
"primeicons": "^7.0.0",
5050
"primevue": "^4.2.5",

simulator/start/routes.ts

+10-8
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,19 @@ for (let i = 0; i < kNodesCount; i++) {
5959
}
6060

6161
router.get('/', async ({ inertia }) => {
62-
const results = nodes.entries().map(async ([key, cache]) => {
63-
return {
64-
name: key,
65-
result: await cache.bento.get({ key: 'value', defaultValue: 0 }),
66-
busId: cache.bus.id,
67-
}
68-
})
62+
const results = await Promise.all(
63+
nodes.entries().map(async ([key, cache]) => {
64+
return {
65+
name: key,
66+
result: await cache.bento.get({ key: 'value', defaultValue: 0 }),
67+
busId: cache.bus.id,
68+
}
69+
}),
70+
)
6971

7072
return inertia.render('home', {
7173
correctValue: await trueCache.get({ key: 'value', defaultValue: 0 }),
72-
caches: await Promise.all(results),
74+
caches: results,
7375
state,
7476
sentMessages: [...nodes.entries()]
7577
.map(([key, cache]) =>

0 commit comments

Comments
 (0)