From c80a7e9d153e2ec753ae0b3c1ae9be26411c0ff1 Mon Sep 17 00:00:00 2001 From: AnxietyisReal <96593068+AnxietyisReal@users.noreply.github.com> Date: Wed, 27 Dec 2023 09:46:08 +1100 Subject: [PATCH] Dedupe the get and set functions. --- src/components/CacheServer.ts | 35 ++++++++++++++++------------------- src/models/MPServer.ts | 4 ++-- src/models/punishments.ts | 4 ++-- src/models/tagSystem.ts | 4 ++-- src/modules/YTModule.ts | 6 +++--- 5 files changed, 25 insertions(+), 28 deletions(-) diff --git a/src/components/CacheServer.ts b/src/components/CacheServer.ts index 3d6809e..54bd6ef 100644 --- a/src/components/CacheServer.ts +++ b/src/components/CacheServer.ts @@ -2,7 +2,6 @@ import {createClient, ErrorReply} from 'redis'; import Logger from '../helpers/Logger.js'; import TSClient from '../helpers/TSClient.js'; -let Prefix = 'Cache'; const RedisClient = createClient({ url: (await TSClient()).redis_uri, database: 0, @@ -11,35 +10,32 @@ const RedisClient = createClient({ }); export default class CacheServer { + protected static prefix = 'Cache'; protected static eventManager() { RedisClient - .on('connect', ()=>Logger.console('log', Prefix, 'Connection to Redis has been established')) + .on('connect', ()=>Logger.console('log', this.prefix, 'Connection to Redis has been established')) .on('error', (err:ErrorReply)=>{ - Logger.console('error', Prefix, `Encountered an error in Redis: ${err.message}`) + Logger.console('error', this.prefix, `Encountered an error in Redis: ${err.message}`) setTimeout(async()=>{ if (!RedisClient.isReady) { - Logger.console('log', Prefix, 'Client is zombified, starting a fresh connection...'); + Logger.console('log', this.prefix, 'Client is zombified, starting a fresh connection...'); RedisClient.quit(); await RedisClient.connect(); } }, 1500) }) } - public static async get(key:any) { - const cachedResult = await RedisClient.get(key); - if (cachedResult) return JSON.parse(cachedResult); - else return null + public static async get(key:any, jsonMode:boolean):Promise { + let cachedResult:any; + if (jsonMode) cachedResult = await RedisClient.json.get(key); + else { + cachedResult = await RedisClient.get(key); + if (cachedResult) cachedResult = JSON.parse(cachedResult); + } } - public static async set(key:any, value:any) { - return await RedisClient.set(key, JSON.stringify(value)); - } - public static async getJSON(key:any) { - const cachedResult = await RedisClient.json.get(key); - if (cachedResult) return cachedResult; - else return null - } - public static async setJSON(key:any, value:any) { - return await RedisClient.json.set(key, '.', value); + public static async set(key:any, value:any, jsonMode:boolean):Promise { + if (jsonMode) return await RedisClient.json.set(key, '.', value); + else return await RedisClient.set(key, JSON.stringify(value)); } public static async expiry(key:any, time:number) { return await RedisClient.expire(key, time); // NOTE: time is in seconds, not milliseconds -- you know what you did wrong @@ -52,7 +48,8 @@ export default class CacheServer { RedisClient.connect(); this.eventManager(); } catch { - console.error('Cannot initialize RedisClient -- is Redis running?') + console.error('Cannot initialize RedisClient -- is Redis running?'); + process.exit(1); } } } diff --git a/src/models/MPServer.ts b/src/models/MPServer.ts index dc76b4f..f61aeb5 100644 --- a/src/models/MPServer.ts +++ b/src/models/MPServer.ts @@ -109,12 +109,12 @@ export class MPServerSvc { } else return false; } async findInCache(): Promise { - const cachedResult = await CacheServer.getJSON(cacheKey); + const cachedResult = await CacheServer.get(cacheKey, true); let result; if (cachedResult) result = cachedResult; else { result = await this.model.findAll(); - CacheServer.setJSON(cacheKey, result).then(()=>CacheServer.expiry(cacheKey, 1800)); + CacheServer.set(cacheKey, result, true).then(()=>CacheServer.expiry(cacheKey, 1800)); } return result; } diff --git a/src/models/punishments.ts b/src/models/punishments.ts index 6f639c3..ca1dbf1 100644 --- a/src/models/punishments.ts +++ b/src/models/punishments.ts @@ -124,12 +124,12 @@ export class PunishmentsSvc { } async findInCache():Promise { const cacheKey = 'punishments'; - const cachedResult = await CacheServer.getJSON(cacheKey); + const cachedResult = await CacheServer.get(cacheKey, true); let result; if (cachedResult) result = cachedResult; else { result = await this.model.findAll(); - CacheServer.setJSON(cacheKey, result).then(()=>CacheServer.expiry(cacheKey, 20)); + CacheServer.set(cacheKey, result, true).then(()=>CacheServer.expiry(cacheKey, 20)); } return result; } diff --git a/src/models/tagSystem.ts b/src/models/tagSystem.ts index d19a8a5..b032c3a 100644 --- a/src/models/tagSystem.ts +++ b/src/models/tagSystem.ts @@ -88,12 +88,12 @@ export class TagSystemSvc { } async findInCache(): Promise { const cacheKey = 'tags'; - const cachedResult = await CacheServer.getJSON(cacheKey); + const cachedResult = await CacheServer.get(cacheKey, true); let result; if (cachedResult) result = cachedResult; else { result = await this.model.findAll(); - CacheServer.setJSON(cacheKey, result).then(()=>CacheServer.expiry(cacheKey, 240)); + CacheServer.set(cacheKey, result, true).then(()=>CacheServer.expiry(cacheKey, 240)); } return result; } diff --git a/src/modules/YTModule.ts b/src/modules/YTModule.ts index 80e0cec..8afdd51 100644 --- a/src/modules/YTModule.ts +++ b/src/modules/YTModule.ts @@ -35,13 +35,13 @@ export default async(client:TClient)=>{ const getVideoId = (index:number)=>Data.items[index].snippet.thumbnails.default.url.split('/')[4]; const videoUrl = `https://www.youtube.com/watch?v=${getVideoId(0)}`; const cacheKey = `YTCache:${YTChannelID}`; - const cachedVideoId = await CacheServer.get(cacheKey); + const cachedVideoId = await CacheServer.get(cacheKey, false); if (!cachedVideoId) { - await CacheServer.set(cacheKey, getVideoId(0)).then(async()=>await CacheServer.expiry(cacheKey, cacheExpiry)); + await CacheServer.set(cacheKey, getVideoId(0), false).then(async()=>await CacheServer.expiry(cacheKey, cacheExpiry)); return; } if (getVideoId(1) === cachedVideoId) { - await CacheServer.delete(cacheKey).then(async()=>await CacheServer.set(cacheKey, getVideoId(0)).then(async()=>await CacheServer.expiry(cacheKey, cacheExpiry))); + await CacheServer.delete(cacheKey).then(async()=>await CacheServer.set(cacheKey, getVideoId(0), false).then(async()=>await CacheServer.expiry(cacheKey, cacheExpiry))); (client.channels.resolve(DiscordChannelID) as TextChannel).send({ content: `${MessageTool.formatMention(DiscordRoleID, 'role')}\n**${YTChannelName}** just uploaded a video!\n${videoUrl}`, allowedMentions: {parse: ['roles']},