This commit is contained in:
parent
a5e00e4850
commit
9355f53ee5
BIN
bun.lockb
BIN
bun.lockb
Binary file not shown.
@ -22,6 +22,7 @@
|
|||||||
"elysia-rate-limit": "^4.1.0",
|
"elysia-rate-limit": "^4.1.0",
|
||||||
"ky": "^1.7.2",
|
"ky": "^1.7.2",
|
||||||
"mongoose": "^8.7.0",
|
"mongoose": "^8.7.0",
|
||||||
|
"node-cache": "^5.1.2",
|
||||||
"react": "^18.3.1"
|
"react": "^18.3.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
@ -3,6 +3,12 @@ import { scoresaberService } from "@ssr/common/service/impl/scoresaber";
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import { formatNumberWithCommas, formatPp } from "@ssr/common/utils/number-utils";
|
import { formatNumberWithCommas, formatPp } from "@ssr/common/utils/number-utils";
|
||||||
import { getDifficultyFromScoreSaberDifficulty } from "website/src/common/scoresaber-utils";
|
import { getDifficultyFromScoreSaberDifficulty } from "website/src/common/scoresaber-utils";
|
||||||
|
import NodeCache from "node-cache";
|
||||||
|
|
||||||
|
const imageCache = new NodeCache({
|
||||||
|
stdTTL: 60 * 60, // 1 hour
|
||||||
|
checkperiod: 120,
|
||||||
|
});
|
||||||
|
|
||||||
export class ImageService {
|
export class ImageService {
|
||||||
/**
|
/**
|
||||||
@ -11,11 +17,16 @@ export class ImageService {
|
|||||||
* @param id the player's id
|
* @param id the player's id
|
||||||
*/
|
*/
|
||||||
public static async generatePlayerImage(id: string) {
|
public static async generatePlayerImage(id: string) {
|
||||||
|
const cacheKey = `player-${id}`;
|
||||||
|
if (imageCache.has(cacheKey)) {
|
||||||
|
return imageCache.get(cacheKey) as ImageResponse;
|
||||||
|
}
|
||||||
|
|
||||||
const player = await scoresaberService.lookupPlayer(id);
|
const player = await scoresaberService.lookupPlayer(id);
|
||||||
if (player == undefined) {
|
if (player == undefined) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
return new ImageResponse(
|
const imageResponse = new ImageResponse(
|
||||||
(
|
(
|
||||||
<div
|
<div
|
||||||
tw="w-full h-full flex flex-col text-white text-3xl p-3 justify-center items-center"
|
tw="w-full h-full flex flex-col text-white text-3xl p-3 justify-center items-center"
|
||||||
@ -67,6 +78,8 @@ export class ImageService {
|
|||||||
emoji: "twemoji",
|
emoji: "twemoji",
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
imageCache.set(cacheKey, imageResponse);
|
||||||
|
return imageResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -75,14 +88,18 @@ export class ImageService {
|
|||||||
* @param id the player's id
|
* @param id the player's id
|
||||||
*/
|
*/
|
||||||
public static async generateLeaderboardImage(id: string) {
|
public static async generateLeaderboardImage(id: string) {
|
||||||
|
const cacheKey = `leaderboard-${id}`;
|
||||||
|
if (imageCache.has(cacheKey)) {
|
||||||
|
return imageCache.get(cacheKey) as ImageResponse;
|
||||||
|
}
|
||||||
|
|
||||||
const leaderboard = await scoresaberService.lookupLeaderboard(id);
|
const leaderboard = await scoresaberService.lookupLeaderboard(id);
|
||||||
if (leaderboard == undefined) {
|
if (leaderboard == undefined) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
const ranked = leaderboard.stars > 0;
|
const ranked = leaderboard.stars > 0;
|
||||||
|
const imageResponse = new ImageResponse(
|
||||||
return new ImageResponse(
|
|
||||||
(
|
(
|
||||||
<div
|
<div
|
||||||
tw="w-full h-full flex flex-col text-white text-3xl p-3 justify-center items-center"
|
tw="w-full h-full flex flex-col text-white text-3xl p-3 justify-center items-center"
|
||||||
@ -130,5 +147,8 @@ export class ImageService {
|
|||||||
emoji: "twemoji",
|
emoji: "twemoji",
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
imageCache.set(cacheKey, imageResponse);
|
||||||
|
return imageResponse;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user