feat: enhance API and query handling with Redis caching; add QueryGuard for query validation; refactor services to utilize RedisClient for improved performance

This commit is contained in:
Boris D
2025-10-10 10:51:52 +03:00
parent 45db65cec8
commit ca134414b0
20 changed files with 228 additions and 64 deletions

View File

@ -1,13 +1,16 @@
import { Injectable } from "@nestjs/common";
import { Inject, Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import { Project } from "./entities/project.entity";
import { RedisClient } from "src/redis/redis.service";
@Injectable()
export class ProjectService {
constructor(
@InjectRepository(Project)
private readonly projectRepository: Repository<Project>
private readonly projectRepository: Repository<Project>,
@Inject(RedisClient)
private readonly redisClient: RedisClient
) {}
create(name: string) {
@ -15,17 +18,33 @@ export class ProjectService {
return this.projectRepository.save(project);
}
findById(id: string) {
return this.projectRepository.findOne({ where: { id: id } });
async findById(id: string) {
const cached = await this.redisClient.get(`project_${id}`);
if (cached) {
return cached;
}
const project = await this.projectRepository.findOne({ where: { id: id } });
if (project) {
await this.redisClient.set(`project_${id}`, project, 300);
}
return project;
}
updateDatabase(projectId: string, databaseId: string) {
async updateDatabase(projectId: string, databaseId: string) {
await this.redisClient.del(`project_${projectId}`);
return this.projectRepository.update(projectId, {
database: { id: databaseId },
});
}
updateRedisNode(projectId: string, redisNodeId: { id: string }[]) {
async updateRedisNode(projectId: string, redisNodeId: { id: string }[]) {
await this.redisClient.del(`project_${projectId}`);
return this.projectRepository.update(projectId, {
redisNodes: redisNodeId,
});