Browse Source

Improve performance by caching properties in memory

pull/7484/head
Thomas Kaul 1 month ago
parent
commit
2639293410
  1. 4
      apps/api/src/app/subscription/subscription.controller.ts
  2. 6
      apps/api/src/services/benchmark/benchmark.service.ts
  3. 31
      apps/api/src/services/property/property.service.ts

4
apps/api/src/app/subscription/subscription.controller.ts

@ -54,7 +54,9 @@ export class SubscriptionController {
} }
let coupons = let coupons =
(await this.propertyService.getByKey<Coupon[]>(PROPERTY_COUPONS)) ?? []; (await this.propertyService.getByKey<Coupon[]>(PROPERTY_COUPONS, {
skipCache: true
})) ?? [];
const coupon = coupons.find((currentCoupon) => { const coupon = coupons.find((currentCoupon) => {
return currentCoupon.code === couponCode; return currentCoupon.code === couponCode;

6
apps/api/src/services/benchmark/benchmark.service.ts

@ -159,7 +159,8 @@ export class BenchmarkService {
let benchmarks = let benchmarks =
(await this.propertyService.getByKey<BenchmarkProperty[]>( (await this.propertyService.getByKey<BenchmarkProperty[]>(
PROPERTY_BENCHMARKS PROPERTY_BENCHMARKS,
{ skipCache: true }
)) ?? []; )) ?? [];
benchmarks.push({ symbolProfileId: assetProfile.id }); benchmarks.push({ symbolProfileId: assetProfile.id });
@ -196,7 +197,8 @@ export class BenchmarkService {
let benchmarks = let benchmarks =
(await this.propertyService.getByKey<BenchmarkProperty[]>( (await this.propertyService.getByKey<BenchmarkProperty[]>(
PROPERTY_BENCHMARKS PROPERTY_BENCHMARKS,
{ skipCache: true }
)) ?? []; )) ?? [];
benchmarks = benchmarks.filter(({ symbolProfileId }) => { benchmarks = benchmarks.filter(({ symbolProfileId }) => {

31
apps/api/src/services/property/property.service.ts

@ -31,14 +31,14 @@ export class PropertyService {
return property; return property;
} }
public async get() { public async get({ skipCache = false } = {}) {
const response: { const response: {
[key: string]: PropertyValue; [key: string]: PropertyValue;
} = { } = {
[PROPERTY_CURRENCIES]: [] [PROPERTY_CURRENCIES]: []
}; };
const properties = await this.getProperties(); const properties = await this.getProperties({ skipCache });
for (const property of properties) { for (const property of properties) {
let value = property.value; let value = property.value;
@ -53,8 +53,11 @@ export class PropertyService {
return response; return response;
} }
public async getByKey<TValue extends PropertyValue>(aKey: PropertyKey) { public async getByKey<TValue extends PropertyValue>(
const properties = await this.get(); aKey: PropertyKey,
{ skipCache = false } = {}
) {
const properties = await this.get({ skipCache });
return properties[aKey] as TValue; return properties[aKey] as TValue;
} }
@ -78,9 +81,14 @@ export class PropertyService {
/** /**
* Returns the properties from the in-memory cache, falling back to the * Returns the properties from the in-memory cache, falling back to the
* database * database. Callers which write back a modified property must set
* skipCache to avoid basing the write on a stale read.
*/ */
private async getProperties() { private async getProperties({ skipCache = false } = {}) {
if (skipCache) {
return this.prismaService.property.findMany();
}
if ( if (
this.cachedProperties && this.cachedProperties &&
isBefore(new Date(), this.cachedPropertiesExpiresAt) isBefore(new Date(), this.cachedPropertiesExpiresAt)
@ -88,14 +96,15 @@ export class PropertyService {
return this.cachedProperties; return this.cachedProperties;
} }
this.cachedProperties = this.prismaService.property const properties = this.prismaService.property.findMany().catch((error) => {
.findMany() if (this.cachedProperties === properties) {
.catch((error) => {
this.invalidateCache(); this.invalidateCache();
}
throw error; throw error;
}); });
this.cachedProperties = properties;
this.cachedPropertiesExpiresAt = addMilliseconds( this.cachedPropertiesExpiresAt = addMilliseconds(
new Date(), new Date(),
PropertyService.CACHE_TTL PropertyService.CACHE_TTL

Loading…
Cancel
Save