-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(elasticsearch-plugin): Optimize indexing using RequestContextCache
This perf optimization uses the RequestContextCacheService to cache the results of DB calls which get performed many times during indexing, i.e. at least once for every ProductVariant being indexed. In testing this cut down the time to index ~10k variants by around 20% (3 mins to 2.5 mins). This commit also adjusts some logging to `debug` from `verbose` as it was too noisy even for verbose.
- Loading branch information
1 parent
479505e
commit 75da3b3
Showing
2 changed files
with
104 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
packages/elasticsearch-plugin/src/indexing/mutable-request-context.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { Channel, ID, RequestContext, SerializedRequestContext } from '@vendure/core'; | ||
|
||
/** | ||
* @description | ||
* This is used during search index creation to allow us to use a single | ||
* RequestContext, but mutate the Channel. In this way, we can take | ||
* full advantage of the RequestContextCacheService, and _massively_ cut | ||
* down on the number of DB calls being made during indexing. | ||
*/ | ||
export class MutableRequestContext extends RequestContext { | ||
constructor(options: ConstructorParameters<typeof RequestContext>[0]) { | ||
super(options); | ||
} | ||
private mutatedChannel: Channel | undefined; | ||
|
||
setChannel(channel: Channel) { | ||
this.mutatedChannel = channel; | ||
} | ||
|
||
get channel(): Channel { | ||
return this.mutatedChannel ?? super.channel; | ||
} | ||
|
||
get channelId(): ID { | ||
return this.mutatedChannel?.id ?? super.channelId; | ||
} | ||
|
||
static deserialize(ctxObject: SerializedRequestContext): MutableRequestContext { | ||
return new MutableRequestContext({ | ||
req: ctxObject._req as any, | ||
apiType: ctxObject._apiType, | ||
channel: new Channel(ctxObject._channel), | ||
session: { | ||
...ctxObject._session, | ||
expires: ctxObject._session?.expires && new Date(ctxObject._session.expires), | ||
}, | ||
languageCode: ctxObject._languageCode, | ||
isAuthorized: ctxObject._isAuthorized, | ||
authorizedAsOwnerOnly: ctxObject._authorizedAsOwnerOnly, | ||
}); | ||
} | ||
} |