Skip to content

Commit 2c6f25b

Browse files
authored
fix: 古いキャッシュを使うのを修正 (misskey-dev#13453)
1 parent 792168f commit 2c6f25b

File tree

8 files changed

+27
-27
lines changed

8 files changed

+27
-27
lines changed

packages/backend/src/core/AccountMoveService.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import { ApPersonService } from '@/core/activitypub/models/ApPersonService.js';
2020
import { ApDeliverManagerService } from '@/core/activitypub/ApDeliverManagerService.js';
2121
import { ApRendererService } from '@/core/activitypub/ApRendererService.js';
2222
import { UserEntityService } from '@/core/entities/UserEntityService.js';
23-
import { CacheService } from '@/core/CacheService.js';
2423
import { ProxyAccountService } from '@/core/ProxyAccountService.js';
2524
import { FederatedInstanceService } from '@/core/FederatedInstanceService.js';
2625
import { MetaService } from '@/core/MetaService.js';
@@ -60,7 +59,6 @@ export class AccountMoveService {
6059
private instanceChart: InstanceChart,
6160
private metaService: MetaService,
6261
private relayService: RelayService,
63-
private cacheService: CacheService,
6462
private queueService: QueueService,
6563
) {
6664
}
@@ -84,7 +82,7 @@ export class AccountMoveService {
8482
Object.assign(src, update);
8583

8684
// Update cache
87-
this.cacheService.uriPersonCache.set(srcUri, src);
85+
this.globalEventService.publishInternalEvent('localUserUpdated', src);
8886

8987
const srcPerson = await this.apRendererService.renderPerson(src);
9088
const updateAct = this.apRendererService.addContext(this.apRendererService.renderUpdate(srcPerson, src));

packages/backend/src/core/CacheService.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,12 @@ export class CacheService implements OnApplicationShutdown {
129129
switch (type) {
130130
case 'userChangeSuspendedState':
131131
case 'userChangeDeletedState':
132-
case 'remoteUserUpdated': {
132+
case 'remoteUserUpdated':
133+
case 'localUserUpdated': {
133134
const user = await this.usersRepository.findOneBy({ id: body.id });
134135
if (user == null) {
135136
this.userByIdCache.delete(body.id);
137+
this.localUserByIdCache.delete(body.id);
136138
for (const [k, v] of this.uriPersonCache.cache.entries()) {
137139
if (v.value?.id === body.id) {
138140
this.uriPersonCache.delete(k);

packages/backend/src/core/GlobalEventService.ts

+1
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ export interface InternalEventTypes {
212212
userChangeDeletedState: { id: MiUser['id']; isDeleted: MiUser['isDeleted']; };
213213
userTokenRegenerated: { id: MiUser['id']; oldToken: string; newToken: string; };
214214
remoteUserUpdated: { id: MiUser['id']; };
215+
localUserUpdated: { id: MiUser['id']; };
215216
follow: { followerId: MiUser['id']; followeeId: MiUser['id']; };
216217
unfollow: { followerId: MiUser['id']; followeeId: MiUser['id']; };
217218
blockingCreated: { blockerId: MiUser['id']; blockeeId: MiUser['id']; };

packages/backend/src/core/UserFollowingService.ts

+9-18
Original file line numberDiff line numberDiff line change
@@ -101,33 +101,24 @@ export class UserFollowingService implements OnModuleInit {
101101
this.queueService.deliver(followee, content, follower.inbox, false);
102102
}
103103

104-
/**
105-
* ThinUserでなくともユーザーの情報が最新でない場合はこちらを使うべき
106-
*/
107104
@bindThis
108-
public async followByThinUser(
105+
public async follow(
109106
_follower: ThinUser,
110107
_followee: ThinUser,
111-
options: Parameters<typeof this.follow>[2] = {},
112-
) {
113-
const [follower, followee] = await Promise.all([
114-
this.usersRepository.findOneByOrFail({ id: _follower.id }),
115-
this.usersRepository.findOneByOrFail({ id: _followee.id }),
116-
]) as [MiLocalUser | MiRemoteUser, MiLocalUser | MiRemoteUser];
117-
118-
await this.follow(follower, followee, options);
119-
}
120-
121-
@bindThis
122-
public async follow(
123-
follower: MiLocalUser | MiRemoteUser,
124-
followee: MiLocalUser | MiRemoteUser,
125108
{ requestId, silent = false, withReplies }: {
126109
requestId?: string,
127110
silent?: boolean,
128111
withReplies?: boolean,
129112
} = {},
130113
): Promise<void> {
114+
/**
115+
* 必ず最新のユーザー情報を取得する
116+
*/
117+
const [follower, followee] = await Promise.all([
118+
this.usersRepository.findOneByOrFail({ id: _follower.id }),
119+
this.usersRepository.findOneByOrFail({ id: _followee.id }),
120+
]) as [MiLocalUser | MiRemoteUser, MiLocalUser | MiRemoteUser];
121+
131122
if (this.userEntityService.isRemoteUser(follower) && this.userEntityService.isRemoteUser(followee)) {
132123
// What?
133124
throw new Error('Remote user cannot follow remote user.');

packages/backend/src/misc/cache.ts

+8
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ export class RedisSingleCache<T> {
187187
// TODO: メモリ節約のためあまり参照されないキーを定期的に削除できるようにする?
188188

189189
export class MemoryKVCache<T> {
190+
/**
191+
* データを持つマップ
192+
* @deprecated これを直接操作するべきではない
193+
*/
190194
public cache: Map<string, { date: number; value: T; }>;
191195
private lifetime: number;
192196
private gcIntervalHandle: NodeJS.Timeout;
@@ -201,6 +205,10 @@ export class MemoryKVCache<T> {
201205
}
202206

203207
@bindThis
208+
/**
209+
* Mapにキャッシュをセットします
210+
* @deprecated これを直接呼び出すべきではない。InternalEventなどで変更を全てのプロセス/マシンに通知するべき
211+
*/
204212
public set(key: string, value: T): void {
205213
this.cache.set(key, {
206214
date: Date.now(),

packages/backend/src/queue/processors/RelationshipProcessorService.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export class RelationshipProcessorService {
3535
@bindThis
3636
public async processFollow(job: Bull.Job<RelationshipJobData>): Promise<string> {
3737
this.logger.info(`${job.data.from.id} is trying to follow ${job.data.to.id} ${job.data.withReplies ? "with replies" : "without replies"}`);
38-
await this.userFollowingService.followByThinUser(job.data.from, job.data.to, {
38+
await this.userFollowingService.follow(job.data.from, job.data.to, {
3939
requestId: job.data.requestId,
4040
silent: job.data.silent,
4141
withReplies: job.data.withReplies,

packages/backend/src/server/api/endpoints/following/create.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export const paramDef = {
7171
type: 'object',
7272
properties: {
7373
userId: { type: 'string', format: 'misskey:id' },
74-
withReplies: { type: 'boolean' }
74+
withReplies: { type: 'boolean' },
7575
},
7676
required: ['userId'],
7777
} as const;

packages/backend/src/server/api/endpoints/i/update.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -456,9 +456,9 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
456456
this.hashtagService.updateUsertags(user, tags);
457457
//#endregion
458458

459-
if (Object.keys(updates).length > 0) await this.usersRepository.update(user.id, updates);
460-
if (Object.keys(updates).includes('alsoKnownAs')) {
461-
this.cacheService.uriPersonCache.set(this.userEntityService.genLocalUserUri(user.id), { ...user, ...updates });
459+
if (Object.keys(updates).length > 0) {
460+
await this.usersRepository.update(user.id, updates);
461+
this.globalEventService.publishInternalEvent('localUserUpdated', { id: user.id });
462462
}
463463

464464
await this.userProfilesRepository.update(user.id, {

0 commit comments

Comments
 (0)