feat: optimize cache update strategy

This commit is contained in:
codytseng
2025-11-09 16:09:11 +08:00
parent c11fc56209
commit fe9b55dcfd
2 changed files with 12 additions and 4 deletions

View File

@@ -1198,9 +1198,12 @@ class ClientService extends EventTarget {
private async fetchReplaceableEventsFromBigRelays(pubkeys: string[], kind: number) {
const events = await indexedDb.getManyReplaceableEvents(pubkeys, kind)
const nonExistingPubkeyIndexMap = new Map<string, number>()
const existingPubkeys: string[] = []
pubkeys.forEach((pubkey, i) => {
if (events[i] === undefined) {
nonExistingPubkeyIndexMap.set(pubkey, i)
} else {
existingPubkeys.push(pubkey)
}
})
const newEvents = await this.replaceableEventFromBigRelaysDataloader.loadMany(
@@ -1215,6 +1218,10 @@ class ClientService extends EventTarget {
}
})
this.replaceableEventFromBigRelaysDataloader.loadMany(
existingPubkeys.map((pubkey) => ({ pubkey, kind }))
) // update cache in background
return events
}
@@ -1306,6 +1313,7 @@ class ClientService extends EventTarget {
private async fetchReplaceableEvent(pubkey: string, kind: number, d?: string) {
const storedEvent = await indexedDb.getReplaceableEvent(pubkey, kind, d)
if (storedEvent !== undefined) {
this.replaceableEventDataLoader.load({ pubkey, kind, d }) // update cache in background
return storedEvent
}