Browse Source

perf(core): Use per-request caching for hot ProductVariant paths

Relates to #988
Michael Bromley 4 years ago
parent
commit
214b86b477
1 changed files with 7 additions and 3 deletions
  1. 7 3
      packages/core/src/service/services/product-variant.service.ts

+ 7 - 3
packages/core/src/service/services/product-variant.service.ts

@@ -274,8 +274,12 @@ export class ProductVariantService {
      * for purchase by Customers.
      */
     async getSaleableStockLevel(ctx: RequestContext, variant: ProductVariant): Promise<number> {
-        // TODO: Use caching (RequestContextCacheService) to reduce DB calls
-        const { outOfStockThreshold, trackInventory } = await this.globalSettingsService.getSettings(ctx);
+        const { outOfStockThreshold, trackInventory } = await this.requestCache.get(
+            ctx,
+            'globalSettings',
+            () => this.globalSettingsService.getSettings(ctx),
+        );
+
         const inventoryNotTracked =
             variant.trackInventory === GlobalFlag.FALSE ||
             (variant.trackInventory === GlobalFlag.INHERIT && trackInventory === false);
@@ -581,7 +585,7 @@ export class ProductVariantService {
             });
         }
         const { taxZoneStrategy } = this.configService.taxOptions;
-        const zones = this.zoneService.findAll(ctx);
+        const zones = this.requestCache.get(ctx, 'allZones', () => this.zoneService.findAll(ctx));
         const activeTaxZone = await this.requestCache.get(ctx, 'activeTaxZone', () =>
             taxZoneStrategy.determineTaxZone(ctx, zones, ctx.channel, order),
         );