From 3c65aa3fd3db136aa7d644c353b78fd4e88522ae Mon Sep 17 00:00:00 2001 From: John Livingston Date: Thu, 6 Jun 2024 16:56:43 +0200 Subject: [PATCH] Custom channel emoticons WIP (#130): cache --- server/lib/emojis/emojis.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/server/lib/emojis/emojis.ts b/server/lib/emojis/emojis.ts index c6be5510..09455a10 100644 --- a/server/lib/emojis/emojis.ts +++ b/server/lib/emojis/emojis.ts @@ -22,6 +22,7 @@ export class Emojis { protected options: RegisterServerOptions protected channelBasePath: string protected channelBaseUri: string + protected readonly channelCache = new Map() protected readonly logger: { debug: (s: string) => void info: (s: string) => void @@ -59,8 +60,12 @@ export class Emojis { * @param channelId channel Id */ public async channelHasCustomEmojis (channelId: number): Promise { + if (this.channelCache.has(channelId)) { return this.channelCache.get(channelId) as boolean } + const filepath = this.channelCustomEmojisDefinitionPath(channelId) - return fs.promises.access(filepath, fs.constants.F_OK).then(() => true, () => false) + const v = await fs.promises.access(filepath, fs.constants.F_OK).then(() => true, () => false) + this.channelCache.set(channelId, v) + return v } /** @@ -335,6 +340,7 @@ export class Emojis { recursive: true }) await fs.promises.writeFile(filepath, JSON.stringify(def)) + this.channelCache.delete(channelId) const fileDirPath = this.channelCustomEmojisDirPath(channelId) await fs.promises.mkdir(fileDirPath, { @@ -386,6 +392,8 @@ export class Emojis { if (!(('code' in err) && err.code === 'ENOENT')) { this.logger.error(err) } + } finally { + this.channelCache.delete(channelId) } }