Skip to content

Commit

Permalink
feat(core): Make entity cache ttl values configurable
Browse files Browse the repository at this point in the history
Relates to #988
  • Loading branch information
michaelbromley committed Sep 28, 2021
1 parent e58f772 commit a05e7ab
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
5 changes: 5 additions & 0 deletions packages/core/src/config/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
AssetOptions,
AuthOptions,
CatalogOptions,
EntityOptions,
ImportExportOptions,
JobQueueOptions,
OrderOptions,
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/config/default-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ export const defaultConfig: RuntimeVendureConfig = {
timezone: 'Z',
type: 'mysql',
},
entityOptions: {
channelCacheTtl: 30000,
zoneCacheTtl: 30000,
},
promotionOptions: {
promotionConditions: defaultPromotionConditions,
promotionActions: defaultPromotionActions,
Expand Down
33 changes: 33 additions & 0 deletions packages/core/src/config/vendure-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -813,6 +844,7 @@ export interface VendureConfig {
* @default AutoIncrementIdStrategy
*/
entityIdStrategy?: EntityIdStrategy<any>;
entityOptions?: EntityOptions;
/**
* @description
* Configuration settings for data import and export.
Expand Down Expand Up @@ -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>;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/service/services/channel.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
});
}
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/service/services/zone.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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'],
Expand Down

0 comments on commit a05e7ab

Please sign in to comment.