Skip to content

Commit 1ed4965

Browse files
committed
perf: only serialize when l2 is present
1 parent 95f24a5 commit 1ed4965

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

packages/bentocache/src/cache/stack/cache_stack.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,22 @@ export class CacheStack extends BaseDriver {
117117
value,
118118
logicalExpiration: options.logicalTtlFromNow(),
119119
}
120-
const item = this.options.serializer.serialize(rawItem)
121120

122-
this.l1?.set(key, this.options.serializeL1 ? item : rawItem, options)
123-
await this.l2?.set(key, item, options)
124-
await this.publish({ type: CacheBusMessageType.Set, keys: [key] })
121+
/**
122+
* Store raw or serialized value in the local cache based on the serializeL1 option
123+
*/
124+
const l1Item = this.options.serializeL1 ? this.options.serializer.serialize(rawItem) : rawItem
125+
this.l1?.set(key, l1Item, options)
126+
127+
/**
128+
* Store the serialized value in the remote cache
129+
*/
130+
if (this.l2) {
131+
const l2Item = this.options.serializeL1 ? l1Item : this.options.serializer.serialize(rawItem)
132+
await this.l2?.set(key, l2Item as any, options)
133+
}
125134

135+
await this.publish({ type: CacheBusMessageType.Set, keys: [key] })
126136
this.emit(cacheEvents.written(key, value, this.name))
127137
return true
128138
}

0 commit comments

Comments
 (0)