cleanup caching
All checks were successful
Deploy Backend / deploy (push) Successful in 4m0s
Deploy Website / deploy (push) Successful in 7m9s

This commit is contained in:
Lee 2024-10-19 04:10:44 +01:00
parent 238ec6e254
commit 7421c47959
9 changed files with 153 additions and 121 deletions

BIN
bun.lockb

Binary file not shown.

@ -0,0 +1,30 @@
import { SSRCache } from "@ssr/common/cache";
import { InternalServerError } from "../error/internal-server-error";
/**
* Fetches data with caching.
*
* @param cache the cache to fetch from
* @param cacheKey The key used for caching.
* @param fetchFn The function to fetch data if it's not in cache.
*/
export async function fetchWithCache<T>(
cache: SSRCache,
cacheKey: string,
fetchFn: () => Promise<T | undefined>
): Promise<T | undefined> {
if (cache == undefined) {
throw new InternalServerError(`Cache is not defined`);
}
if (cache.has(cacheKey)) {
return cache.get<T>(cacheKey);
}
const data = await fetchFn();
if (data) {
cache.set(cacheKey, data);
}
return data;
}

@ -1,5 +1,5 @@
import { Controller, Get } from "elysia-decorators";
import { getAppVersion } from "../common/app-utils";
import { getAppVersion } from "../common/app.util";
import { AppService } from "../service/app.service";
@Controller()

