From a35ec28ecbab004be2ccb11ca1acb414a79ab786 Mon Sep 17 00:00:00 2001 From: Liam <67254223+RealFascinated@users.noreply.github.com> Date: Fri, 21 Oct 2022 14:51:29 +0100 Subject: [PATCH] Add cache messages --- pages/api/beatleader/stars.js | 25 +++++++++++++++++++++++-- pages/api/beatsaver/art/[hash].js | 6 ++++++ pages/api/steamavatar.js | 4 ++++ src/helpers/validateSteamId.js | 4 ++++ 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/pages/api/beatleader/stars.js b/pages/api/beatleader/stars.js index 5ae4b5c..56ace1a 100644 --- a/pages/api/beatleader/stars.js +++ b/pages/api/beatleader/stars.js @@ -28,6 +28,7 @@ export default async function handler(req, res) { }); } + const before = Date.now(); const data = await fetch( WebsiteTypes.BeatLeader.ApiUrl.MapData.replace("%h", mapHash) .replace("%d", difficulty) @@ -45,10 +46,30 @@ export default async function handler(req, res) { }); } 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"); return res.status(200).json({ status: "OK", - stars: json.difficulty.stars, + stars: starCount, }); } diff --git a/pages/api/beatsaver/art/[hash].js b/pages/api/beatsaver/art/[hash].js index 0ab0f93..b0ea3c6 100644 --- a/pages/api/beatsaver/art/[hash].js +++ b/pages/api/beatsaver/art/[hash].js @@ -26,6 +26,7 @@ export default async function handler(req, res) { return res.end(buffer); } + const before = Date.now(); const data = await fetch(`https://eu.cdn.beatsaver.com/${mapHash}.${ext}`); if (data.status === 404) { return res.status(404).json({ @@ -39,6 +40,11 @@ export default async function handler(req, res) { const bytes = buffer.toString("base64"); 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("Content-Type", "image/" + ext); res.status(200).send(buffer); diff --git a/pages/api/steamavatar.js b/pages/api/steamavatar.js index fb493eb..16899ff 100644 --- a/pages/api/steamavatar.js +++ b/pages/api/steamavatar.js @@ -34,6 +34,7 @@ export default async function handler(req, res) { return res.end(buffer); } + const before = Date.now(); const data = await fetch( `https://cdn.scoresaber.com/avatars/${steamId}.${ext}` ); @@ -49,6 +50,9 @@ export default async function handler(req, res) { const bytes = buffer.toString("base64"); 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("Content-Type", "image/" + ext); res.status(200).send(buffer); diff --git a/src/helpers/validateSteamId.js b/src/helpers/validateSteamId.js index d05a811..69464c7 100644 --- a/src/helpers/validateSteamId.js +++ b/src/helpers/validateSteamId.js @@ -22,6 +22,7 @@ async function isValidSteamId(steamId) { return Boolean(data); } + const before = Date.now(); let valid = false; for (const url of TO_CHECK) { const isValid = await Utils.checkLeaderboard(url, steamId); @@ -32,6 +33,9 @@ async function isValidSteamId(steamId) { } await RedisUtils.setValue(`${KEY}${steamId}`, valid); + console.log( + `[Cache]: Cached Steam ID for id ${steamId} in ${Date.now() - before}ms` + ); return valid; }