Add cache messages

This commit is contained in:
Liam 2022-10-21 14:51:29 +01:00
parent c0cac09a4d
commit a35ec28ecb
4 changed files with 37 additions and 2 deletions

@ -28,6 +28,7 @@ export default async function handler(req, res) {
}); });
} }
const before = Date.now();
const data = await fetch( const data = await fetch(
WebsiteTypes.BeatLeader.ApiUrl.MapData.replace("%h", mapHash) WebsiteTypes.BeatLeader.ApiUrl.MapData.replace("%h", mapHash)
.replace("%d", difficulty) .replace("%d", difficulty)
@ -45,10 +46,30 @@ export default async function handler(req, res) {
}); });
} }
const json = await data.json(); const json = await data.json();
RedisUtils.setValue(key, json.difficulty.stars); let starCount = undefined;
for (const diff of json.difficulties) {
if (
diff.difficultyName === difficulty &&
diff.modeName === characteristic
) {
starCount = diff.stars;
}
}
if (starCount === undefined) {
return res.status(404).json({
status: 404,
message: "Unknown Map Hash",
});
}
await RedisUtils.setValue(key, starCount);
console.log(
`[Cache]: Cached BL Star Count for hash ${mapHash} in ${
Date.now() - before
}ms`
);
res.setHeader("Cache-Status", "miss"); res.setHeader("Cache-Status", "miss");
return res.status(200).json({ return res.status(200).json({
status: "OK", status: "OK",
stars: json.difficulty.stars, stars: starCount,
}); });
} }

@ -26,6 +26,7 @@ export default async function handler(req, res) {
return res.end(buffer); return res.end(buffer);
} }
const before = Date.now();
const data = await fetch(`https://eu.cdn.beatsaver.com/${mapHash}.${ext}`); const data = await fetch(`https://eu.cdn.beatsaver.com/${mapHash}.${ext}`);
if (data.status === 404) { if (data.status === 404) {
return res.status(404).json({ return res.status(404).json({
@ -39,6 +40,11 @@ export default async function handler(req, res) {
const bytes = buffer.toString("base64"); const bytes = buffer.toString("base64");
await RedisUtils.setValue(`${KEY}${mapHash}`.replace(" ", ""), bytes); await RedisUtils.setValue(`${KEY}${mapHash}`.replace(" ", ""), bytes);
console.log(
`[Cache]: Cached BS Song Art for hash ${mapHash} in ${
Date.now() - before
}ms`
);
res.setHeader("Cache-Status", "miss"); res.setHeader("Cache-Status", "miss");
res.setHeader("Content-Type", "image/" + ext); res.setHeader("Content-Type", "image/" + ext);
res.status(200).send(buffer); res.status(200).send(buffer);

@ -34,6 +34,7 @@ export default async function handler(req, res) {
return res.end(buffer); return res.end(buffer);
} }
const before = Date.now();
const data = await fetch( const data = await fetch(
`https://cdn.scoresaber.com/avatars/${steamId}.${ext}` `https://cdn.scoresaber.com/avatars/${steamId}.${ext}`
); );
@ -49,6 +50,9 @@ export default async function handler(req, res) {
const bytes = buffer.toString("base64"); const bytes = buffer.toString("base64");
await RedisUtils.setValue(`${KEY}${steamId}`, bytes); await RedisUtils.setValue(`${KEY}${steamId}`, bytes);
console.log(
`[Cache]: Cached Avatar for id ${steamId} in ${Date.now() - before}ms`
);
res.setHeader("Cache-Status", "miss"); res.setHeader("Cache-Status", "miss");
res.setHeader("Content-Type", "image/" + ext); res.setHeader("Content-Type", "image/" + ext);
res.status(200).send(buffer); res.status(200).send(buffer);

@ -22,6 +22,7 @@ async function isValidSteamId(steamId) {
return Boolean(data); return Boolean(data);
} }
const before = Date.now();
let valid = false; let valid = false;
for (const url of TO_CHECK) { for (const url of TO_CHECK) {
const isValid = await Utils.checkLeaderboard(url, steamId); const isValid = await Utils.checkLeaderboard(url, steamId);
@ -32,6 +33,9 @@ async function isValidSteamId(steamId) {
} }
await RedisUtils.setValue(`${KEY}${steamId}`, valid); await RedisUtils.setValue(`${KEY}${steamId}`, valid);
console.log(
`[Cache]: Cached Steam ID for id ${steamId} in ${Date.now() - before}ms`
);
return valid; return valid;
} }