cache statistics
All checks were successful
Deploy Backend / docker (ubuntu-latest) (push) Successful in 47s
Deploy Website / docker (ubuntu-latest) (push) Successful in 2m20s

This commit is contained in:
Lee 2024-10-27 15:17:54 +00:00
parent c58f24103f
commit b83fb6f3a8
2 changed files with 13 additions and 2 deletions

View File

@ -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<AppStatistics> {
if (statisticsCache.has("app-statistics")) {
return statisticsCache.get<AppStatistics>("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;
}
}

View File

@ -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;