fix: update cache when tags are updated

This commit is contained in:
Isaac 2023-02-02 20:45:18 +00:00
parent 5d86d9464d
commit 9fc1130c4c
No known key found for this signature in database
GPG Key ID: 0DE40AE37BBA5C33
3 changed files with 43 additions and 1 deletions

View File

@ -1,6 +1,6 @@
{
"name": "discord-tickets",
"version": "4.0.0-beta.2",
"version": "4.0.0-beta.3",
"private": "true",
"description": "An open-source Discord bot for ticket management",
"main": "src/",

View File

@ -1,3 +1,4 @@
const ms = require('ms');
const { logAdminEvent } = require('../../../../../../lib/logging');
module.exports.delete = fastify => ({
@ -10,6 +11,17 @@ module.exports.delete = fastify => ({
if (original.guildId !== guildId) return res.status(404).send(new Error('Not Found'));
const tag = await client.prisma.tag.delete({ where: { id: tagId } });
const cacheKey = `cache/guild-tags:${guildId}`;
client.keyv.set(cacheKey, await client.prisma.tag.findMany({
select: {
content: true,
id: true,
name: true,
regex: true,
},
where: { guildId: guildId },
}), ms('1h'));
logAdminEvent(client, {
action: 'delete',
guildId: req.params.guild,
@ -62,6 +74,17 @@ module.exports.patch = fastify => ({
where: { id: tagId },
});
const cacheKey = `cache/guild-tags:${guildId}`;
client.keyv.set(cacheKey, await client.prisma.tag.findMany({
select: {
content: true,
id: true,
name: true,
regex: true,
},
where: { guildId: guildId },
}), ms('1h'));
logAdminEvent(client, {
action: 'update',
diff: {

View File

@ -1,3 +1,4 @@
const ms = require('ms');
const { logAdminEvent } = require('../../../../../../lib/logging');
module.exports.get = fastify => ({
@ -29,6 +30,24 @@ module.exports.post = fastify => ({
},
});
const cacheKey = `cache/guild-tags:${guild.id}`;
let tags = await client.keyv.get(cacheKey);
if (!tags) {
tags = await client.prisma.tag.findMany({
select: {
content: true,
id: true,
name: true,
regex: true,
},
where: { guildId: guild.id },
});
client.keyv.set(cacheKey, tags, ms('1h'));
} else {
tags.push(tag);
client.keyv.set(cacheKey, tags, ms('1h'));
}
logAdminEvent(client, {
action: 'create',
guildId: guild.id,