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:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user