Skip to content

Commit

Permalink
Adjusted instance retrieval service to take into account event state …
Browse files Browse the repository at this point in the history
…and caching policy
  • Loading branch information
Matt Cavanagh committed Jul 31, 2023
1 parent e81a751 commit 06f7794
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -74,7 +74,7 @@ export class RestInstanceMetagameController {
type: InstanceMetagameTerritoryEntity,
})
@UseInterceptors(ClassSerializerInterceptor)
async findOne(@Param('instance') instanceId: string): Promise<InstanceMetagameTerritoryEntity> {
async findOne(@Param('instance') instanceId: string): Promise<InstanceMetagameTerritoryEntity | ObjectLiteral> {
return await this.instanceRetrievalService.findOne(instanceId);
}

Expand Down
27 changes: 19 additions & 8 deletions src/services/instance.retrieval.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -11,16 +13,25 @@ export default class InstanceRetrievalService {
private readonly cacheService: RedisCacheService,
) {}

public async findOne(instanceId: string): Promise<InstanceMetagameTerritoryEntity> {
public async findOne(instanceId: string): Promise<InstanceMetagameTerritoryEntity | ObjectLiteral> {
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);
}
}
}

0 comments on commit 06f7794

Please sign in to comment.