From c3a75b139a89eef8f231f38bdaeb13b61a194b95 Mon Sep 17 00:00:00 2001 From: Liam Date: Sat, 26 Oct 2024 15:33:05 +0100 Subject: [PATCH] minimize api calls to scoresaber --- .../common/src/service/impl/scoresaber.ts | 2 +- .../src/app/(pages)/player/[...slug]/page.tsx | 2 +- .../website/src/common/database/database.ts | 22 +++++++++++++++++-- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/projects/common/src/service/impl/scoresaber.ts b/projects/common/src/service/impl/scoresaber.ts index b845049..0896026 100644 --- a/projects/common/src/service/impl/scoresaber.ts +++ b/projects/common/src/service/impl/scoresaber.ts @@ -30,7 +30,7 @@ const LOOKUP_LEADERBOARD_SCORES_ENDPOINT = `${API_BASE}/leaderboard/by-id/:id/sc const STAR_MULTIPLIER = 42.117208413; const playerCache = new SSRCache({ - ttl: 60 * 30, // 30 minutes + ttl: 60, // 1 minute }); class ScoreSaberService extends Service { diff --git a/projects/website/src/app/(pages)/player/[...slug]/page.tsx b/projects/website/src/app/(pages)/player/[...slug]/page.tsx index 16af7b4..81da0cc 100644 --- a/projects/website/src/app/(pages)/player/[...slug]/page.tsx +++ b/projects/website/src/app/(pages)/player/[...slug]/page.tsx @@ -38,7 +38,7 @@ type PlayerData = { }; const getPlayer = cache(async (id: string): Promise => { - const playerToken = await scoresaberService.lookupPlayer(id); + const playerToken = await scoresaberService.lookupPlayer(id, true); return playerToken && (await getScoreSaberPlayerFromToken(playerToken, await getCookieValue("playerId"))); }); diff --git a/projects/website/src/common/database/database.ts b/projects/website/src/common/database/database.ts index 58fb6eb..de929ae 100644 --- a/projects/website/src/common/database/database.ts +++ b/projects/website/src/common/database/database.ts @@ -5,9 +5,14 @@ import ScoreSaberPlayerToken from "@ssr/common/types/token/scoresaber/score-sabe import { scoresaberService } from "@ssr/common/service/impl/scoresaber"; import { setCookieValue } from "@ssr/common/utils/cookie-utils"; import { trackPlayer } from "@ssr/common/utils/player-utils"; +import { SSRCache } from "@ssr/common/cache"; const SETTINGS_ID = "SSR"; // DO NOT CHANGE +const playerCache = new SSRCache({ + ttl: 60 * 30, // 30 minutes +}); + export default class Database extends Dexie { /** * The settings for the website. @@ -79,7 +84,15 @@ export default class Database extends Dexie { if (settings == undefined || settings.playerId == undefined) { return; } - return scoresaberService.lookupPlayer(settings.playerId, true); + if (playerCache.has(settings.playerId)) { + return playerCache.get(settings.playerId); + } + const player = scoresaberService.lookupPlayer(settings.playerId); + if (player == undefined) { + return undefined; + } + playerCache.set(settings.playerId, player); + return player; } /** @@ -119,10 +132,15 @@ export default class Database extends Dexie { const friends = await this.friends.toArray(); const players = await Promise.all( friends.map(async ({ id }) => { - const token = await scoresaberService.lookupPlayer(id, true); + if (playerCache.has(id)) { + return playerCache.get(id); + } + + const token = await scoresaberService.lookupPlayer(id); if (token == undefined) { return undefined; } + playerCache.set(id, token); await trackPlayer(id); // Track the player return token; })