Added map data cache on BeatSaver API wrapper

This commit is contained in:
Fascinated 2022-09-02 00:29:30 +01:00
parent f7d805911d
commit 738d476962

@ -1,8 +1,9 @@
import Config from '../config.json';
const mapCache = new Map();
module.exports = {
BEATSAVER_MAP_API: Config.proxy_url + "/https://api.beatsaver.com/maps/hash/%s",
// todo: cache map data for 12 hrs
/**
* Gets a specified maps data from BeatSaver
@ -11,7 +12,12 @@ module.exports = {
* @returns The map data
*/
async getMapData(hash) {
const data = await fetch(this.BEATSAVER_MAP_API.replace("%s", hash), {
hash = this.BEATSAVER_MAP_API.replace("%s", hash);
if (mapCache.has(hash)) { // Return from cache
return mapCache.get(hash);
}
const data = await fetch(hash, {
headers: {
"origin": "Fascinated Overlay"
}
@ -19,6 +25,11 @@ module.exports = {
if (data.status === 404) {
return undefined;
}
return await data.json();
const json = await data.json();
mapCache.set(hash, json);
setTimeout(() => {
mapCache.delete(hash);
}, 60 * 60 * 1000); // 1h
return json;
}
}