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
@@ -64,60 +64,42 @@ export class PartnerConsumersService {
async findAll(partner_id: string, page = 1, perPage = 10) {
const cacheKey = RedisKeyMaker.partnerConsumersList(partner_id, page, perPage)
const cached = await this.redisService.getJson<{ items: unknown[]; total: number }>(
cacheKey,
)
if (cached) {
return ResponseMapper.paginate(cached.items, { total: cached.total, page, perPage })
}
return await this.redisService.getAndSet(cacheKey, 'paginate', async () => {
const [consumers, total] = await this.prisma.$transaction(async tx => [
await tx.consumer.findMany({
where: this.defaultWhere(partner_id),
select: this.defaultSelect,
skip: (page - 1) * perPage,
take: 10,
}),
await tx.consumer.count({
where: this.defaultWhere(partner_id),
}),
])
const mappedConsumers = consumers.map(mapConsumerWithLicenseUtil)
const [consumers, total] = await this.prisma.$transaction(async tx => [
await tx.consumer.findMany({
where: this.defaultWhere(partner_id),
select: this.defaultSelect,
skip: (page - 1) * perPage,
take: 10,
}),
await tx.consumer.count({
where: this.defaultWhere(partner_id),
}),
])
const mappedItems = consumers.map(mapConsumerWithLicenseUtil)
await this.redisService.setJson(
cacheKey,
{ items: mappedItems, total },
this.infoCacheTtlSeconds,
)
return ResponseMapper.paginate(mappedItems, {
total,
page,
perPage,
return {
items: mappedConsumers,
total,
page,
perPage,
}
})
}
async findOne(partner_id: string, consumer_id: string) {
const cacheKey = RedisKeyMaker.partnerConsumerInfo(partner_id, consumer_id)
const cached = await this.redisService.getJson<unknown>(cacheKey)
if (cached) {
return ResponseMapper.single(cached)
}
return await this.redisService.getAndSet(cacheKey, 'single', async () => {
const consumer = await this.prisma.consumer.findUniqueOrThrow({
where: {
...(this.defaultWhere(partner_id) as any),
id: consumer_id,
},
select: this.defaultSelect,
})
const consumer = await this.prisma.consumer.findUniqueOrThrow({
where: {
...(this.defaultWhere(partner_id) as any),
id: consumer_id,
},
select: this.defaultSelect,
return mapConsumerWithLicenseUtil(consumer)
})
const mappedConsumer = mapConsumerWithLicenseUtil(consumer)
await this.redisService.setJson(
RedisKeyMaker.consumerInfo(consumer_id),
mappedConsumer,
this.infoCacheTtlSeconds,
)
await this.redisService.setJson(cacheKey, mappedConsumer, this.infoCacheTtlSeconds)
return ResponseMapper.single(mappedConsumer)
}
async create(partner_id: string, data: CreateConsumerDto) {