From 5825710a0f44ce5bf02ad91fdc0f8c5465ff4b46 Mon Sep 17 00:00:00 2001 From: Yuzu Date: Tue, 14 Sep 2021 09:56:49 -0700 Subject: [PATCH] Implemented Caching --- src/providers/Cache.ts | 50 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/providers/Cache.ts diff --git a/src/providers/Cache.ts b/src/providers/Cache.ts new file mode 100644 index 0000000..41fbf18 --- /dev/null +++ b/src/providers/Cache.ts @@ -0,0 +1,50 @@ +import Prisma from "./Prisma"; + +class Cache { + + private cache: any; + + constructor () { + this.cache = { + Guilds: { + + } + }; + } + + public async updateGuildsCache() { + const Guilds = await Prisma.client.guild.findMany(); + for(let Guild of Guilds) { + this.setGuildData(Guild.id, Guild); + } + } + + public setGuildData(id: string, data: Object): void { + this.cache.Guilds[id] = data; + } + + public async getGuild(id: string) { + if(typeof this.cache.Guilds[id] !== 'undefined') + return this.cache.Guilds[id]; + + const Guild = await Prisma.client.guild.findFirst({ + where: { + id: id + } + }); + + if(Guild === null) + return undefined; + + this.setGuildData(Guild.id, Guild); + return this.cache.Guilds[id]; + + } + + public getGuilds(): void { + return this.cache.Guilds; + } + +} + +export default new Cache(); \ No newline at end of file