minimize api calls to scoresaber
All checks were successful
Deploy Backend / docker (ubuntu-latest) (push) Successful in 47s
Deploy Website / docker (ubuntu-latest) (push) Successful in 2m24s

This commit is contained in:
Lee 2024-10-26 15:33:05 +01:00
parent ba80b9623b
commit c3a75b139a
3 changed files with 22 additions and 4 deletions

@ -30,7 +30,7 @@ const LOOKUP_LEADERBOARD_SCORES_ENDPOINT = `${API_BASE}/leaderboard/by-id/:id/sc
const STAR_MULTIPLIER = 42.117208413; const STAR_MULTIPLIER = 42.117208413;
const playerCache = new SSRCache({ const playerCache = new SSRCache({
ttl: 60 * 30, // 30 minutes ttl: 60, // 1 minute
}); });
class ScoreSaberService extends Service { class ScoreSaberService extends Service {

@ -38,7 +38,7 @@ type PlayerData = {
}; };
const getPlayer = cache(async (id: string): Promise<ScoreSaberPlayer | undefined> => { const getPlayer = cache(async (id: string): Promise<ScoreSaberPlayer | undefined> => {
const playerToken = await scoresaberService.lookupPlayer(id); const playerToken = await scoresaberService.lookupPlayer(id, true);
return playerToken && (await getScoreSaberPlayerFromToken(playerToken, await getCookieValue("playerId"))); return playerToken && (await getScoreSaberPlayerFromToken(playerToken, await getCookieValue("playerId")));
}); });

@ -5,9 +5,14 @@ import ScoreSaberPlayerToken from "@ssr/common/types/token/scoresaber/score-sabe
import { scoresaberService } from "@ssr/common/service/impl/scoresaber"; import { scoresaberService } from "@ssr/common/service/impl/scoresaber";
import { setCookieValue } from "@ssr/common/utils/cookie-utils"; import { setCookieValue } from "@ssr/common/utils/cookie-utils";
import { trackPlayer } from "@ssr/common/utils/player-utils"; import { trackPlayer } from "@ssr/common/utils/player-utils";
import { SSRCache } from "@ssr/common/cache";
const SETTINGS_ID = "SSR"; // DO NOT CHANGE const SETTINGS_ID = "SSR"; // DO NOT CHANGE
const playerCache = new SSRCache({
ttl: 60 * 30, // 30 minutes
});
export default class Database extends Dexie { export default class Database extends Dexie {
/** /**
* The settings for the website. * The settings for the website.
@ -79,7 +84,15 @@ export default class Database extends Dexie {
if (settings == undefined || settings.playerId == undefined) { if (settings == undefined || settings.playerId == undefined) {
return; 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 friends = await this.friends.toArray();
const players = await Promise.all( const players = await Promise.all(
friends.map(async ({ id }) => { 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) { if (token == undefined) {
return undefined; return undefined;
} }
playerCache.set(id, token);
await trackPlayer(id); // Track the player await trackPlayer(id); // Track the player
return token; return token;
}) })