From 58b836787d2347e266e105b0364267aa50c9fbf1 Mon Sep 17 00:00:00 2001 From: Zaur Abdulgalimov Date: Fri, 17 May 2024 11:33:34 +0300 Subject: [PATCH] fix: task 521; long response time of filters --- package-lock.json | 4 +- package.json | 2 +- packages/common/modules/config/load.config.ts | 7 ++++ packages/common/modules/config/types.ts | 5 +++ .../market/src/offers/view-offers.service.ts | 37 ++++++++++++++----- 5 files changed, 43 insertions(+), 12 deletions(-) diff --git a/package-lock.json b/package-lock.json index 89e6a1a..5f50d50 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "unique-marketplace-backend", - "version": "3.0.242", + "version": "3.0.243", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "unique-marketplace-backend", - "version": "3.0.242", + "version": "3.0.243", "license": "GPL-3.0", "dependencies": { "@commitlint/cli": "^17.6.1", diff --git a/package.json b/package.json index c6eb9a1..034374a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "unique-marketplace-backend", - "version": "3.0.242", + "version": "3.0.243", "description": "Backend project for unique marketplace", "author": "Unique Network", "private": true, diff --git a/packages/common/modules/config/load.config.ts b/packages/common/modules/config/load.config.ts index 6eb118b..64d2a70 100644 --- a/packages/common/modules/config/load.config.ts +++ b/packages/common/modules/config/load.config.ts @@ -11,6 +11,12 @@ export function loadFileStorageConfig(): FileStorageConfig { }; } +const createAppCacheConfig = () => { + return { + offersAttributesTtlMs: +process.env.OFFERS_ATTRIBUTES_TTL_MS || 3_600_000, + }; +}; + export const loadConfig = (): Config => ({ environment: process.env.ENVIRONMENT || 'development', port: parseInt(process.env.PORT, 10) || 3000, @@ -30,6 +36,7 @@ export const loadConfig = (): Config => ({ signatureKey: process.env.SIGNATURE_KEY || '', // Sign and Verify key (sign the following data) cache: createCacheConfig(process.env), + appCache: createAppCacheConfig(), releaseVersion: process.env.npm_package_version, diff --git a/packages/common/modules/config/types.ts b/packages/common/modules/config/types.ts index 2650122..cfddd80 100644 --- a/packages/common/modules/config/types.ts +++ b/packages/common/modules/config/types.ts @@ -8,6 +8,10 @@ export interface FileStorageConfig { secretKey: string; } +export type AppCacheConfig = { + offersAttributesTtlMs: number; +}; + export type Config = { environment: string; port: number; @@ -16,6 +20,7 @@ export type Config = { market: MarketSwaggerOptions; cache: CacheConfig; + appCache: AppCacheConfig; signer?: SignerConfig; signatureKey?: string; diff --git a/packages/market/src/offers/view-offers.service.ts b/packages/market/src/offers/view-offers.service.ts index 4c91375..6f82ad7 100644 --- a/packages/market/src/offers/view-offers.service.ts +++ b/packages/market/src/offers/view-offers.service.ts @@ -1,4 +1,4 @@ -import { BadRequestException, HttpStatus, Injectable, Logger } from '@nestjs/common'; +import { BadRequestException, CACHE_MANAGER, HttpStatus, Inject, Injectable, Logger } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { OfferEntity, ViewOffers } from '@app/common/modules/database'; import { DataSource, Repository, SelectQueryBuilder, ValueTransformer } from 'typeorm'; @@ -11,6 +11,9 @@ import { GetOneFilter, SortingOrder, SortingParameter } from './interfaces/offer import { HelperService } from '@app/common/src/lib/helper.service'; import { PaginationRouting } from '@app/common/src/lib/base.constants'; import { SortingOfferRequest } from '@app/common/modules/types/requests'; +import { Cache } from 'cache-manager'; +import { ConfigService } from '@nestjs/config'; +import { AppCacheConfig } from '@app/common/modules/config/types'; const offersMapping = { priceRaw: 'price_raw', @@ -36,13 +39,18 @@ const priceTransformer: ValueTransformer = { export class ViewOffersService { private logger = new Logger(ViewOffersService.name); private readonly offersSorts: Record; + private appCache: AppCacheConfig; constructor( + configService: ConfigService, private connection: DataSource, @InjectRepository(ViewOffers) private viewOffersRepository: Repository, private readonly bundleService: BundleService, + @Inject(CACHE_MANAGER) + private readonly cache: Cache, ) { this.offersSorts = this.prepareMapping(offersMapping, connection.getMetadata(OfferEntity).columns); + this.appCache = configService.getOrThrow('appCache'); } prepareMapping = (input: Record, columnMetadata: ColumnMetadata[]): Record => { @@ -167,14 +175,25 @@ export class ViewOffersService { } public async getAttributes(): Promise { - const queryFilter = this.viewOffersRepository.createQueryBuilder('view_offers'); - const counts = await this.byAttributesCount(queryFilter); - const attributes = await this.byAttributes(queryFilter).getRawMany(); - const attributesParsed = this.parseAttributes(attributes); - return { - counts, - attributes: attributesParsed, - }; + let response = await this.cache.get('offers-attributes'); + + if (response) { + return response; + } else { + const queryFilter = this.viewOffersRepository.createQueryBuilder('view_offers'); + const counts = await this.byAttributesCount(queryFilter); + const attributes = await this.byAttributes(queryFilter).getRawMany(); + const attributesParsed = this.parseAttributes(attributes); + + response = { + counts, + attributes: attributesParsed, + }; + + await this.cache.set('offers-attributes', response, this.appCache.offersAttributesTtlMs); + + return response; + } } public async filter(offersFilter: OffersFilter, pagination: PaginationRouting, sort: SortingOfferRequest): Promise {