feat: implement Redis caching for business activities and consumers
- Added Redis caching to BusinessActivitiesService for findAll and findOne methods. - Integrated Redis caching in BusinessActivityComplexesService for findAll and findOne methods. - Enhanced ConsumersService with Redis caching for findAll and findOne methods. - Introduced cache invalidation for partner consumers and business activities. - Created RedisKeyMaker utility for generating cache keys for consumers, partners, and POS. - Implemented cache invalidation services for partners and POS. - Added Redis service methods for JSON handling and key deletion by patterns. - Updated goods service to include caching and invalidation for goods list. - Introduced DTO for updating goods.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { UploadedFileTypes } from '@/common/enums/enums'
|
||||
import { translateEnumValue } from '@/common/utils'
|
||||
import { PasswordUtil } from '@/common/utils/password.util'
|
||||
import { RedisKeyMaker } from '@/common/utils/redisKeyMaker'
|
||||
import {
|
||||
AccountStatus,
|
||||
AccountType,
|
||||
@@ -8,8 +9,10 @@ import {
|
||||
PartnerStatus,
|
||||
} from '@/generated/prisma/enums'
|
||||
import { PartnerSelect } from '@/generated/prisma/models'
|
||||
import { PartnersCacheInvalidationService } from '@/modules/partners/cache/partners-cache-invalidation.service'
|
||||
import { UploaderService } from '@/modules/uploader/uploader.service'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { RedisService } from '@/redis/redis.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
import { CreatePartnerDto, UpdatePartnerDto } from './dto/partner.dto'
|
||||
@@ -20,6 +23,8 @@ export class PartnersService {
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly uploaderService: UploaderService,
|
||||
private readonly redisService: RedisService,
|
||||
private readonly cacheInvalidationService: PartnersCacheInvalidationService,
|
||||
) {}
|
||||
|
||||
private readonly defaultSelect: PartnerSelect = {
|
||||
@@ -137,22 +142,40 @@ export class PartnersService {
|
||||
}
|
||||
|
||||
async findAll() {
|
||||
const cacheKey = RedisKeyMaker.partnersList()
|
||||
const cached = await this.redisService.getJson<unknown[]>(cacheKey)
|
||||
if (cached) {
|
||||
console.log('cached', cached)
|
||||
|
||||
return ResponseMapper.list(cached)
|
||||
}
|
||||
|
||||
const partners = await this.prisma.partner.findMany({
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
|
||||
const mappedPartners = partners.map(this.mapPartner)
|
||||
await this.redisService.setJson(cacheKey, mappedPartners, 300)
|
||||
|
||||
return ResponseMapper.list(mappedPartners)
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
const cacheKey = RedisKeyMaker.partnerDetail(id)
|
||||
const cached = await this.redisService.getJson<unknown>(cacheKey)
|
||||
if (cached) {
|
||||
return ResponseMapper.single(cached)
|
||||
}
|
||||
|
||||
const partner = await this.prisma.partner.findUniqueOrThrow({
|
||||
where: { id },
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
|
||||
return ResponseMapper.single(this.mapPartner(partner))
|
||||
const mappedPartner = this.mapPartner(partner)
|
||||
await this.redisService.setJson(cacheKey, mappedPartner, 300)
|
||||
|
||||
return ResponseMapper.single(mappedPartner)
|
||||
}
|
||||
|
||||
async create(data: CreatePartnerDto, logo?: any) {
|
||||
@@ -184,6 +207,8 @@ export class PartnersService {
|
||||
},
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
await this.cacheInvalidationService.invalidatePartnersList()
|
||||
await this.cacheInvalidationService.invalidatePartnerDetail(partner.id)
|
||||
return ResponseMapper.create(partner)
|
||||
}
|
||||
|
||||
@@ -220,11 +245,17 @@ export class PartnersService {
|
||||
})
|
||||
})
|
||||
|
||||
await this.cacheInvalidationService.invalidatePartnersList()
|
||||
await this.cacheInvalidationService.invalidatePartnerDetail(id)
|
||||
|
||||
return ResponseMapper.update(partner)
|
||||
}
|
||||
|
||||
async delete(id: string) {
|
||||
await this.prisma.partner.delete({ where: { id } })
|
||||
await this.cacheInvalidationService.invalidatePartnersList()
|
||||
await this.cacheInvalidationService.invalidatePartnerDetail(id)
|
||||
await this.cacheInvalidationService.invalidatePartnerLicenses(id)
|
||||
return ResponseMapper.delete()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user