summaryrefslogtreecommitdiff
path: root/packages/backend/src/core/CustomEmojiService.ts
diff options
context:
space:
mode:
authordakkar <dakkar@thenautilus.net>2024-08-06 17:51:51 +0100
committerdakkar <dakkar@thenautilus.net>2024-08-06 17:51:51 +0100
commit94dceb9e15474f85d94b7ea89a5e2197d4f9b6fc (patch)
tree6bb6da6dea6e802a5c5106a90564e11b01a31ddd /packages/backend/src/core/CustomEmojiService.ts
parentappease the linter (diff)
parentmerge: Remove infinite caches to prevent memory leak (!587) (diff)
downloadsharkey-94dceb9e15474f85d94b7ea89a5e2197d4f9b6fc.tar.gz
sharkey-94dceb9e15474f85d94b7ea89a5e2197d4f9b6fc.tar.bz2
sharkey-94dceb9e15474f85d94b7ea89a5e2197d4f9b6fc.zip
Merge branch 'develop' into feature/misskey-2024.07
Diffstat (limited to 'packages/backend/src/core/CustomEmojiService.ts')
-rw-r--r--packages/backend/src/core/CustomEmojiService.ts21
1 files changed, 14 insertions, 7 deletions
diff --git a/packages/backend/src/core/CustomEmojiService.ts b/packages/backend/src/core/CustomEmojiService.ts
index 1eff2fdbff..dd5f79a22b 100644
--- a/packages/backend/src/core/CustomEmojiService.ts
+++ b/packages/backend/src/core/CustomEmojiService.ts
@@ -26,7 +26,7 @@ const parseEmojiStrRegexp = /^([-\w]+)(?:@([\w.-]+))?$/;
@Injectable()
export class CustomEmojiService implements OnApplicationShutdown {
- private cache: MemoryKVCache<MiEmoji | null>;
+ private emojisCache: MemoryKVCache<MiEmoji | null>;
public localEmojisCache: RedisSingleCache<Map<string, MiEmoji>>;
constructor(
@@ -49,7 +49,7 @@ export class CustomEmojiService implements OnApplicationShutdown {
private globalEventService: GlobalEventService,
private driveService: DriveService,
) {
- this.cache = new MemoryKVCache<MiEmoji | null>(1000 * 60 * 60 * 12);
+ this.emojisCache = new MemoryKVCache<MiEmoji | null>(1000 * 60 * 60 * 12); // 12h
this.localEmojisCache = new RedisSingleCache<Map<string, MiEmoji>>(this.redisClient, 'localEmojis', {
lifetime: 1000 * 60 * 30, // 30m
@@ -142,6 +142,13 @@ export class CustomEmojiService implements OnApplicationShutdown {
this.localEmojisCache.refresh();
+ if (data.driveFile != null) {
+ const file = await this.driveFilesRepository.findOneBy({ url: emoji.originalUrl, userHost: emoji.host ? emoji.host : IsNull() });
+ if (file && file.id != data.driveFile.id) {
+ await this.driveService.deleteFile(file, false, moderator ? moderator : undefined);
+ }
+ }
+
const packed = await this.emojiEntityService.packDetailed(emoji.id);
if (emoji.name === data.name) {
@@ -350,14 +357,14 @@ export class CustomEmojiService implements OnApplicationShutdown {
if (name == null) return null;
if (host == null) return null;
- const newHost = host === this.config.host ? null : host;
+ const newHost = host === this.config.host ? null : host;
const queryOrNull = async () => (await this.emojisRepository.findOneBy({
name,
host: newHost ?? IsNull(),
})) ?? null;
- const emoji = await this.cache.fetch(`${name} ${host}`, queryOrNull);
+ const emoji = await this.emojisCache.fetch(`${name} ${host}`, queryOrNull);
if (emoji == null) return null;
return emoji.publicUrl || emoji.originalUrl; // || emoji.originalUrl してるのは後方互換性のため(publicUrlはstringなので??はだめ)
@@ -384,7 +391,7 @@ export class CustomEmojiService implements OnApplicationShutdown {
*/
@bindThis
public async prefetchEmojis(emojis: { name: string; host: string | null; }[]): Promise<void> {
- const notCachedEmojis = emojis.filter(emoji => this.cache.get(`${emoji.name} ${emoji.host}`) == null);
+ const notCachedEmojis = emojis.filter(emoji => this.emojisCache.get(`${emoji.name} ${emoji.host}`) == null);
const emojisQuery: any[] = [];
const hosts = new Set(notCachedEmojis.map(e => e.host));
for (const host of hosts) {
@@ -399,7 +406,7 @@ export class CustomEmojiService implements OnApplicationShutdown {
select: ['name', 'host', 'originalUrl', 'publicUrl'],
}) : [];
for (const emoji of _emojis) {
- this.cache.set(`${emoji.name} ${emoji.host}`, emoji);
+ this.emojisCache.set(`${emoji.name} ${emoji.host}`, emoji);
}
}
@@ -424,7 +431,7 @@ export class CustomEmojiService implements OnApplicationShutdown {
@bindThis
public dispose(): void {
- this.cache.dispose();
+ this.emojisCache.dispose();
}
@bindThis