Добавил сортировку по свежести

This commit is contained in:
Dhaverd 2026-07-10 13:01:15 +08:00
parent b48e6d11b7
commit ab8071da07
4 changed files with 53 additions and 2 deletions

4
.env
View File

@ -2,4 +2,6 @@ DB_HOST=127.0.0.1
DB_PORT=3306
DB_USER=root
DB_PASS=root
DB_NAME=db_name
DB_NAME=db_name
REDIS_HOST=127.0.0.1
REDIS_PORT=6379

View File

@ -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",

View File

@ -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<string>('REDIS_HOST')}:${configService.get<string>('REDIS_PORT')}`;
return {
stores: [
createKeyv(url, {
namespace: '',
}),
],
};
},
}),
BlacklistModule,
],
controllers: [AppController],

View File

@ -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<BlacklistItemList> {
const cacheKey = 'hunt-blacklist:all';
const cachedRecords = await this.cacheManager.get<any[]>(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<BlacklistItemList> {
const cacheKey = `hunt-blacklist:link:${link}`;
const cachedRecords = await this.cacheManager.get<any[]>(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);
}