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:
@@ -52,47 +52,44 @@ export class GoodsService {
|
||||
guild_id: string,
|
||||
consumer_account_id: string,
|
||||
) {
|
||||
const cacheKey = RedisKeyMaker.posGoodsList(guild_id, business_activity_id)
|
||||
try {
|
||||
const cached = await this.redisService.getJson<unknown[]>(cacheKey)
|
||||
if (cached) {
|
||||
return ResponseMapper.list(cached)
|
||||
}
|
||||
throw new Error('Cache miss')
|
||||
} catch (error) {
|
||||
const goods = await this.prisma.good.findMany({
|
||||
where: {
|
||||
OR: [
|
||||
{
|
||||
is_default_guild_good: true,
|
||||
category: {
|
||||
guild_id,
|
||||
const cacheKey = RedisKeyMaker.posGoodsList(business_activity_id, guild_id)
|
||||
return this.redisService.getAndSet(
|
||||
cacheKey,
|
||||
'list',
|
||||
async () => {
|
||||
const goods = await this.prisma.good.findMany({
|
||||
where: {
|
||||
OR: [
|
||||
{
|
||||
is_default_guild_good: true,
|
||||
category: {
|
||||
guild_id,
|
||||
},
|
||||
},
|
||||
{
|
||||
business_activity_id,
|
||||
},
|
||||
],
|
||||
},
|
||||
select: {
|
||||
...this.defaultSelect,
|
||||
consumer_account_good_favorites: {
|
||||
where: {
|
||||
consumer_account_id,
|
||||
},
|
||||
},
|
||||
{
|
||||
business_activity_id,
|
||||
},
|
||||
],
|
||||
},
|
||||
select: {
|
||||
...this.defaultSelect,
|
||||
consumer_account_good_favorites: {
|
||||
where: {
|
||||
consumer_account_id,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
const mappedGoods = goods.map(good => ({
|
||||
...good,
|
||||
is_favorite: good.consumer_account_good_favorites.length > 0,
|
||||
}))
|
||||
const mappedGoods = goods.map(good => ({
|
||||
...good,
|
||||
is_favorite: good.consumer_account_good_favorites.length > 0,
|
||||
}))
|
||||
|
||||
await this.redisService.setJson(cacheKey, mappedGoods, this.listCacheTtlSeconds)
|
||||
|
||||
return ResponseMapper.list(mappedGoods)
|
||||
}
|
||||
return mappedGoods
|
||||
},
|
||||
this.listCacheTtlSeconds,
|
||||
)
|
||||
}
|
||||
|
||||
async findOne(good_id: string, business_activity_id: string, guild_id: string) {
|
||||
|
||||
Reference in New Issue
Block a user