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
@@ -17,14 +17,6 @@ export class PartnerActivatedLicensesService {
) {}
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: {
@@ -32,69 +24,70 @@ export class PartnerActivatedLicensesService {
},
},
}
const [activatedLicense, total] = await this.prisma.$transaction(async tx => [
await tx.licenseActivation.findMany({
where: defaultWhere,
skip: (page - 1) * perPage,
take: perPage,
select: {
id: true,
starts_at: true,
expires_at: true,
created_at: true,
account_allocations: {
select: {
id: true,
account_id: true,
const cacheKey = RedisKeyMaker.partnerActivatedLicensesList(partner_id, page, perPage)
return await this.redisService.getAndSet(cacheKey, 'paginate', async () => {
const [activatedLicense, total] = await this.prisma.$transaction(async tx => [
await tx.licenseActivation.findMany({
where: defaultWhere,
skip: (page - 1) * perPage,
take: perPage,
select: {
id: true,
starts_at: true,
expires_at: true,
created_at: true,
account_allocations: {
select: {
id: true,
account_id: true,
},
},
},
business_activity: {
select: {
id: true,
name: true,
consumer: {
select: {
id: true,
...QUERY_CONSTANTS.CONSUMER.infoSelect,
business_activity: {
select: {
id: true,
name: true,
consumer: {
select: {
id: true,
...QUERY_CONSTANTS.CONSUMER.infoSelect,
},
},
},
},
},
},
}),
await tx.licenseActivation.count({
where: defaultWhere,
}),
])
}),
await tx.licenseActivation.count({
where: defaultWhere,
}),
])
const mappedLicenses = activatedLicense.map(license => {
const { account_allocations, business_activity, ...rest } = license
const { consumer, ...businessActivityRest } = business_activity
const mappedLicenses = activatedLicense.map(license => {
const { account_allocations, business_activity, ...rest } = license
const { consumer, ...businessActivityRest } = business_activity
const mappedConsumer = consumer_mappersUtil(consumer)
const mappedConsumer = consumer_mappersUtil(consumer)
return {
...rest,
license: {
accounts_limit: account_allocations.length,
allocated_account_count: account_allocations.filter(
allocation => allocation.account_id !== null,
).length,
},
business_activity: {
...business_activity,
consumer_name: mappedConsumer.name,
},
}
})
return {
...rest,
license: {
accounts_limit: account_allocations.length,
allocated_account_count: account_allocations.filter(
allocation => allocation.account_id !== null,
).length,
},
business_activity: {
...business_activity,
consumer_name: mappedConsumer.name,
},
items: mappedLicenses,
page,
perPage,
total,
}
})
await this.redisService.setJson(cacheKey, { data: mappedLicenses, total }, 300)
return ResponseMapper.paginate(mappedLicenses, {
page,
perPage,
total,
})
}
// async update(partnerId: string, id: string, data: UpdateLicenseDto) {
@@ -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) {
+12 -25
View File
@@ -143,39 +143,26 @@ export class PartnersService {
async findAll() {
const cacheKey = RedisKeyMaker.partnersList()
const cached = await this.redisService.getJson<unknown[]>(cacheKey)
if (cached) {
console.log('cached', cached)
return await this.redisService.getAndSet(cacheKey, 'list', async () => {
const partners = await this.prisma.partner.findMany({
select: this.defaultSelect,
})
return ResponseMapper.list(cached)
}
const partners = await this.prisma.partner.findMany({
select: this.defaultSelect,
return partners.map(this.mapPartner)
})
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 await this.redisService.getAndSet(cacheKey, 'single', async () => {
const partner = await this.prisma.partner.findUniqueOrThrow({
where: { id },
select: this.defaultSelect,
})
return 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) {