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:
@@ -61,34 +61,23 @@ export class BusinessActivitiesService {
|
||||
|
||||
async findAll(consumer_id: string) {
|
||||
const cacheKey = RedisKeyMaker.consumerBusinessActivitiesList(consumer_id)
|
||||
const cached = await this.redisService.getJson<unknown[]>(cacheKey)
|
||||
if (cached) {
|
||||
return ResponseMapper.list(cached)
|
||||
}
|
||||
|
||||
const businessActivities =
|
||||
await this.businessActivitiesQueryService.findAllByConsumer(
|
||||
return await this.redisService.getAndSet(cacheKey, 'list', async () => {
|
||||
return await this.businessActivitiesQueryService.findAllByConsumer(
|
||||
consumer_id,
|
||||
this.defaultSelect,
|
||||
)
|
||||
await this.redisService.setJson(cacheKey, businessActivities, this.cacheTtlSeconds)
|
||||
return ResponseMapper.list(businessActivities)
|
||||
})
|
||||
}
|
||||
|
||||
async findOne(consumer_id: string, id: string) {
|
||||
const cacheKey = RedisKeyMaker.consumerBusinessActivityInfo(consumer_id, id)
|
||||
const cached = await this.redisService.getJson<unknown>(cacheKey)
|
||||
if (cached) {
|
||||
return ResponseMapper.single(cached)
|
||||
}
|
||||
|
||||
const businessActivity = await this.businessActivitiesQueryService.findOneByConsumer(
|
||||
consumer_id,
|
||||
id,
|
||||
this.defaultSelect,
|
||||
)
|
||||
await this.redisService.setJson(cacheKey, businessActivity, this.cacheTtlSeconds)
|
||||
return ResponseMapper.single(businessActivity)
|
||||
return await this.redisService.getAndSet(cacheKey, 'list', async () => {
|
||||
return await this.businessActivitiesQueryService.findOneByConsumer(
|
||||
consumer_id,
|
||||
id,
|
||||
this.defaultSelect,
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
async update(consumer_id: string, id: string, data: any) {
|
||||
|
||||
Reference in New Issue
Block a user