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:
2026-05-19 15:40:45 +03:30
parent c5c522f69c
commit 62b659246f
32 changed files with 1146 additions and 42 deletions
+25
View File
@@ -0,0 +1,25 @@
import { Injectable } from '@nestjs/common'
import { RedisService } from '@/redis/redis.service'
@Injectable()
export class PosCacheInvalidationService {
constructor(private readonly redisService: RedisService) {}
async invalidateGoodsListByGuild(guildId: string): Promise<void> {
const client = await this.redisService.getClient()
const keys = await client.keys(`pos:ba:*:guild:${guildId}:goods:list`)
if (keys.length > 0) {
await client.del(keys)
}
}
async invalidateGoodsListByBusinessActivity(
businessActivityId: string,
): Promise<void> {
const client = await this.redisService.getClient()
const keys = await client.keys(`pos:ba:${businessActivityId}:guild:*:goods:list`)
if (keys.length > 0) {
await client.del(keys)
}
}
}
@@ -0,0 +1,4 @@
import { PartialType } from '@nestjs/swagger'
import { CreateGoodDto } from './create-good.dto'
export class UpdateGoodDto extends PartialType(CreateGoodDto) {}
+86 -1
View File
@@ -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()
}
}
+29 -5
View File
@@ -1,15 +1,29 @@
import { QUERY_CONSTANTS } from '@/common/queryConstants'
import consumer_mappersUtil from '@/common/utils/mappers/consumer_mappers.util'
import { RedisKeyMaker } from '@/common/utils/redisKeyMaker'
import { ConsumerStatus } from '@/generated/prisma/enums'
import { PrismaService } from '@/prisma/prisma.service'
import { RedisService } from '@/redis/redis.service'
import { Injectable } from '@nestjs/common'
import { ResponseMapper } from 'common/response/response-mapper'
@Injectable()
export class PosService {
constructor(private readonly prisma: PrismaService) {}
constructor(
private readonly prisma: PrismaService,
private readonly redisService: RedisService,
) {}
private readonly infoCacheTtlSeconds = 300
private readonly meCacheTtlSeconds = 120
async getInfo(pos_id: string) {
const cacheKey = RedisKeyMaker.posInfo(pos_id)
const cached = await this.redisService.getJson<unknown>(cacheKey)
if (cached) {
return ResponseMapper.single(cached)
}
const pos = await this.prisma.pos.findUniqueOrThrow({
where: {
id: pos_id,
@@ -71,7 +85,7 @@ export class PosService {
license_activation,
} = business_activity
return ResponseMapper.single({
const payload = {
name,
complex: {
id: complexId,
@@ -89,7 +103,9 @@ export class PosService {
license_id: license_activation?.license.id,
},
partner: license_activation?.license.charge_transaction.partner,
})
}
await this.redisService.setJson(cacheKey, payload, this.infoCacheTtlSeconds)
return ResponseMapper.single(payload)
}
async getAccessible(account_id: string) {
@@ -136,6 +152,12 @@ export class PosService {
}
async getMe(account_id: string, pos_id: string) {
const cacheKey = RedisKeyMaker.posMe(account_id, pos_id)
const cached = await this.redisService.getJson<unknown>(cacheKey)
if (cached) {
return ResponseMapper.single(cached)
}
const pos = await this.prisma.consumerAccount.findUniqueOrThrow({
where: {
id: account_id,
@@ -177,9 +199,11 @@ export class PosService {
const { consumer, ...rest } = pos
return ResponseMapper.single({
const payload = {
...rest,
consumer: consumer_mappersUtil(consumer),
})
}
await this.redisService.setJson(cacheKey, payload, this.meCacheTtlSeconds)
return ResponseMapper.single(payload)
}
}