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 { QUERY_CONSTANTS } from '@/common/queryConstants'
|
||||
import mapConsumerWithLicenseUtil from '@/common/utils/mappers/consumer_mappers.util'
|
||||
import { PasswordUtil } from '@/common/utils/password.util'
|
||||
import { RedisKeyMaker } from '@/common/utils/redisKeyMaker'
|
||||
import {
|
||||
AccountStatus,
|
||||
AccountType,
|
||||
@@ -15,13 +16,21 @@ import {
|
||||
ConsumerWhereInput,
|
||||
} from '@/generated/prisma/models'
|
||||
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 { PartnersCacheInvalidationService } from '../cache/partners-cache-invalidation.service'
|
||||
import { CreateConsumerDto, UpdateConsumerDto } from './dto/create-consumers.dto'
|
||||
|
||||
@Injectable()
|
||||
export class PartnerConsumersService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly redisService: RedisService,
|
||||
private readonly partnersCacheInvalidationService: PartnersCacheInvalidationService,
|
||||
) {}
|
||||
|
||||
private readonly infoCacheTtlSeconds = 300
|
||||
|
||||
private readonly setExpireDate = (starts_at: Date) => {
|
||||
return new Date(
|
||||
@@ -54,6 +63,14 @@ export class PartnerConsumersService {
|
||||
})
|
||||
|
||||
async findAll(partner_id: string, page = 1, perPage = 10) {
|
||||
const cacheKey = RedisKeyMaker.partnerConsumersList(partner_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 [consumers, total] = await this.prisma.$transaction(async tx => [
|
||||
await tx.consumer.findMany({
|
||||
where: this.defaultWhere(partner_id),
|
||||
@@ -65,7 +82,13 @@ export class PartnerConsumersService {
|
||||
where: this.defaultWhere(partner_id),
|
||||
}),
|
||||
])
|
||||
return ResponseMapper.paginate(consumers.map(mapConsumerWithLicenseUtil), {
|
||||
const mappedItems = consumers.map(mapConsumerWithLicenseUtil)
|
||||
await this.redisService.setJson(
|
||||
cacheKey,
|
||||
{ items: mappedItems, total },
|
||||
this.infoCacheTtlSeconds,
|
||||
)
|
||||
return ResponseMapper.paginate(mappedItems, {
|
||||
total,
|
||||
page,
|
||||
perPage,
|
||||
@@ -73,6 +96,12 @@ export class PartnerConsumersService {
|
||||
}
|
||||
|
||||
async findOne(partner_id: string, consumer_id: string) {
|
||||
const cacheKey = RedisKeyMaker.partnerConsumerInfo(partner_id, consumer_id)
|
||||
const cached = await this.redisService.getJson<unknown>(cacheKey)
|
||||
if (cached) {
|
||||
return ResponseMapper.single(cached)
|
||||
}
|
||||
|
||||
const consumer = await this.prisma.consumer.findUniqueOrThrow({
|
||||
where: {
|
||||
...(this.defaultWhere(partner_id) as any),
|
||||
@@ -81,7 +110,14 @@ export class PartnerConsumersService {
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
|
||||
return ResponseMapper.single(mapConsumerWithLicenseUtil(consumer))
|
||||
const mappedConsumer = mapConsumerWithLicenseUtil(consumer)
|
||||
await this.redisService.setJson(
|
||||
RedisKeyMaker.consumerInfo(consumer_id),
|
||||
mappedConsumer,
|
||||
this.infoCacheTtlSeconds,
|
||||
)
|
||||
await this.redisService.setJson(cacheKey, mappedConsumer, this.infoCacheTtlSeconds)
|
||||
return ResponseMapper.single(mappedConsumer)
|
||||
}
|
||||
|
||||
async create(partner_id: string, data: CreateConsumerDto) {
|
||||
@@ -224,11 +260,16 @@ export class PartnerConsumersService {
|
||||
})
|
||||
})
|
||||
|
||||
await this.partnersCacheInvalidationService.invalidatePartnerConsumerReadModels(
|
||||
partner_id,
|
||||
consumer.id,
|
||||
)
|
||||
|
||||
return ResponseMapper.single(mapConsumerWithLicenseUtil(consumer))
|
||||
}
|
||||
|
||||
async update(id: string, partner_id: string, data: UpdateConsumerDto) {
|
||||
return this.prisma.consumer.update({
|
||||
const updatedConsumer = await this.prisma.consumer.update({
|
||||
where: {
|
||||
...(this.defaultWhere(partner_id) as any),
|
||||
id,
|
||||
@@ -243,5 +284,13 @@ export class PartnerConsumersService {
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
await this.partnersCacheInvalidationService.invalidatePartnerConsumerReadModels(
|
||||
partner_id,
|
||||
id,
|
||||
)
|
||||
await this.redisService.delete(RedisKeyMaker.consumerInfo(id))
|
||||
|
||||
return updatedConsumer
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user