Skip to content

Commit

Permalink
optimize: added namespace cache to CacheStack to avoid unnecessary cr…
Browse files Browse the repository at this point in the history
…eation of caches and buses
  • Loading branch information
gkachru committed Oct 8, 2024
1 parent 53c3681 commit 7747149
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions packages/bentocache/src/cache/stack/cache_stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ export class CacheStack {
l1?: LocalCache
l2?: RemoteCache
bus?: Bus
#busDriver?: BusDriver
#busOptions?: BusOptions
defaultOptions: CacheEntryOptions
logger: Logger
#busDriver?: BusDriver
#busOptions?: BusOptions
#namespaceCache: Map<string, CacheStack> = new Map()

constructor(
public name: string,
Expand Down Expand Up @@ -59,12 +60,19 @@ export class CacheStack {
}

namespace(namespace: string) {
return new CacheStack(this.name, this.options, {
l1Driver: this.l1?.namespace(namespace),
l2Driver: this.l2?.namespace(namespace),
busDriver: this.#busDriver,
busOptions: { ...this.#busOptions, prefix: this.bus?.namespace(namespace) },
})
if (!this.#namespaceCache.has(namespace)) {
this.#namespaceCache.set(
namespace,
new CacheStack(this.name, this.options, {
l1Driver: this.l1?.namespace(namespace),
l2Driver: this.l2?.namespace(namespace),
busDriver: this.#busDriver,
busOptions: { ...this.#busOptions, prefix: this.bus?.namespace(namespace) },
}),
)
}

return this.#namespaceCache.get(namespace)
}

emit(event: CacheEvent) {
Expand Down

0 comments on commit 7747149

Please sign in to comment.