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,13 +1,22 @@
|
||||
import { QUERY_CONSTANTS } from '@/common/queryConstants'
|
||||
import { RedisKeyMaker } from '@/common/utils'
|
||||
import { ComplexSelect, ComplexWhereInput } from '@/generated/prisma/models'
|
||||
import { PartnersCacheInvalidationService } from '@/modules/partners/cache/partners-cache-invalidation.service'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { RedisService } from '@/redis/redis.service'
|
||||
import { BadRequestException, Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
import { CreateComplexDto, UpdateComplexDto } from './dto/complex.dto'
|
||||
|
||||
@Injectable()
|
||||
export class BusinessActivityComplexesService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly redisService: RedisService,
|
||||
private readonly partnersCacheInvalidationService: PartnersCacheInvalidationService,
|
||||
) {}
|
||||
|
||||
private readonly cacheTtlSeconds = 300
|
||||
|
||||
private readonly defaultSelect: ComplexSelect = {
|
||||
id: true,
|
||||
@@ -49,6 +58,20 @@ export class BusinessActivityComplexesService {
|
||||
page = 1,
|
||||
perPage = 10,
|
||||
) {
|
||||
const cacheKey = RedisKeyMaker.partnerConsumerBusinessActivityComplexesList(
|
||||
partner_id,
|
||||
consumer_id,
|
||||
business_activity_id,
|
||||
page,
|
||||
perPage,
|
||||
)
|
||||
const cached = await this.redisService.getJson<{ items: unknown[]; total: number }>(
|
||||
cacheKey,
|
||||
)
|
||||
if (cached) {
|
||||
return ResponseMapper.paginate(cached.items, { total: cached.total, page, perPage })
|
||||
}
|
||||
|
||||
const where = this.defaultWhere(partner_id, consumer_id, business_activity_id)
|
||||
const [complexes, total] = await this.prisma.$transaction(async tx => [
|
||||
await tx.complex.findMany({
|
||||
@@ -74,6 +97,11 @@ export class BusinessActivityComplexesService {
|
||||
pos_count: _count.pos_list,
|
||||
}
|
||||
})
|
||||
await this.redisService.setJson(
|
||||
cacheKey,
|
||||
{ items: mappedComplexes, total },
|
||||
this.cacheTtlSeconds,
|
||||
)
|
||||
return ResponseMapper.paginate(mappedComplexes, { total, page, perPage })
|
||||
}
|
||||
|
||||
@@ -83,18 +111,40 @@ export class BusinessActivityComplexesService {
|
||||
business_activity_id: string,
|
||||
id: string,
|
||||
) {
|
||||
const complex = await this.prisma.complex.findUnique({
|
||||
const cacheKey = RedisKeyMaker.partnerConsumerBusinessActivityComplexInfo(
|
||||
partner_id,
|
||||
consumer_id,
|
||||
business_activity_id,
|
||||
id,
|
||||
)
|
||||
const cached = await this.redisService.getJson<unknown>(cacheKey)
|
||||
if (cached) {
|
||||
return ResponseMapper.single(cached)
|
||||
}
|
||||
|
||||
const complex = await this.prisma.complex.findFirst({
|
||||
where: {
|
||||
...this.defaultWhere(partner_id, consumer_id, business_activity_id),
|
||||
id,
|
||||
},
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
|
||||
if (!complex) {
|
||||
throw new BadRequestException('شعبه مورد نظر یافت نشد.')
|
||||
}
|
||||
|
||||
await this.redisService.setJson(cacheKey, complex, this.cacheTtlSeconds)
|
||||
return ResponseMapper.single(complex)
|
||||
}
|
||||
|
||||
async create(partner_id: string, business_id: string, data: CreateComplexDto) {
|
||||
const complex = this.prisma.$transaction(async tx => {
|
||||
async create(
|
||||
partner_id: string,
|
||||
business_id: string,
|
||||
consumer_id: string,
|
||||
data: CreateComplexDto,
|
||||
) {
|
||||
const complex = await this.prisma.$transaction(async tx => {
|
||||
const relatedLicense = await tx.licenseAccountAllocation.findFirst({
|
||||
where: {
|
||||
license_activation: {
|
||||
@@ -121,7 +171,7 @@ export class BusinessActivityComplexesService {
|
||||
)
|
||||
}
|
||||
|
||||
return await this.prisma.complex.create({
|
||||
return await tx.complex.create({
|
||||
data: {
|
||||
...data,
|
||||
business_activity: {
|
||||
@@ -133,6 +183,12 @@ export class BusinessActivityComplexesService {
|
||||
})
|
||||
})
|
||||
|
||||
await this.partnersCacheInvalidationService.invalidatePartnerConsumerBusinessActivityComplexesReadModels(
|
||||
partner_id,
|
||||
consumer_id,
|
||||
business_id,
|
||||
)
|
||||
|
||||
return ResponseMapper.create(complex)
|
||||
}
|
||||
|
||||
@@ -147,6 +203,13 @@ export class BusinessActivityComplexesService {
|
||||
where: { ...this.defaultWhere(partner_id, consumer_id, business_activity_id), id },
|
||||
data,
|
||||
})
|
||||
|
||||
await this.partnersCacheInvalidationService.invalidatePartnerConsumerBusinessActivityComplexReadModels(
|
||||
partner_id,
|
||||
consumer_id,
|
||||
business_activity_id,
|
||||
id,
|
||||
)
|
||||
return ResponseMapper.update(complex)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user