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,12 +1,22 @@
|
||||
import { RedisKeyMaker } from '@/common/utils/redisKeyMaker'
|
||||
import { GoodSelect } from '@/generated/prisma/models'
|
||||
import { PosCacheInvalidationService } from '@/modules/pos/cache/pos-cache-invalidation.service'
|
||||
import { RedisService } from '@/redis/redis.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from '../../../common/response/response-mapper'
|
||||
import { PrismaService } from '../../../prisma/prisma.service'
|
||||
import { CreateGoodDto } from './dto/create-good.dto'
|
||||
import { UpdateGoodDto } from './dto/update-good.dto'
|
||||
|
||||
@Injectable()
|
||||
export class GoodsService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
constructor(
|
||||
private prisma: PrismaService,
|
||||
private readonly redisService: RedisService,
|
||||
private readonly posCacheInvalidationService: PosCacheInvalidationService,
|
||||
) {}
|
||||
|
||||
private readonly listCacheTtlSeconds = 300
|
||||
|
||||
private readonly defaultSelect: GoodSelect = {
|
||||
id: true,
|
||||
@@ -42,6 +52,12 @@ export class GoodsService {
|
||||
}
|
||||
|
||||
async findAll(business_activity_id: string, guild_id: string) {
|
||||
const cacheKey = RedisKeyMaker.posGoodsList(business_activity_id, guild_id)
|
||||
const cached = await this.redisService.getJson<unknown[]>(cacheKey)
|
||||
if (cached) {
|
||||
return ResponseMapper.list(cached)
|
||||
}
|
||||
|
||||
const goods = await this.prisma.good.findMany({
|
||||
where: {
|
||||
OR: [
|
||||
@@ -59,6 +75,8 @@ export class GoodsService {
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
|
||||
await this.redisService.setJson(cacheKey, goods, this.listCacheTtlSeconds)
|
||||
|
||||
return ResponseMapper.list(goods)
|
||||
}
|
||||
|
||||
@@ -117,6 +135,73 @@ export class GoodsService {
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
|
||||
await this.posCacheInvalidationService.invalidateGoodsListByBusinessActivity(
|
||||
business_activity_id,
|
||||
)
|
||||
|
||||
return ResponseMapper.create(good)
|
||||
}
|
||||
|
||||
async update(id: string, data: UpdateGoodDto, business_activity_id: string) {
|
||||
const { category_id, sku_id, measure_unit_id, ...rest } = data
|
||||
|
||||
const good = await this.prisma.good.update({
|
||||
where: {
|
||||
id,
|
||||
business_activity_id,
|
||||
},
|
||||
data: {
|
||||
...rest,
|
||||
...(measure_unit_id
|
||||
? {
|
||||
measure_unit: {
|
||||
connect: {
|
||||
id: measure_unit_id,
|
||||
},
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
...(sku_id
|
||||
? {
|
||||
sku: {
|
||||
connect: {
|
||||
id: sku_id,
|
||||
},
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
...(category_id
|
||||
? {
|
||||
category: {
|
||||
connect: {
|
||||
id: category_id,
|
||||
},
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
},
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
|
||||
await this.posCacheInvalidationService.invalidateGoodsListByBusinessActivity(
|
||||
business_activity_id,
|
||||
)
|
||||
|
||||
return ResponseMapper.update(good)
|
||||
}
|
||||
|
||||
async delete(id: string, business_activity_id: string) {
|
||||
await this.prisma.good.delete({
|
||||
where: {
|
||||
id,
|
||||
business_activity_id,
|
||||
},
|
||||
})
|
||||
|
||||
await this.posCacheInvalidationService.invalidateGoodsListByBusinessActivity(
|
||||
business_activity_id,
|
||||
)
|
||||
|
||||
return ResponseMapper.delete()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user