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) {
|
||||
|
||||
@@ -21,25 +21,21 @@ export class ConsumerService {
|
||||
|
||||
async getInfo(consumer_id: string) {
|
||||
const cacheKey = RedisKeyMaker.consumerInfo(consumer_id)
|
||||
const cached = await this.redisService.getJson<unknown>(cacheKey)
|
||||
if (cached) {
|
||||
return ResponseMapper.single(cached)
|
||||
}
|
||||
|
||||
const consumer = await this.prisma.consumer.findUniqueOrThrow({
|
||||
where: {
|
||||
id: consumer_id,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
status: true,
|
||||
...QUERY_CONSTANTS.CONSUMER.infoSelect,
|
||||
},
|
||||
return await this.redisService.getAndSet(cacheKey, 'single', async () => {
|
||||
const consumer = await this.prisma.consumer.findUniqueOrThrow({
|
||||
where: {
|
||||
id: consumer_id,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
status: true,
|
||||
...QUERY_CONSTANTS.CONSUMER.infoSelect,
|
||||
},
|
||||
})
|
||||
|
||||
return consumer_mappersUtil(consumer)
|
||||
})
|
||||
|
||||
const mappedConsumer = consumer_mappersUtil(consumer)
|
||||
await this.redisService.setJson(cacheKey, mappedConsumer, this.infoCacheTtlSeconds)
|
||||
return ResponseMapper.single(mappedConsumer)
|
||||
}
|
||||
|
||||
async updateInfo(consumer_id: string, data: UpdateConsumerInfoDto) {
|
||||
|
||||
Reference in New Issue
Block a user