Custom channel emoticons WIP (#130): cache

This commit is contained in:
John Livingston 2024-06-06 16:56:43 +02:00
parent a777c7ac8d
commit 3c65aa3fd3
No known key found for this signature in database
GPG Key ID: B17B5640CE66CDBC
1 changed files with 9 additions and 1 deletions

View File

@ -22,6 +22,7 @@ export class Emojis {
protected options: RegisterServerOptions
protected channelBasePath: string
protected channelBaseUri: string
protected readonly channelCache = new Map<number, boolean>()
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<boolean> {
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)
}
}