diff --git a/src/modules/data/ps2alerts-constants b/src/modules/data/ps2alerts-constants index 396dbc2d..4bdfcab5 160000 --- a/src/modules/data/ps2alerts-constants +++ b/src/modules/data/ps2alerts-constants @@ -1 +1 @@ -Subproject commit 396dbc2d58f284d877f7ddb008aa566962ee6794 +Subproject commit 4bdfcab567d392d8f885143b0e7e78e034db5951 diff --git a/src/modules/rest/controllers/rest.instance.metagame.controller.ts b/src/modules/rest/controllers/rest.instance.metagame.controller.ts index 217bdf67..b7c656d4 100644 --- a/src/modules/rest/controllers/rest.instance.metagame.controller.ts +++ b/src/modules/rest/controllers/rest.instance.metagame.controller.ts @@ -37,7 +37,7 @@ import {RedisCacheService} from '../../../services/cache/redis.cache.service'; import {AuthGuard} from '@nestjs/passport'; import {UpdateInstanceMetagameDto} from '../Dto/UpdateInstanceMetagameDto'; import {CreateInstanceMetagameDto} from '../Dto/CreateInstanceMetagameDto'; -import {ObjectID} from 'typeorm'; +import {ObjectID, ObjectLiteral} from 'typeorm'; import {ZONE_IMPLICIT_QUERY} from './common/rest.zone.query'; import InstanceRetrievalService from '../../../services/instance.retrieval.service'; @@ -74,7 +74,7 @@ export class RestInstanceMetagameController { type: InstanceMetagameTerritoryEntity, }) @UseInterceptors(ClassSerializerInterceptor) - async findOne(@Param('instance') instanceId: string): Promise { + async findOne(@Param('instance') instanceId: string): Promise { return await this.instanceRetrievalService.findOne(instanceId); } diff --git a/src/services/instance.retrieval.service.ts b/src/services/instance.retrieval.service.ts index b9e60db3..86607e0a 100644 --- a/src/services/instance.retrieval.service.ts +++ b/src/services/instance.retrieval.service.ts @@ -2,6 +2,8 @@ import {Inject, Injectable} from '@nestjs/common'; import MongoOperationsService from './mongo/mongo.operations.service'; import {RedisCacheService} from './cache/redis.cache.service'; import InstanceMetagameTerritoryEntity from '../modules/data/entities/instance/instance.metagame.territory.entity'; +import {Ps2AlertsEventState} from '../modules/data/ps2alerts-constants/ps2AlertsEventState'; +import {ObjectLiteral} from 'typeorm'; // This service purely grabs the instances out of the database and caches them in a consistent manner. @Injectable() @@ -11,16 +13,25 @@ export default class InstanceRetrievalService { private readonly cacheService: RedisCacheService, ) {} - public async findOne(instanceId: string): Promise { + public async findOne(instanceId: string): Promise { const key = `cache:instances:${instanceId}`; - return await this.cacheService.get(key) ?? await this.cacheService.set( - key, - await this.mongoOperationsService.findOne( - InstanceMetagameTerritoryEntity, - {instanceId}, - ), - 60 * 60 * 24 * 7, + const data = await this.cacheService.get(key); + + if (data) { + return data; + } + + const instance = await this.mongoOperationsService.findOne( + InstanceMetagameTerritoryEntity, + {instanceId}, ); + + // If alert is not complete yet, don't cache it + if (instance.state !== Ps2AlertsEventState.ENDED) { + return instance; + } else { + return await this.cacheService.set(key, instance, 60 * 60 * 24 * 7); + } } }