diff --git a/.env b/.env index 7ba05a9..229cf34 100644 --- a/.env +++ b/.env @@ -2,4 +2,6 @@ DB_HOST=127.0.0.1 DB_PORT=3306 DB_USER=root DB_PASS=root -DB_NAME=db_name \ No newline at end of file +DB_NAME=db_name +REDIS_HOST=127.0.0.1 +REDIS_PORT=6379 \ No newline at end of file diff --git a/package.json b/package.json index 73f0341..6c25229 100644 --- a/package.json +++ b/package.json @@ -20,11 +20,14 @@ "test:e2e": "jest --config ./test/jest-e2e.json" }, "dependencies": { + "@keyv/redis": "^5.1.6", + "@nestjs/cache-manager": "^3.1.3", "@nestjs/common": "^11.0.1", "@nestjs/config": "^4.0.2", "@nestjs/core": "^11.0.1", "@nestjs/platform-express": "^11.0.1", "@nestjs/sequelize": "^11.0.1", + "cache-manager": "^7.2.9", "cors": "^2.8.6", "mysql2": "^3.16.0", "reflect-metadata": "^0.2.2", diff --git a/src/app.module.ts b/src/app.module.ts index 865fc2e..581e91e 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -4,6 +4,8 @@ import { AppService } from './app.service'; import { ConfigModule, ConfigService } from '@nestjs/config'; import { BlacklistModule } from './modules/blacklist/blacklist.module'; import { SequelizeModule } from '@nestjs/sequelize'; +import { CacheModule } from '@nestjs/cache-manager'; +import { createKeyv } from '@keyv/redis'; @Module({ imports: [ @@ -25,6 +27,21 @@ import { SequelizeModule } from '@nestjs/sequelize'; synchronize: true, // Включите обратно для синхронизации }), }), + CacheModule.registerAsync({ + imports: [ConfigModule], + inject: [ConfigService], + isGlobal: true, // Делает модуль доступным во всем приложении + useFactory: (configService: ConfigService) => { + const url = `redis://${configService.get('REDIS_HOST')}:${configService.get('REDIS_PORT')}`; + return { + stores: [ + createKeyv(url, { + namespace: '', + }), + ], + }; + }, + }), BlacklistModule, ], controllers: [AppController], diff --git a/src/modules/blacklist/blacklist.service.ts b/src/modules/blacklist/blacklist.service.ts index 44bd912..ffd9bea 100644 --- a/src/modules/blacklist/blacklist.service.ts +++ b/src/modules/blacklist/blacklist.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@nestjs/common'; +import { Inject, Injectable } from '@nestjs/common'; import { InjectModel } from '@nestjs/sequelize'; import { BlacklistReport } from './models/blacklistReport.model'; import { BlacklistItem } from './types/blacklistItem'; @@ -6,12 +6,15 @@ import { BlacklistItemList } from './types/blacklistItemList'; import { BlacklistReportDto } from './dto/blacklistReport'; import { BadRequestException } from '@nestjs/common'; import { Sequelize, Op } from 'sequelize'; +import { CACHE_MANAGER } from '@nestjs/cache-manager'; +import { Cache } from '@nestjs/cache-manager'; @Injectable() export class BlacklistService { constructor( @InjectModel(BlacklistReport) private blacklistReportModel: typeof BlacklistReport, + @Inject(CACHE_MANAGER) private cacheManager: Cache, ) {} getHealthCheck(): string { @@ -19,7 +22,20 @@ export class BlacklistService { } async getAllRecords(): Promise { + const cacheKey = 'hunt-blacklist:all'; + const cachedRecords = await this.cacheManager.get(cacheKey); + if (cachedRecords && Array.isArray(cachedRecords)) { + const readyRecords = cachedRecords.map((item) => + this.blacklistReportModel.build(item, { isNewRecord: false }), + ); + return this.groupRecordsByLink(readyRecords); + } const allRecords = await this.blacklistReportModel.findAll({ raw: true }); + if (allRecords.length > 0) { + await this.cacheManager.set(cacheKey, allRecords, 60 * 1000); + const check = await this.cacheManager.get(cacheKey); + console.log('Данные в кэше:', check); + } return this.groupRecordsByLink(allRecords); } @@ -37,11 +53,24 @@ export class BlacklistService { } async getRecordsByLink(link: string): Promise { + const cacheKey = `hunt-blacklist:link:${link}`; + const cachedRecords = await this.cacheManager.get(cacheKey); + if (cachedRecords && Array.isArray(cachedRecords)) { + const readyRecords = cachedRecords.map((item) => + this.blacklistReportModel.build(item, { isNewRecord: false }), + ); + return this.groupRecordsByLink(readyRecords); + } const allRecords: BlacklistReport[] = await this.blacklistReportModel.findAll({ raw: true, where: { steam_link: link }, }); + if (allRecords.length > 0) { + await this.cacheManager.set(cacheKey, allRecords, 60 * 1000); + const check = await this.cacheManager.get(cacheKey); + console.log('Данные в кэше:', check); + } return this.groupRecordsByLink(allRecords); }