Improve cache

This commit is contained in:
Daniel Odendahl Jr
2017-10-21 22:37:47 +00:00
parent e61c8ea03c
commit fa26fb5a56
+14 -7
View File
@@ -25,6 +25,7 @@ module.exports = class LeagueOfLegendsChampionCommand extends Command {
}); });
this.version = null; this.version = null;
this.champions = null;
this.cache = new Map(); this.cache = new Map();
this.client.setInterval(() => this.cache.clear(), 3600000); this.client.setInterval(() => this.cache.clear(), 3600000);
} }
@@ -34,12 +35,10 @@ module.exports = class LeagueOfLegendsChampionCommand extends Command {
try { try {
let data; let data;
if (!this.cache.has(champion)) { if (!this.cache.has(champion)) {
const search = await snekfetch if (!this.champions) await this.fetchChampions();
.get('https://na1.api.riotgames.com/lol/static-data/v3/champions') const name = Object.keys(this.champions).find(key => key.toLowerCase() === champion);
.query({ api_key: RIOT_KEY });
const name = Object.keys(search.body.data).find(key => key.toLowerCase() === champion);
if (!name) return msg.say('Could not find any results.'); if (!name) return msg.say('Could not find any results.');
const { id } = search.body.data[name]; const { id } = this.champions[name];
const { body } = await snekfetch const { body } = await snekfetch
.get(`https://na1.api.riotgames.com/lol/static-data/v3/champions/${id}`) .get(`https://na1.api.riotgames.com/lol/static-data/v3/champions/${id}`)
.query({ .query({
@@ -47,8 +46,7 @@ module.exports = class LeagueOfLegendsChampionCommand extends Command {
tags: 'all' tags: 'all'
}); });
data = body; data = body;
this.cache.set(name, body); this.cache.set(champion, body);
this.version = search.body.version;
} else { } else {
data = this.cache.get(champion); data = this.cache.get(champion);
} }
@@ -100,4 +98,13 @@ module.exports = class LeagueOfLegendsChampionCommand extends Command {
return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
} }
} }
async fetchChampions() {
const { body } = await snekfetch
.get('https://na1.api.riotgames.com/lol/static-data/v3/champions')
.query({ api_key: RIOT_KEY });
this.version = body.version;
this.champions = body.data;
return body.data;
}
}; };