refactor: streamline Redis caching logic across services

- Implemented a unified `getAndSet` method in RedisService to handle caching for single, list, and paginated responses.
- Removed redundant cache checks and writes in various services, simplifying the code and improving readability.
- Updated GoodsService, StockKeepingUnitsService, PartnerActivatedLicensesService, and others to utilize the new caching mechanism.
- Adjusted Prisma connection limit for better resource management.
This commit is contained in:
2026-05-21 17:27:37 +03:30
parent 1d47fb1a1d
commit 9aa12184a1
16 changed files with 689 additions and 1896 deletions
@@ -65,56 +65,45 @@ export class PartnerLicenseChargeTransactionService {
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,
}
return await this.redisService.getAndSet(cacheKey, 'paginate', async () => {
const [transactions, total] = await this.prisma.$transaction(async tx => [
await tx.licenseChargeTransaction.findMany({
where: defaultWhere,
skip: (page - 1) * perPage,
take: perPage,
select: this.defaultSelect,
}),
await tx.licenseChargeTransaction.count({
where: defaultWhere,
}),
])
const [transactions, total] = await this.prisma.$transaction(async tx => [
await tx.licenseChargeTransaction.findMany({
where: defaultWhere,
skip: (page - 1) * perPage,
take: perPage,
select: this.defaultSelect,
}),
await tx.licenseChargeTransaction.count({
where: defaultWhere,
}),
])
const mappedTransactions = transactions.map(this.mappedTransaction)
await this.redisService.setJson(cacheKey, { data: mappedTransactions, total }, 300)
return ResponseMapper.paginate(mappedTransactions, {
page,
perPage,
total,
const mappedTransactions = transactions.map(this.mappedTransaction)
return {
items: mappedTransactions,
page,
perPage,
total,
}
})
}
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,
partner_id,
},
select: this.defaultSelect,
return await this.redisService.getAndSet(cacheKey, 'single', async () => {
const transaction = await this.prisma.licenseChargeTransaction.findUniqueOrThrow({
where: {
id,
partner_id,
},
select: this.defaultSelect,
})
return 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) {