Files
Yumi/src/providers/Cache.ts
T

101 lines
2.6 KiB
TypeScript
Raw Normal View History

2022-06-06 20:11:07 +07:00
import { User, Guild } from '@prisma/client';
2022-06-06 14:59:53 +07:00
import { Snowflake } from 'discord-api-types/v10';
import Prisma from './Prisma';
2021-09-14 09:56:49 -07:00
class Cache {
2022-06-06 20:11:07 +07:00
private cache: {
Guilds: { [key: Snowflake]: Guild };
Users: { [key: Snowflake]: User };
};
2021-09-14 09:56:49 -07:00
2022-06-06 14:59:53 +07:00
constructor() {
2021-09-14 09:56:49 -07:00
this.cache = {
2022-06-06 20:11:07 +07:00
Guilds: {},
Users: {}
2021-09-14 09:56:49 -07:00
};
}
public async updateGuildsCache() {
const Guilds = await Prisma.client.guild.findMany();
2022-06-06 20:11:07 +07:00
for (let guild of Guilds) {
this.setGuildCache(guild.id, guild);
2021-09-14 09:56:49 -07:00
}
}
2022-06-06 20:11:07 +07:00
public async updateGuildCache(id: Snowflake) {
const DBGuild = await Prisma.client.guild.findFirst({ where: { id: id } });
2022-06-06 14:59:53 +07:00
2022-06-06 20:11:07 +07:00
// TODO: Try to fetch guild, create if not found or throw error
2022-06-06 14:59:53 +07:00
if (DBGuild === null) return;
2022-06-06 20:11:07 +07:00
this.setGuildCache(id, DBGuild);
2021-09-18 05:35:02 -07:00
}
2022-06-06 20:11:07 +07:00
public async updateUserCache(id: Snowflake) {
let user = await Prisma.client.user.findUnique({ where: { id } });
if (!user) {
user = await Prisma.client.user.create({
data: {
id
}
});
}
this.setUserCache(user.id, user);
2021-09-14 09:56:49 -07:00
}
2022-06-06 20:11:07 +07:00
public async getCachedGuild(id: Snowflake): Promise<Guild | undefined> {
2022-06-06 14:59:53 +07:00
if (typeof this.cache.Guilds[id] !== 'undefined') return this.cache.Guilds[id];
2021-09-14 09:56:49 -07:00
2022-06-06 20:11:07 +07:00
const Guild = await Prisma.client.guild.findUnique({
2021-09-14 09:56:49 -07:00
where: {
id: id
}
});
2022-06-06 14:59:53 +07:00
if (Guild === null) return undefined;
2021-09-14 09:56:49 -07:00
2022-06-06 20:11:07 +07:00
this.setGuildCache(Guild.id, Guild);
2021-09-14 09:56:49 -07:00
return this.cache.Guilds[id];
}
2022-06-06 20:11:07 +07:00
public async getCachedUser(id: Snowflake): Promise<User | undefined> {
if (typeof this.cache.Users[id] !== 'undefined') return this.cache.Users[id];
const user = await Prisma.client.user.findUnique({
where: {
id: id
}
});
if (user === null) return undefined;
this.setUserCache(user.id, user);
return this.cache.Users[id];
}
public setGuildCache(id: Snowflake, data: Guild): void {
this.cache.Guilds[id] = data;
}
public setUserCache(id: Snowflake, data: User): void {
this.cache.Users[id] = data;
}
public isGuildCached(id: Snowflake) {
return typeof this.cache.Guilds[id] !== 'undefined';
}
public isUserCached(id: Snowflake) {
return typeof this.cache.Users[id] !== 'undefined';
}
public getCachedGuilds(): Object {
2021-09-14 09:56:49 -07:00
return this.cache.Guilds;
}
2022-06-06 20:11:07 +07:00
public getCachedUsers(): Object {
return this.cache.Users;
}
2021-09-14 09:56:49 -07:00
}
2022-06-06 14:59:53 +07:00
export default new Cache();