diff --git a/projects/backend/src/service/app.service.ts b/projects/backend/src/service/app.service.ts index 83387f4..169ed92 100644 --- a/projects/backend/src/service/app.service.ts +++ b/projects/backend/src/service/app.service.ts @@ -4,24 +4,35 @@ import { ScoreSaberScoreModel } from "@ssr/common/model/score/impl/scoresaber-sc import { AdditionalScoreDataModel } from "@ssr/common/model/additional-score-data/additional-score-data"; import { BeatSaverMapModel } from "@ssr/common/model/beatsaver/map"; import { ScoreSaberLeaderboardModel } from "@ssr/common/model/leaderboard/impl/scoresaber-leaderboard"; +import { SSRCache } from "@ssr/common/cache"; + +const statisticsCache = new SSRCache({ + ttl: 120 * 1000, // 2 minutes +}); export class AppService { /** * Gets the app statistics. */ public static async getAppStatistics(): Promise { + if (statisticsCache.has("app-statistics")) { + return statisticsCache.get("app-statistics")!; + } + const trackedPlayers = await PlayerModel.countDocuments(); const trackedScores = await ScoreSaberScoreModel.countDocuments(); const additionalScoresData = await AdditionalScoreDataModel.countDocuments(); const cachedBeatSaverMaps = await BeatSaverMapModel.countDocuments(); const cachedScoreSaberLeaderboards = await ScoreSaberLeaderboardModel.countDocuments(); - return { + const response = { trackedPlayers, trackedScores, additionalScoresData, cachedBeatSaverMaps, cachedScoreSaberLeaderboards, }; + statisticsCache.set("app-statistics", response); + return response; } } diff --git a/projects/common/src/cache.ts b/projects/common/src/cache.ts index b94de58..cd1d663 100644 --- a/projects/common/src/cache.ts +++ b/projects/common/src/cache.ts @@ -1,6 +1,6 @@ type CacheOptions = { /** - * The time the cached object will be valid for + * The time (in ms) the cached object will be valid for */ ttl?: number;