Add category cache

This commit is contained in:
Isaac 2022-08-06 21:35:42 +01:00
parent 3318ddcaa6
commit 7b692facc1
No known key found for this signature in database
GPG Key ID: F4EAABEB0FFCC06A

View File

@ -7,6 +7,7 @@ const {
TextInputStyle, TextInputStyle,
} = require('discord.js'); } = require('discord.js');
const emoji = require('node-emoji'); const emoji = require('node-emoji');
const ms = require('ms');
module.exports = class TicketManager { module.exports = class TicketManager {
constructor(client) { constructor(client) {
@ -23,13 +24,20 @@ module.exports = class TicketManager {
async create({ async create({
categoryId, interaction, topic, reference, categoryId, interaction, topic, reference,
}) { }) {
const category = await this.client.prisma.category.findUnique({ const cacheKey = `cache/category+guild+questions:${categoryId}`;
let category = await this.client.keyv.get(cacheKey);
if (!category) {
category = await this.client.prisma.category.findUnique({
include: { include: {
guild: true, guild: true,
questions: true, questions: true,
}, },
where: { id: Number(categoryId) }, where: { id: Number(categoryId) },
}); });
this.client.keyv.set(cacheKey, category, ms('5m'));
}
// TODO: if member !required roles -> stop // TODO: if member !required roles -> stop
@ -41,6 +49,8 @@ module.exports = class TicketManager {
// TODO: if cooldown -> stop // TODO: if cooldown -> stop
// TODO: if 10s ratelimit -> stop
const getMessage = this.client.i18n.getLocale(category.guild.locale); const getMessage = this.client.i18n.getLocale(category.guild.locale);
if (category.questions.length >= 1) { if (category.questions.length >= 1) {