From 7b692facc1338739470b608d9a1b93267b867780 Mon Sep 17 00:00:00 2001 From: Isaac Date: Sat, 6 Aug 2022 21:35:42 +0100 Subject: [PATCH] Add category cache --- src/lib/tickets/manager.js | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/lib/tickets/manager.js b/src/lib/tickets/manager.js index 6554bfe..7fb450e 100644 --- a/src/lib/tickets/manager.js +++ b/src/lib/tickets/manager.js @@ -7,6 +7,7 @@ const { TextInputStyle, } = require('discord.js'); const emoji = require('node-emoji'); +const ms = require('ms'); module.exports = class TicketManager { constructor(client) { @@ -23,13 +24,20 @@ module.exports = class TicketManager { async create({ categoryId, interaction, topic, reference, }) { - const category = await this.client.prisma.category.findUnique({ - include: { - guild: true, - questions: true, - }, - where: { id: Number(categoryId) }, - }); + 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: { + guild: true, + questions: true, + }, + where: { id: Number(categoryId) }, + }); + this.client.keyv.set(cacheKey, category, ms('5m')); + } + // TODO: if member !required roles -> stop @@ -41,6 +49,8 @@ module.exports = class TicketManager { // TODO: if cooldown -> stop + // TODO: if 10s ratelimit -> stop + const getMessage = this.client.i18n.getLocale(category.guild.locale); if (category.questions.length >= 1) {