From 2caec2f530c076651b1e453b0b263e46edcbdcc3 Mon Sep 17 00:00:00 2001 From: Hazelnoot Date: Wed, 1 Oct 2025 12:26:58 -0400 Subject: [PATCH] manage avatar decorations cache --- .../src/core/AvatarDecorationService.ts | 40 ++++++++----------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/packages/backend/src/core/AvatarDecorationService.ts b/packages/backend/src/core/AvatarDecorationService.ts index 4efd6122b1..d811f6a28a 100644 --- a/packages/backend/src/core/AvatarDecorationService.ts +++ b/packages/backend/src/core/AvatarDecorationService.ts @@ -13,10 +13,12 @@ import { bindThis } from '@/decorators.js'; import { MemorySingleCache } from '@/misc/cache.js'; import type { GlobalEvents } from '@/core/GlobalEventService.js'; import { ModerationLogService } from '@/core/ModerationLogService.js'; +import { CacheManagementService, type ManagedMemorySingleCache } from '@/core/CacheManagementService.js'; +import { InternalEventService } from '@/core/InternalEventService.js'; @Injectable() export class AvatarDecorationService implements OnApplicationShutdown { - public cache: MemorySingleCache; + public cache: ManagedMemorySingleCache; constructor( @Inject(DI.redisForSub) @@ -28,29 +30,19 @@ export class AvatarDecorationService implements OnApplicationShutdown { private idService: IdService, private moderationLogService: ModerationLogService, private globalEventService: GlobalEventService, + private readonly internalEventService: InternalEventService, + cacheManagementService: CacheManagementService, ) { - this.cache = new MemorySingleCache(1000 * 60 * 30); // 30s + this.cache = cacheManagementService.createMemorySingleCache(1000 * 60 * 30); // 30s - this.redisForSub.on('message', this.onMessage); + this.internalEventService.on('avatarDecorationCreated', this.onAvatarEvent); + this.internalEventService.on('avatarDecorationUpdated', this.onAvatarEvent); + this.internalEventService.on('avatarDecorationDeleted', this.onAvatarEvent); } @bindThis - private async onMessage(_: string, data: string): Promise { - const obj = JSON.parse(data); - - if (obj.channel === 'internal') { - const { type, body } = obj.message as GlobalEvents['internal']['payload']; - switch (type) { - case 'avatarDecorationCreated': - case 'avatarDecorationUpdated': - case 'avatarDecorationDeleted': { - this.cache.delete(); - break; - } - default: - break; - } - } + private onAvatarEvent(): void { + this.cache.delete(); } @bindThis @@ -60,7 +52,7 @@ export class AvatarDecorationService implements OnApplicationShutdown { ...options, }); - this.globalEventService.publishInternalEvent('avatarDecorationCreated', created); + await this.internalEventService.emit('avatarDecorationCreated', created); if (moderator) { this.moderationLogService.log(moderator, 'createAvatarDecoration', { @@ -83,7 +75,7 @@ export class AvatarDecorationService implements OnApplicationShutdown { }); const updated = await this.avatarDecorationsRepository.findOneByOrFail({ id: avatarDecoration.id }); - this.globalEventService.publishInternalEvent('avatarDecorationUpdated', updated); + await this.internalEventService.emit('avatarDecorationUpdated', updated); if (moderator) { this.moderationLogService.log(moderator, 'updateAvatarDecoration', { @@ -99,7 +91,7 @@ export class AvatarDecorationService implements OnApplicationShutdown { const avatarDecoration = await this.avatarDecorationsRepository.findOneByOrFail({ id }); await this.avatarDecorationsRepository.delete({ id: avatarDecoration.id }); - this.globalEventService.publishInternalEvent('avatarDecorationDeleted', avatarDecoration); + await this.internalEventService.emit('avatarDecorationDeleted', avatarDecoration); if (moderator) { this.moderationLogService.log(moderator, 'deleteAvatarDecoration', { @@ -119,7 +111,9 @@ export class AvatarDecorationService implements OnApplicationShutdown { @bindThis public dispose(): void { - this.redisForSub.off('message', this.onMessage); + this.internalEventService.off('avatarDecorationCreated', this.onAvatarEvent); + this.internalEventService.off('avatarDecorationUpdated', this.onAvatarEvent); + this.internalEventService.off('avatarDecorationDeleted', this.onAvatarEvent); } @bindThis