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,7 +1,10 @@
|
||||
import { UploadedFileTypes } from '@/common/enums/enums'
|
||||
import { RedisKeyMaker } from '@/common/utils/redisKeyMaker'
|
||||
import { GoodSelect, GoodWhereInput } from '@/generated/prisma/models'
|
||||
import { AdminGuildCacheInvalidationService } from '@/modules/admin/guilds/cache/admin-guild-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 { CreateGuildGoodDto, UpdateGuildGoodDto } from './dto/create-good.dto'
|
||||
@@ -11,8 +14,12 @@ export class GoodsService {
|
||||
constructor(
|
||||
private prisma: PrismaService,
|
||||
private readonly uploaderService: UploaderService,
|
||||
private readonly redisService: RedisService,
|
||||
private readonly cacheInvalidationService: AdminGuildCacheInvalidationService,
|
||||
) {}
|
||||
|
||||
private readonly listCacheTtlSeconds = 300
|
||||
|
||||
private readonly defaultSelect: GoodSelect = {
|
||||
id: true,
|
||||
name: true,
|
||||
@@ -49,6 +56,14 @@ export class GoodsService {
|
||||
})
|
||||
|
||||
async findAll(guildId: string) {
|
||||
const cacheKey = RedisKeyMaker.guildGoodsList(guildId)
|
||||
const cached = await this.redisService.getJson<{ goods: unknown[]; total: number }>(
|
||||
cacheKey,
|
||||
)
|
||||
if (cached) {
|
||||
return ResponseMapper.paginate(cached.goods, { total: cached.total })
|
||||
}
|
||||
|
||||
const [goods, total] = await this.prisma.$transaction([
|
||||
this.prisma.good.findMany({
|
||||
where: this.defaultWhere(guildId),
|
||||
@@ -56,6 +71,9 @@ export class GoodsService {
|
||||
}),
|
||||
this.prisma.good.count(),
|
||||
])
|
||||
|
||||
await this.redisService.setJson(cacheKey, { goods, total }, this.listCacheTtlSeconds)
|
||||
|
||||
return ResponseMapper.paginate(goods, {
|
||||
total,
|
||||
})
|
||||
@@ -75,6 +93,10 @@ export class GoodsService {
|
||||
|
||||
async create(data: CreateGuildGoodDto, file: Express.Multer.File) {
|
||||
const { category_id, measure_unit_id, sku_id, ...rest } = data
|
||||
const category = await this.prisma.goodCategory.findUnique({
|
||||
where: { id: category_id },
|
||||
select: { guild_id: true },
|
||||
})
|
||||
|
||||
const good = await this.prisma.$transaction(async tx => {
|
||||
let image_url = ''
|
||||
@@ -111,11 +133,25 @@ export class GoodsService {
|
||||
})
|
||||
})
|
||||
|
||||
if (category?.guild_id) {
|
||||
await this.cacheInvalidationService.invalidateGoodsList(category.guild_id)
|
||||
}
|
||||
|
||||
return ResponseMapper.create(good)
|
||||
}
|
||||
|
||||
async update(id: string, data: UpdateGuildGoodDto, file: Express.Multer.File) {
|
||||
const { category_id, measure_unit_id, sku_id, ...rest } = data
|
||||
const prevGood = await this.prisma.good.findUnique({
|
||||
where: { id },
|
||||
select: { category: { select: { guild_id: true } } },
|
||||
})
|
||||
const nextCategory = category_id
|
||||
? await this.prisma.goodCategory.findUnique({
|
||||
where: { id: category_id },
|
||||
select: { guild_id: true },
|
||||
})
|
||||
: null
|
||||
|
||||
const good = await this.prisma.$transaction(async tx => {
|
||||
let image_url = ''
|
||||
@@ -162,6 +198,18 @@ export class GoodsService {
|
||||
})
|
||||
})
|
||||
|
||||
const cacheGuildIds = new Set<string>()
|
||||
if (prevGood?.category?.guild_id) {
|
||||
cacheGuildIds.add(prevGood.category.guild_id)
|
||||
}
|
||||
if (nextCategory?.guild_id) {
|
||||
cacheGuildIds.add(nextCategory.guild_id)
|
||||
}
|
||||
|
||||
for (const guildId of cacheGuildIds) {
|
||||
await this.cacheInvalidationService.invalidateGoodsList(guildId)
|
||||
}
|
||||
|
||||
return ResponseMapper.create(good)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user