@ -5,14 +5,17 @@ import { formatNumberWithCommas, formatPp } from "@ssr/common/utils/number-utils
import { getDifficultyFromScoreSaberDifficulty } from "@ssr/common/utils/scoresaber-utils";
import { StarIcon } from "../../components/star-icon";
import { GlobeIcon } from "../../components/globe-icon";
import NodeCache from "node-cache";
import ScoreSaberLeaderboardToken from "@ssr/common/types/token/scoresaber/score-saber-leaderboard-token";
import ScoreSaberPlayer, { getScoreSaberPlayerFromToken } from "@ssr/common/player/impl/scoresaber-player";
import { Jimp } from "jimp";
import { extractColors } from "extract-colors";
import { Config } from "@ssr/common/config";
import { fetchWithCache } from "../common/cache.util";
import { SSRCache } from "@ssr/common/cache";
const cache = new NodeCache({ stdTTL: 60 * 60, checkperiod: 120 });
const cache = new SSRCache({
ttl: 1000 * 60 * 60, // 1 hour
});
const imageOptions = { width: 1200, height: 630 };
export class ImageService {
@ -26,7 +29,7 @@ export class ImageService {
public static async getAverageImageColor(src: string): Promise<{ color: string } | undefined> {
src = decodeURIComponent(src);
return await this.fetchWithCache<{ color: string }>(`average_color-${src}`, async () => {
return await fetchWithCache<{ color: string }>(cache, `average_color-${src}`, async () => {
try {
const image = await Jimp.read(src); // Load image using Jimp
const { width, height, data } = image.bitmap; // Access image dimensions and pixel data
@ -54,28 +57,6 @@ export class ImageService {
});
}
/**
* Fetches data with caching.
*
* @param cacheKey The key used for caching.
* @param fetchFn The function to fetch data if it's not in cache.
*/
private static async fetchWithCache<T>(
cacheKey: string,
fetchFn: () => Promise<T | undefined>
): Promise<T | undefined> {
if (cache.has(cacheKey)) {
return cache.get<T>(cacheKey);
}
const data = await fetchFn();
if (data) {
cache.set(cacheKey, data);
}
return data;
}
/**
* The base of the OpenGraph image
*
@ -120,7 +101,7 @@ export class ImageService {
* @param id the player's id
*/
public static async generatePlayerImage(id: string) {
const player = await this.fetchWithCache<ScoreSaberPlayer>(`player-${id}`, async () => {
const player = await fetchWithCache<ScoreSaberPlayer>(cache, `player-${id}`, async () => {
const token = await scoresaberService.lookupPlayer(id);
return token ? await getScoreSaberPlayerFromToken(token, Config.apiUrl) : undefined;
});
@ -194,7 +175,7 @@ export class ImageService {
* @param id the leaderboard's id
*/
public static async generateLeaderboardImage(id: string) {
const leaderboard = await this.fetchWithCache<ScoreSaberLeaderboardToken>(`leaderboard-${id}`, () =>
const leaderboard = await fetchWithCache<ScoreSaberLeaderboardToken>(cache, `leaderboard-${id}`, () =>
scoresaberService.lookupLeaderboard(id)
);
if (!leaderboard) {

@ -21,6 +21,16 @@ import PlayerScoresResponse from "@ssr/common/response/player-scores-response";
import { DiscordChannels, logToChannel } from "../bot/bot";
import { EmbedBuilder } from "discord.js";
import { Config } from "@ssr/common/config";
import { SSRCache } from "@ssr/common/cache";
import { fetchWithCache } from "../common/cache.util";
const playerScoresCache = new SSRCache({
ttl: 1000 * 60, // 1 minute
});
const leaderboardScoresCache = new SSRCache({
ttl: 1000 * 60, // 1 minute
});
export class ScoreService {
/**
@ -116,7 +126,11 @@ export class ScoreService {
page: number,
sort: string,
search?: string
): Promise<PlayerScoresResponse<unknown, unknown>> {
): Promise<PlayerScoresResponse<unknown, unknown> | undefined> {
return fetchWithCache(
playerScoresCache,
`player-scores-${leaderboardName}-${id}-${page}-${sort}-${search}`,
async () => {
const scores: PlayerScore<unknown, unknown>[] | undefined = [];
let beatSaverMap: BeatSaverMap | undefined;
let metadata: Metadata = new Metadata(0, 0, 0, 0); // Default values
@ -169,6 +183,8 @@ export class ScoreService {
metadata: metadata,
};
}
);
}
/**
* Gets scores for a leaderboard.
@ -182,7 +198,8 @@ export class ScoreService {
leaderboardName: Leaderboards,
id: string,
page: number
): Promise<LeaderboardScoresResponse<unknown, unknown>> {
): Promise<LeaderboardScoresResponse<unknown, unknown> | undefined> {
return fetchWithCache(leaderboardScoresCache, `leaderboard-scores-${leaderboardName}-${id}-${page}`, async () => {
const scores: Score[] = [];
let leaderboard: Leaderboard | undefined;
let beatSaverMap: BeatSaverMap | undefined;
@ -229,5 +246,6 @@ export class ScoreService {
beatSaver: beatSaverMap,
metadata: metadata,
};
});
}
}

@ -38,7 +38,6 @@
"next": "15.0.0-rc.1",
"next-build-id": "^3.0.0",
"next-themes": "^0.3.0",
"node-cache": "^5.1.2",
"react": "18.3.1",
"react-chartjs-2": "^5.2.0",
"react-countup": "^6.5.3",

@ -3,7 +3,6 @@ import { redirect } from "next/navigation";
import { Colors } from "@/common/colors";
import { getAverageColor } from "@/common/image-utils";
import { LeaderboardData } from "@/components/leaderboard/leaderboard-data";
import NodeCache from "node-cache";
import { Config } from "@ssr/common/config";
import ScoreSaberScore from "@ssr/common/score/impl/scoresaber-score";
import { LeaderboardResponse } from "@ssr/common/response/leaderboard-response";
@ -11,6 +10,7 @@ import ScoreSaberLeaderboard from "@ssr/common/leaderboard/impl/scoresaber-leade
import { fetchLeaderboard } from "@ssr/common/utils/leaderboard.util";
import { fetchLeaderboardScores } from "@ssr/common/utils/score-utils";
import LeaderboardScoresResponse from "@ssr/common/response/leaderboard-scores-response";
import { SSRCache } from "@ssr/common/cache";
const UNKNOWN_LEADERBOARD = {
title: "ScoreSaber Reloaded - Unknown Leaderboard",
@ -32,7 +32,9 @@ type LeaderboardData = {
page: number;
};
const leaderboardCache = new NodeCache({ stdTTL: 60, checkperiod: 120 });
const leaderboardCache = new SSRCache({
ttl: 1000 * 60, // 1 minute
});
/**
* Gets the leaderboard data and scores

@ -4,7 +4,6 @@ import { redirect } from "next/navigation";
import { Colors } from "@/common/colors";
import { getAverageColor } from "@/common/image-utils";
import { scoresaberService } from "@ssr/common/service/impl/scoresaber";
import NodeCache from "node-cache";
import { getCookieValue } from "@ssr/common/utils/cookie-utils";
import { Config } from "@ssr/common/config";
import ScoreSaberPlayer, { getScoreSaberPlayerFromToken } from "@ssr/common/player/impl/scoresaber-player";
@ -13,6 +12,7 @@ import ScoreSaberScore from "@ssr/common/score/impl/scoresaber-score";
import ScoreSaberLeaderboard from "@ssr/common/leaderboard/impl/scoresaber-leaderboard";
import { fetchPlayerScores } from "@ssr/common/utils/score-utils";
import PlayerScoresResponse from "@ssr/common/response/player-scores-response";
import { SSRCache } from "@ssr/common/cache";
const UNKNOWN_PLAYER = {
title: "ScoreSaber Reloaded - Unknown Player",
@ -36,7 +36,9 @@ type PlayerData = {
search: string;
};
const playerCache = new NodeCache({ stdTTL: 60, checkperiod: 120 });
const playerCache = new SSRCache({
ttl: 1000 * 60, // 1 minute
});
/**
* Gets the player data and scores