From 3da9d206861771c23e1ec06a21cbddf38392d4e8 Mon Sep 17 00:00:00 2001 From: syuilo Date: Tue, 22 Mar 2022 00:17:10 +0900 Subject: [PATCH] perf(server): reduce db query --- packages/backend/src/queue/processors/inbox.ts | 2 +- packages/backend/src/remote/activitypub/db-resolver.ts | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/backend/src/queue/processors/inbox.ts b/packages/backend/src/queue/processors/inbox.ts index 145a32e34..4fbfdb234 100644 --- a/packages/backend/src/queue/processors/inbox.ts +++ b/packages/backend/src/queue/processors/inbox.ts @@ -49,7 +49,7 @@ export default async (job: Bull.Job): Promise => { // HTTP-Signature keyIdを元にDBから取得 let authUser: { user: CacheableRemoteUser; - key?: UserPublickey; + key: UserPublickey | null; } | null = await dbResolver.getAuthUserFromKeyId(signature.keyId); // keyIdでわからなければ、activity.actorを元にDBから取得 || activity.actorを元にリモートから取得 diff --git a/packages/backend/src/remote/activitypub/db-resolver.ts b/packages/backend/src/remote/activitypub/db-resolver.ts index 5c748bd61..67ee5663a 100644 --- a/packages/backend/src/remote/activitypub/db-resolver.ts +++ b/packages/backend/src/remote/activitypub/db-resolver.ts @@ -10,6 +10,7 @@ import { resolvePerson } from './models/person.js'; import { Cache } from '@/misc/cache.js'; const publicKeyCache = new Cache<(UserPublickey & { user: User }) | null>(Infinity); +const publicKeyByUserIdCache = new Cache(Infinity); export default class DbResolver { constructor() { @@ -107,14 +108,13 @@ export default class DbResolver { */ public async getAuthUserFromApId(uri: string): Promise<{ user: CacheableRemoteUser; - key?: UserPublickey; + key: UserPublickey | null; } | null> { const user = await resolvePerson(uri) as CacheableRemoteUser; if (user == null) return null; - // TODO: cache - const key = await UserPublickeys.findOne(user.id); + const key = await publicKeyByUserIdCache.fetch(user.id, () => UserPublickeys.findOne(user.id).then(x => x || null), v => v != null); // TODO: typeorm 3.0 にしたら.then(x => x || null)は消せる return { user,