فهرست منبع

feat(core): Make entity cache ttl values configurable

Relates to #988
Michael Bromley 4 سال پیش
والد
کامیت
a05e7ab214

+ 5 - 0
packages/core/src/config/config.service.ts

@@ -11,6 +11,7 @@ import {
     AssetOptions,
     AuthOptions,
     CatalogOptions,
+    EntityOptions,
     ImportExportOptions,
     JobQueueOptions,
     OrderOptions,
@@ -54,6 +55,10 @@ export class ConfigService implements VendureConfig {
         return this.activeConfig.defaultLanguageCode;
     }
 
+    get entityOptions(): Required<EntityOptions> {
+        return this.activeConfig.entityOptions;
+    }
+
     get entityIdStrategy(): EntityIdStrategy<any> {
         return this.activeConfig.entityIdStrategy;
     }

+ 4 - 0
packages/core/src/config/default-config.ts

@@ -102,6 +102,10 @@ export const defaultConfig: RuntimeVendureConfig = {
         timezone: 'Z',
         type: 'mysql',
     },
+    entityOptions: {
+        channelCacheTtl: 30000,
+        zoneCacheTtl: 30000,
+    },
     promotionOptions: {
         promotionConditions: defaultPromotionConditions,
         promotionActions: defaultPromotionActions,

+ 33 - 0
packages/core/src/config/vendure-config.ts

@@ -745,6 +745,37 @@ export interface JobQueueOptions {
     activeQueues?: string[];
 }
 
+/**
+ * @description
+ * Options relating to the internal handling of entities.
+ *
+ * @since 1.3.0
+ */
+export interface EntityOptions {
+    /**
+     * @description
+     * Channels get cached in-memory as they are accessed very frequently. This
+     * setting determines how long the cache lives (in ms) until it is considered stale and
+     * refreshed. For multi-instance deployments (e.g. serverless, load-balanced), a
+     * smaller value here will prevent data inconsistencies between instances.
+     *
+     * @since 1.3.0
+     * @default 30000
+     */
+    channelCacheTtl?: number;
+    /**
+     * @description
+     * Zones get cached in-memory as they are accessed very frequently. This
+     * setting determines how long the cache lives (in ms) until it is considered stale and
+     * refreshed. For multi-instance deployments (e.g. serverless, load-balanced), a
+     * smaller value here will prevent data inconsistencies between instances.
+     *
+     * @since 1.3.0
+     * @default 30000
+     */
+    zoneCacheTtl?: number;
+}
+
 /**
  * @description
  * All possible configuration options are defined by the
@@ -813,6 +844,7 @@ export interface VendureConfig {
      * @default AutoIncrementIdStrategy
      */
     entityIdStrategy?: EntityIdStrategy<any>;
+    entityOptions?: EntityOptions;
     /**
      * @description
      * Configuration settings for data import and export.
@@ -879,6 +911,7 @@ export interface RuntimeVendureConfig extends Required<VendureConfig> {
     authOptions: Required<AuthOptions>;
     catalogOptions: Required<CatalogOptions>;
     customFields: Required<CustomFields>;
+    entityOptions: Required<EntityOptions>;
     importExportOptions: Required<ImportExportOptions>;
     jobQueueOptions: Required<JobQueueOptions>;
     orderOptions: Required<OrderOptions>;

+ 1 - 1
packages/core/src/service/services/channel.service.ts

@@ -50,7 +50,7 @@ export class ChannelService {
         await this.ensureDefaultChannelExists();
         this.allChannels = await createSelfRefreshingCache({
             name: 'ChannelService.allChannels',
-            ttl: 10000,
+            ttl: this.configService.entityOptions.channelCacheTtl,
             refreshFn: () => this.findAll(RequestContext.empty()),
         });
     }

+ 3 - 2
packages/core/src/service/services/zone.service.ts

@@ -13,6 +13,7 @@ import { unique } from '@vendure/common/lib/unique';
 import { RequestContext } from '../../api/common/request-context';
 import { createSelfRefreshingCache, SelfRefreshingCache } from '../../common/self-refreshing-cache';
 import { assertFound } from '../../common/utils';
+import { ConfigService } from '../../config/config.service';
 import { Channel, TaxRate } from '../../entity';
 import { Country } from '../../entity/country/country.entity';
 import { Zone } from '../../entity/zone/zone.entity';
@@ -26,12 +27,12 @@ export class ZoneService {
      * We cache all Zones to avoid hitting the DB many times per request.
      */
     private zones: SelfRefreshingCache<Zone[]>;
-    constructor(private connection: TransactionalConnection) {}
+    constructor(private connection: TransactionalConnection, private configService: ConfigService) {}
 
     async initZones() {
         this.zones = await createSelfRefreshingCache({
             name: 'ZoneService.zones',
-            ttl: 10000,
+            ttl: this.configService.entityOptions.zoneCacheTtl,
             refreshFn: () =>
                 this.connection.getRepository(Zone).find({
                     relations: ['members'],