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
@@ -0,0 +1,17 @@
import { RedisKeyMaker } from '@/common/utils/redisKeyMaker'
import { PosCacheInvalidationService } from '@/modules/pos/cache/pos-cache-invalidation.service'
import { RedisService } from '@/redis/redis.service'
import { Injectable } from '@nestjs/common'
@Injectable()
export class AdminGuildCacheInvalidationService {
constructor(
private readonly redisService: RedisService,
private readonly posCacheInvalidationService: PosCacheInvalidationService,
) {}
async invalidateGoodsList(guildId: string): Promise<void> {
await this.redisService.delete(RedisKeyMaker.guildGoodsList(guildId))
await this.posCacheInvalidationService.invalidateGoodsListByGuild(guildId)
}
}
@@ -1,5 +1,6 @@
import { GoodCategoryOmit } from '@/generated/prisma/models'
import { PrismaService } from '@/prisma/prisma.service'
import { AdminGuildCacheInvalidationService } from '@/modules/admin/guilds/cache/admin-guild-cache-invalidation.service'
import { Injectable } from '@nestjs/common'
import { ResponseMapper } from 'common/response/response-mapper'
import {
@@ -9,7 +10,10 @@ import {
@Injectable()
export class GoodCategoriesService {
constructor(private prisma: PrismaService) {}
constructor(
private prisma: PrismaService,
private readonly cacheInvalidationService: AdminGuildCacheInvalidationService,
) {}
private readonly goodCategoriesOmit = {
complex_id: true,
@@ -67,8 +71,8 @@ export class GoodCategoriesService {
})
}
create(guild_id: string, data: CreateGoodCategoryDto) {
const category = this.prisma.goodCategory.create({
async create(guild_id: string, data: CreateGoodCategoryDto) {
const category = await this.prisma.goodCategory.create({
data: {
guild: {
connect: {
@@ -83,6 +87,8 @@ export class GoodCategoriesService {
omit: this.goodCategoriesOmit,
})
await this.cacheInvalidationService.invalidateGoodsList(guild_id)
return ResponseMapper.create({ ...category, goods_count: 0 })
}
@@ -113,6 +119,8 @@ export class GoodCategoriesService {
omit: this.goodCategoriesOmit,
})
await this.cacheInvalidationService.invalidateGoodsList(guild_id)
return ResponseMapper.update({
...category,
goods_count: Number(category?._count.goods),
@@ -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)
}
}
@@ -1,4 +1,7 @@
import { RedisKeyMaker } from '@/common/utils/redisKeyMaker'
import { AdminGuildCacheInvalidationService } from '@/modules/admin/guilds/cache/admin-guild-cache-invalidation.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 { CreateStockKeepingUnitDto } from './dto/create-stock-keeping-unit.dto'
@@ -6,7 +9,17 @@ import { UpdateStockKeepingUnitDto } from './dto/update-stock-keeping-unit.dto'
@Injectable()
export class StockKeepingUnitsService {
constructor(private readonly prisma: PrismaService) {}
constructor(
private readonly prisma: PrismaService,
private readonly redisService: RedisService,
private readonly cacheInvalidationService: AdminGuildCacheInvalidationService,
) {}
private readonly listCacheTtlSeconds = 300
private getListCacheKey(guild_id: string): string {
return RedisKeyMaker.guildStockKeepingUnitsList(guild_id)
}
private mapData(data: any) {
const { name, is_foreign, is_domestic, ...rest } = data
@@ -17,12 +30,19 @@ export class StockKeepingUnitsService {
}
async findAll(guild_id: string) {
const cacheKey = this.getListCacheKey(guild_id)
const cached = await this.redisService.getJson<unknown[]>(cacheKey)
if (cached) {
return ResponseMapper.list(cached)
}
const items = await this.prisma.stockKeepingUnits.findMany({
where: { guild_id },
orderBy: { created_at: 'desc' },
})
const mappedData = items.map(this.mapData)
await this.redisService.setJson(cacheKey, mappedData, this.listCacheTtlSeconds)
return ResponseMapper.list(mappedData)
}
@@ -39,6 +59,9 @@ export class StockKeepingUnitsService {
},
})
await this.redisService.delete(this.getListCacheKey(guild_id))
await this.cacheInvalidationService.invalidateGoodsList(guild_id)
const mappedData = this.mapData(item)
return ResponseMapper.create(mappedData)
}
@@ -48,6 +71,10 @@ export class StockKeepingUnitsService {
where: { guild_id, id },
data,
})
await this.redisService.delete(this.getListCacheKey(guild_id))
await this.cacheInvalidationService.invalidateGoodsList(guild_id)
return ResponseMapper.update(item)
}
}
@@ -1,15 +1,30 @@
import { QUERY_CONSTANTS } from '@/common/queryConstants'
import consumer_mappersUtil from '@/common/utils/mappers/consumer_mappers.util'
import { RedisKeyMaker } from '@/common/utils/redisKeyMaker'
import { LicenseActivationWhereInput } 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 { Injectable } from '@nestjs/common'
import { ResponseMapper } from 'common/response/response-mapper'
@Injectable()
export class PartnerActivatedLicensesService {
constructor(private readonly prisma: PrismaService) {}
constructor(
private readonly prisma: PrismaService,
private readonly redisService: RedisService,
private readonly cacheInvalidationService: PartnersCacheInvalidationService,
) {}
async findAll(partner_id: string, page = 1, perPage = 10) {
const cacheKey = RedisKeyMaker.partnerActivatedLicensesList(partner_id, page, perPage)
const cached = await this.redisService.getJson<{ data: unknown[]; total: number }>(
cacheKey,
)
if (cached) {
return ResponseMapper.paginate(cached.data, { page, perPage, total: cached.total })
}
const defaultWhere: LicenseActivationWhereInput = {
license: {
charge_transaction: {
@@ -73,6 +88,8 @@ export class PartnerActivatedLicensesService {
}
})
await this.redisService.setJson(cacheKey, { data: mappedLicenses, total }, 300)
return ResponseMapper.paginate(mappedLicenses, {
page,
perPage,
@@ -90,6 +107,7 @@ export class PartnerActivatedLicensesService {
async delete(partnerId: string, id: string) {
await this.prisma.license.delete({ where: { id } })
await this.cacheInvalidationService.invalidatePartnerLicenses(partnerId)
return ResponseMapper.delete()
}
}
@@ -1,3 +1,4 @@
import { RedisKeyMaker } from '@/common/utils/redisKeyMaker'
import {
generateTrackingCode,
isTrackingCodeUniqueViolation,
@@ -6,14 +7,20 @@ import {
LicenseChargeTransactionSelect,
LicenseChargeTransactionWhereInput,
} 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 { ChargeLicenseDto } from './dto/chargedLicenseTransactions.dto'
@Injectable()
export class PartnerLicenseChargeTransactionService {
constructor(private readonly prisma: PrismaService) {}
constructor(
private readonly prisma: PrismaService,
private readonly redisService: RedisService,
private readonly cacheInvalidationService: PartnersCacheInvalidationService,
) {}
private readonly TRACKING_CODE_LENGTH = 8
private readonly TRACKING_CODE_MAX_ATTEMPTS = 5
@@ -53,6 +60,18 @@ export class PartnerLicenseChargeTransactionService {
}
async findAll(partner_id: string, page = 1, perPage = 10) {
const cacheKey = RedisKeyMaker.partnerLicenseChargeTransactionsList(
partner_id,
page,
perPage,
)
const cached = await this.redisService.getJson<{ data: unknown[]; total: number }>(
cacheKey,
)
if (cached) {
return ResponseMapper.paginate(cached.data, { page, perPage, total: cached.total })
}
const defaultWhere: LicenseChargeTransactionWhereInput = {
partner_id,
}
@@ -70,6 +89,7 @@ export class PartnerLicenseChargeTransactionService {
])
const mappedTransactions = transactions.map(this.mappedTransaction)
await this.redisService.setJson(cacheKey, { data: mappedTransactions, total }, 300)
return ResponseMapper.paginate(mappedTransactions, {
page,
@@ -79,6 +99,12 @@ export class PartnerLicenseChargeTransactionService {
}
async findOne(partner_id: string, id: string) {
const cacheKey = RedisKeyMaker.partnerLicenseChargeTransactionDetail(partner_id, id)
const cached = await this.redisService.getJson<unknown>(cacheKey)
if (cached) {
return ResponseMapper.single(cached)
}
const transaction = await this.prisma.licenseChargeTransaction.findUniqueOrThrow({
where: {
id,
@@ -86,7 +112,9 @@ export class PartnerLicenseChargeTransactionService {
},
select: this.defaultSelect,
})
return ResponseMapper.single(this.mappedTransaction(transaction))
const mappedTransaction = this.mappedTransaction(transaction)
await this.redisService.setJson(cacheKey, mappedTransaction, 300)
return ResponseMapper.single(mappedTransaction)
}
async create(partner_id: string, data: ChargeLicenseDto) {
@@ -134,6 +162,8 @@ export class PartnerLicenseChargeTransactionService {
})
this.scheduleLicenseProvisioning(transaction.id, transaction.purchased_count)
await this.cacheInvalidationService.invalidatePartnersList()
await this.cacheInvalidationService.invalidatePartnerLicenses(partner_id)
return ResponseMapper.create({
transaction_id: transaction.id,
charged_license_count: transaction.purchased_count,
+32 -1
View File
@@ -1,6 +1,7 @@
import { UploadedFileTypes } from '@/common/enums/enums'
import { translateEnumValue } from '@/common/utils'
import { PasswordUtil } from '@/common/utils/password.util'
import { RedisKeyMaker } from '@/common/utils/redisKeyMaker'
import {
AccountStatus,
AccountType,
@@ -8,8 +9,10 @@ import {
PartnerStatus,
} from '@/generated/prisma/enums'
import { PartnerSelect } from '@/generated/prisma/models'
import { PartnersCacheInvalidationService } from '@/modules/partners/cache/partners-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 { CreatePartnerDto, UpdatePartnerDto } from './dto/partner.dto'
@@ -20,6 +23,8 @@ export class PartnersService {
constructor(
private readonly prisma: PrismaService,
private readonly uploaderService: UploaderService,
private readonly redisService: RedisService,
private readonly cacheInvalidationService: PartnersCacheInvalidationService,
) {}
private readonly defaultSelect: PartnerSelect = {
@@ -137,22 +142,40 @@ export class PartnersService {
}
async findAll() {
const cacheKey = RedisKeyMaker.partnersList()
const cached = await this.redisService.getJson<unknown[]>(cacheKey)
if (cached) {
console.log('cached', cached)
return ResponseMapper.list(cached)
}
const partners = await this.prisma.partner.findMany({
select: this.defaultSelect,
})
const mappedPartners = partners.map(this.mapPartner)
await this.redisService.setJson(cacheKey, mappedPartners, 300)
return ResponseMapper.list(mappedPartners)
}
async findOne(id: string) {
const cacheKey = RedisKeyMaker.partnerDetail(id)
const cached = await this.redisService.getJson<unknown>(cacheKey)
if (cached) {
return ResponseMapper.single(cached)
}
const partner = await this.prisma.partner.findUniqueOrThrow({
where: { id },
select: this.defaultSelect,
})
return ResponseMapper.single(this.mapPartner(partner))
const mappedPartner = this.mapPartner(partner)
await this.redisService.setJson(cacheKey, mappedPartner, 300)
return ResponseMapper.single(mappedPartner)
}
async create(data: CreatePartnerDto, logo?: any) {
@@ -184,6 +207,8 @@ export class PartnersService {
},
select: this.defaultSelect,
})
await this.cacheInvalidationService.invalidatePartnersList()
await this.cacheInvalidationService.invalidatePartnerDetail(partner.id)
return ResponseMapper.create(partner)
}
@@ -220,11 +245,17 @@ export class PartnersService {
})
})
await this.cacheInvalidationService.invalidatePartnersList()
await this.cacheInvalidationService.invalidatePartnerDetail(id)
return ResponseMapper.update(partner)
}
async delete(id: string) {
await this.prisma.partner.delete({ where: { id } })
await this.cacheInvalidationService.invalidatePartnersList()
await this.cacheInvalidationService.invalidatePartnerDetail(id)
await this.cacheInvalidationService.invalidatePartnerLicenses(id)
return ResponseMapper.delete()
}
}