-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
enhance: カスタム絵文字の更新をサーバーに確認してクライアントキャッシュを更新するように #17758
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,7 +13,7 @@ import { ModerationLogService } from '@/core/ModerationLogService.js'; | |
| import { UtilityService } from '@/core/UtilityService.js'; | ||
| import { bindThis } from '@/decorators.js'; | ||
| import { DI } from '@/di-symbols.js'; | ||
| import { MemoryKVCache, RedisSingleCache } from '@/misc/cache.js'; | ||
| import { MemoryKVCache, MemorySingleCache, RedisSingleCache } from '@/misc/cache.js'; | ||
| import { sqlLikeEscape } from '@/misc/sql-like-escape.js'; | ||
| import type { EmojisRepository, MiRole, MiUser } from '@/models/_.js'; | ||
| import type { MiEmoji } from '@/models/Emoji.js'; | ||
|
|
@@ -57,9 +57,15 @@ export const fetchEmojisSortKeys = [ | |
| ] as const; | ||
| export type FetchEmojisSortKeys = typeof fetchEmojisSortKeys[number]; | ||
|
|
||
| export type LocalEmojisStats = { | ||
| count: number; | ||
| lastUpdatedAt: Date | null; | ||
| }; | ||
|
|
||
| @Injectable() | ||
| export class CustomEmojiService implements OnApplicationShutdown { | ||
| private emojisCache: MemoryKVCache<MiEmoji | null>; | ||
| private localEmojisStatsCache: MemorySingleCache<LocalEmojisStats>; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| public localEmojisCache: RedisSingleCache<Map<string, MiEmoji>>; | ||
|
|
||
| constructor( | ||
|
|
@@ -74,7 +80,7 @@ export class CustomEmojiService implements OnApplicationShutdown { | |
| private globalEventService: GlobalEventService, | ||
| ) { | ||
| this.emojisCache = new MemoryKVCache<MiEmoji | null>(1000 * 60 * 60 * 12); // 12h | ||
|
|
||
| this.localEmojisStatsCache = new MemorySingleCache<LocalEmojisStats>(1000 * 60 * 3); // 3m | ||
| this.localEmojisCache = new RedisSingleCache<Map<string, MiEmoji>>(this.redisClient, 'localEmojis', { | ||
| lifetime: 1000 * 60 * 30, // 30m | ||
| memoryCacheLifetime: 1000 * 60 * 3, // 3m | ||
|
|
@@ -89,6 +95,12 @@ export class CustomEmojiService implements OnApplicationShutdown { | |
| }); | ||
| } | ||
|
|
||
| @bindThis | ||
| private refreshLocalEmojiCaches(): void { | ||
| this.localEmojisCache.refresh(); | ||
| this.localEmojisStatsCache.delete(); | ||
| } | ||
|
|
||
| @bindThis | ||
| public async add(data: { | ||
| originalUrl: string; | ||
|
|
@@ -120,7 +132,7 @@ export class CustomEmojiService implements OnApplicationShutdown { | |
| }); | ||
|
|
||
| if (data.host == null) { | ||
| this.localEmojisCache.refresh(); | ||
| this.refreshLocalEmojiCaches(); | ||
|
|
||
| this.globalEventService.publishBroadcastStream('emojiAdded', { | ||
| emoji: await this.emojiEntityService.packDetailed(emoji.id), | ||
|
|
@@ -182,7 +194,7 @@ export class CustomEmojiService implements OnApplicationShutdown { | |
| roleIdsThatCanBeUsedThisEmojiAsReaction: data.roleIdsThatCanBeUsedThisEmojiAsReaction ?? undefined, | ||
| }); | ||
|
|
||
| this.localEmojisCache.refresh(); | ||
| this.refreshLocalEmojiCaches(); | ||
|
|
||
| const packed = await this.emojiEntityService.packDetailed(emoji.id); | ||
|
|
||
|
|
@@ -224,7 +236,7 @@ export class CustomEmojiService implements OnApplicationShutdown { | |
| }); | ||
| } | ||
|
|
||
| this.localEmojisCache.refresh(); | ||
| this.refreshLocalEmojiCaches(); | ||
|
|
||
| this.globalEventService.publishBroadcastStream('emojiUpdated', { | ||
| emojis: await this.emojiEntityService.packDetailedMany(ids), | ||
|
|
@@ -240,7 +252,7 @@ export class CustomEmojiService implements OnApplicationShutdown { | |
| aliases: aliases, | ||
| }); | ||
|
|
||
| this.localEmojisCache.refresh(); | ||
| this.refreshLocalEmojiCaches(); | ||
|
|
||
| this.globalEventService.publishBroadcastStream('emojiUpdated', { | ||
| emojis: await this.emojiEntityService.packDetailedMany(ids), | ||
|
|
@@ -260,7 +272,7 @@ export class CustomEmojiService implements OnApplicationShutdown { | |
| }); | ||
| } | ||
|
|
||
| this.localEmojisCache.refresh(); | ||
| this.refreshLocalEmojiCaches(); | ||
|
|
||
| this.globalEventService.publishBroadcastStream('emojiUpdated', { | ||
| emojis: await this.emojiEntityService.packDetailedMany(ids), | ||
|
|
@@ -276,7 +288,7 @@ export class CustomEmojiService implements OnApplicationShutdown { | |
| category: category, | ||
| }); | ||
|
|
||
| this.localEmojisCache.refresh(); | ||
| this.refreshLocalEmojiCaches(); | ||
|
|
||
| this.globalEventService.publishBroadcastStream('emojiUpdated', { | ||
| emojis: await this.emojiEntityService.packDetailedMany(ids), | ||
|
|
@@ -292,7 +304,7 @@ export class CustomEmojiService implements OnApplicationShutdown { | |
| license: license, | ||
| }); | ||
|
|
||
| this.localEmojisCache.refresh(); | ||
| this.refreshLocalEmojiCaches(); | ||
|
|
||
| this.globalEventService.publishBroadcastStream('emojiUpdated', { | ||
| emojis: await this.emojiEntityService.packDetailedMany(ids), | ||
|
|
@@ -305,7 +317,7 @@ export class CustomEmojiService implements OnApplicationShutdown { | |
|
|
||
| await this.emojisRepository.delete(emoji.id); | ||
|
|
||
| this.localEmojisCache.refresh(); | ||
| this.refreshLocalEmojiCaches(); | ||
|
|
||
| this.globalEventService.publishBroadcastStream('emojiDeleted', { | ||
| emojis: [await this.emojiEntityService.packDetailed(emoji)], | ||
|
|
@@ -336,7 +348,7 @@ export class CustomEmojiService implements OnApplicationShutdown { | |
| } | ||
| } | ||
|
|
||
| this.localEmojisCache.refresh(); | ||
| this.refreshLocalEmojiCaches(); | ||
|
|
||
| this.globalEventService.publishBroadcastStream('emojiDeleted', { | ||
| emojis: await this.emojiEntityService.packDetailedMany(emojis), | ||
|
|
@@ -601,6 +613,22 @@ export class CustomEmojiService implements OnApplicationShutdown { | |
| }; | ||
| } | ||
|
|
||
| @bindThis | ||
| public async getLocalEmojisStats(): Promise<LocalEmojisStats> { | ||
| return this.localEmojisStatsCache.fetch(async () => { | ||
| const raw = await this.emojisRepository.createQueryBuilder('emoji') | ||
| .select('COUNT(*)', 'count') | ||
| .addSelect('MAX(emoji.updatedAt)', 'lastUpdatedAt') | ||
| .where('emoji.host IS NULL') | ||
| .getRawOne<{ count: string; lastUpdatedAt: Date | null }>(); | ||
|
|
||
| return { | ||
| count: raw ? Number(raw.count) : 0, | ||
| lastUpdatedAt: raw?.lastUpdatedAt ?? null, | ||
| }; | ||
| }); | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 現状、件数と最終更新日時ベースでは絵文字削除が追えないかと思います(物理削除なので)。 「有効な絵文字一覧」としてのバージョンをmetaあたりに持っておくのが良いかと思います。
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
で問題なく更新される説がある
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ありがとうございます。削除の追跡が漏れる懸念はなさそうです。 -- ただ、それでもやはり気になるのが、絵文字一覧の変更有無を表現するためだけに
という点から、負荷と整合性の両面でウィークポイントになりやすいと考えています。 そのため再度、metaやRedisなどの共有領域に、集計不要な絵文字一覧のリビジョンを持たせ、論理的な変更操作ごとに更新する方式を提案させて頂きます |
||
|
|
||
| @bindThis | ||
| public dispose(): void { | ||
| this.emojisCache.dispose(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: syuilo and misskey-project | ||
| * SPDX-License-Identifier: AGPL-3.0-only | ||
| */ | ||
|
|
||
| import { Injectable } from '@nestjs/common'; | ||
| import { Endpoint } from '@/server/api/endpoint-base.js'; | ||
| import { CustomEmojiService } from '@/core/CustomEmojiService.js'; | ||
|
|
||
| export const meta = { | ||
| tags: ['meta'], | ||
|
|
||
| requireCredential: false, | ||
|
|
||
| res: { | ||
| type: 'object', | ||
| optional: false, nullable: false, | ||
| properties: { | ||
| count: { | ||
| type: 'number', | ||
| optional: false, nullable: false, | ||
| }, | ||
| lastUpdatedAt: { | ||
| type: 'string', | ||
| optional: false, nullable: true, | ||
| format: 'date-time', | ||
| }, | ||
| }, | ||
| }, | ||
| } as const; | ||
|
|
||
| export const paramDef = { | ||
| type: 'object', | ||
| properties: { | ||
| }, | ||
| required: [], | ||
| } as const; | ||
|
|
||
| @Injectable() | ||
| export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export | ||
| constructor( | ||
| private customEmojiService: CustomEmojiService, | ||
| ) { | ||
| super(meta, paramDef, async (ps, me) => { | ||
| const stats = await this.customEmojiService.getLocalEmojisStats(); | ||
|
|
||
| return { | ||
| count: stats.count, | ||
| lastUpdatedAt: stats.lastUpdatedAt ? stats.lastUpdatedAt.toISOString() : null, | ||
| }; | ||
| }); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.