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
+14 -18
View File
@@ -57,26 +57,22 @@ export class GoodsService {
async findAll(guildId: string) {
const cacheKey = RedisKeyMaker.guildGoodsList(guildId)
const cached = await this.redisService.getJson<{ goods: unknown[]; total: number }>(
return this.redisService.getAndSet(
cacheKey,
'paginate',
async () => {
const [goods, total] = await this.prisma.$transaction([
this.prisma.good.findMany({
where: this.defaultWhere(guildId),
select: this.defaultSelect,
}),
this.prisma.good.count(),
])
return { items: goods, total }
},
this.listCacheTtlSeconds,
)
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),
select: this.defaultSelect,
}),
this.prisma.good.count(),
])
await this.redisService.setJson(cacheKey, { goods, total }, this.listCacheTtlSeconds)
return ResponseMapper.paginate(goods, {
total,
})
}
async findOne(guild_id: string, id: string) {