perf(server): use cached user info in getUserFromApId

This commit is contained in:
syuilo 2022-03-26 19:09:57 +09:00
parent 8cbfc047bb
commit 17589843da
5 changed files with 38 additions and 13 deletions

View File

@ -48,7 +48,32 @@ export class Cache<T> {
// Cache MISS // Cache MISS
const value = await fetcher(); const value = await fetcher();
return value;
}
/**
* fetcherを呼び出して結果をキャッシュ&
* optional: キャッシュが存在してもvalidatorでfalseを返すとキャッシュ無効扱いにします
*/
public async fetchMaybe(key: string | null, fetcher: () => Promise<T | undefined>, validator?: (cachedValue: T) => boolean): Promise<T | undefined> {
const cachedValue = this.get(key);
if (cachedValue !== undefined) {
if (validator) {
if (validator(cachedValue)) {
// Cache HIT
return cachedValue;
}
} else {
// Cache HIT
return cachedValue;
}
}
// Cache MISS
const value = await fetcher();
if (value !== undefined) {
this.set(key, value); this.set(key, value);
}
return value; return value;
} }
} }

View File

@ -1,14 +1,14 @@
import escapeRegexp from 'escape-regexp'; import escapeRegexp from 'escape-regexp';
import config from '@/config/index.js'; import config from '@/config/index.js';
import { Note } from '@/models/entities/note.js'; import { Note } from '@/models/entities/note.js';
import { User, IRemoteUser, CacheableRemoteUser } from '@/models/entities/user.js'; import { User, IRemoteUser, CacheableRemoteUser, CacheableUser } from '@/models/entities/user.js';
import { UserPublickey } from '@/models/entities/user-publickey.js'; import { UserPublickey } from '@/models/entities/user-publickey.js';
import { MessagingMessage } from '@/models/entities/messaging-message.js'; import { MessagingMessage } from '@/models/entities/messaging-message.js';
import { Notes, Users, UserPublickeys, MessagingMessages } from '@/models/index.js'; import { Notes, Users, UserPublickeys, MessagingMessages } from '@/models/index.js';
import { IObject, getApId } from './type.js'; import { IObject, getApId } from './type.js';
import { resolvePerson } from './models/person.js'; import { resolvePerson } from './models/person.js';
import { Cache } from '@/misc/cache.js'; import { Cache } from '@/misc/cache.js';
import { userByIdCache } from '@/services/user-cache.js'; import { uriPersonCache, userByIdCache } from '@/services/user-cache.js';
const publicKeyCache = new Cache<UserPublickey | null>(Infinity); const publicKeyCache = new Cache<UserPublickey | null>(Infinity);
const publicKeyByUserIdCache = new Cache<UserPublickey | null>(Infinity); const publicKeyByUserIdCache = new Cache<UserPublickey | null>(Infinity);
@ -59,19 +59,19 @@ export default class DbResolver {
/** /**
* AP Person => Misskey User in DB * AP Person => Misskey User in DB
*/ */
public async getUserFromApId(value: string | IObject): Promise<User | null> { public async getUserFromApId(value: string | IObject): Promise<CacheableUser | null> {
const parsed = this.parseUri(value); const parsed = this.parseUri(value);
if (parsed.id) { if (parsed.id) {
return await Users.findOneBy({ return await userByIdCache.fetchMaybe(parsed.id, () => Users.findOneBy({
id: parsed.id, id: parsed.id,
}); }).then(x => x ?? undefined)) ?? null;
} }
if (parsed.uri) { if (parsed.uri) {
return await Users.findOneBy({ return await uriPersonCache.fetch(parsed.uri, () => Users.findOneBy({
uri: parsed.uri, uri: parsed.uri,
}); }));
} }
return null; return null;

View File

@ -18,6 +18,6 @@ export default async (actor: CacheableRemoteUser, activity: IBlock): Promise<str
return `skip: ブロックしようとしているユーザーはローカルユーザーではありません`; return `skip: ブロックしようとしているユーザーはローカルユーザーではありません`;
} }
await block(await Users.findOneByOrFail({ id: actor.id }), blockee); await block(await Users.findOneByOrFail({ id: actor.id }), await Users.findOneByOrFail({ id: blockee.id }));
return `ok`; return `ok`;
}; };

View File

@ -3,12 +3,12 @@ import renderBlock from '@/remote/activitypub/renderer/block.js';
import renderUndo from '@/remote/activitypub/renderer/undo.js'; import renderUndo from '@/remote/activitypub/renderer/undo.js';
import { deliver } from '@/queue/index.js'; import { deliver } from '@/queue/index.js';
import Logger from '../logger.js'; import Logger from '../logger.js';
import { User } from '@/models/entities/user.js'; import { CacheableUser, User } from '@/models/entities/user.js';
import { Blockings, Users } from '@/models/index.js'; import { Blockings, Users } from '@/models/index.js';
const logger = new Logger('blocking/delete'); const logger = new Logger('blocking/delete');
export default async function(blocker: User, blockee: User) { export default async function(blocker: CacheableUser, blockee: CacheableUser) {
const blocking = await Blockings.findOneBy({ const blocking = await Blockings.findOneBy({
blockerId: blocker.id, blockerId: blocker.id,
blockeeId: blockee.id, blockeeId: blockee.id,

View File

@ -4,11 +4,11 @@ import renderAccept from '@/remote/activitypub/renderer/accept.js';
import { deliver } from '@/queue/index.js'; import { deliver } from '@/queue/index.js';
import { publishMainStream } from '@/services/stream.js'; import { publishMainStream } from '@/services/stream.js';
import { insertFollowingDoc } from '../create.js'; import { insertFollowingDoc } from '../create.js';
import { User, ILocalUser } from '@/models/entities/user.js'; import { User, ILocalUser, CacheableUser } from '@/models/entities/user.js';
import { FollowRequests, Users } from '@/models/index.js'; import { FollowRequests, Users } from '@/models/index.js';
import { IdentifiableError } from '@/misc/identifiable-error.js'; import { IdentifiableError } from '@/misc/identifiable-error.js';
export default async function(followee: { id: User['id']; host: User['host']; uri: User['host']; inbox: User['inbox']; sharedInbox: User['sharedInbox']; }, follower: User) { export default async function(followee: { id: User['id']; host: User['host']; uri: User['host']; inbox: User['inbox']; sharedInbox: User['sharedInbox']; }, follower: CacheableUser) {
const request = await FollowRequests.findOneBy({ const request = await FollowRequests.findOneBy({
followeeId: followee.id, followeeId: followee.id,
followerId: follower.id, followerId: follower.id,