Integrated redis into the discord.ts controller
This commit is contained in:
parent
acb3f2402c
commit
d494512249
@ -1,5 +1,6 @@
|
||||
import DiscordOauth2 from 'discord-oauth2';
|
||||
import {User} from "../models/User";
|
||||
import {redisClient} from "../app";
|
||||
|
||||
const oauth = new DiscordOauth2();
|
||||
|
||||
@ -57,7 +58,15 @@ export const updateToken = async (refreshDate: Date, refreshToken: string, acces
|
||||
* @param accessToken The token to access the discord api
|
||||
*/
|
||||
export const getGuilds = async (refreshDate: Date, refreshToken: string, accessToken: string) => {
|
||||
return await oauth.getUserGuilds(await updateToken(refreshDate, refreshToken, accessToken));
|
||||
const cacheResults = await redisClient.get(`guilds:${accessToken}`);
|
||||
|
||||
if (cacheResults) {
|
||||
return JSON.parse(cacheResults) as DiscordOauth2.PartialGuild[];
|
||||
} else {
|
||||
const guilds = await oauth.getUserGuilds(await updateToken(refreshDate, refreshToken, accessToken));
|
||||
await redisClient.setEx(`guilds:${accessToken}`, 60 * 60 * 24, JSON.stringify(guilds));
|
||||
return guilds;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user