From 6f2e93c6a17a0aca30da6590e66e3b60f67efe4e Mon Sep 17 00:00:00 2001 From: syuilo Date: Mon, 21 Mar 2022 05:21:37 +0900 Subject: [PATCH] perf(server): reduce db query --- packages/backend/src/server/activitypub.ts | 13 ++++++++----- packages/backend/src/server/activitypub/cache.ts | 6 ++++++ packages/backend/src/server/activitypub/featured.ts | 7 ++++--- .../backend/src/server/activitypub/followers.ts | 7 ++++--- .../backend/src/server/activitypub/following.ts | 7 ++++--- packages/backend/src/server/activitypub/outbox.ts | 7 ++++--- 6 files changed, 30 insertions(+), 17 deletions(-) create mode 100644 packages/backend/src/server/activitypub/cache.ts diff --git a/packages/backend/src/server/activitypub.ts b/packages/backend/src/server/activitypub.ts index 21be0a2517..f2d320bb68 100644 --- a/packages/backend/src/server/activitypub.ts +++ b/packages/backend/src/server/activitypub.ts @@ -18,6 +18,7 @@ import { ILocalUser, User } from '@/models/entities/user.js'; import { In } from 'typeorm'; import { renderLike } from '@/remote/activitypub/renderer/like.js'; import { getUserKeypair } from '@/misc/keypair-store.js'; +import { noteCache, userCache } from './activitypub/cache.js'; // Init router const router = new Router(); @@ -65,11 +66,12 @@ router.post('/users/:user/inbox', json(), inbox); router.get('/notes/:note', async (ctx, next) => { if (!isActivityPubReq(ctx)) return await next(); - const note = await Notes.findOne({ + // TODO: typeorm 3.0にしたら .then(x => x || null) は消せる + const note = await noteCache.fetch(ctx.params.note, () => Notes.findOne({ id: ctx.params.note, visibility: In(['public' as const, 'home' as const]), localOnly: false, - }); + }).then(x => x || null)); if (note == null) { ctx.status = 404; @@ -148,7 +150,7 @@ router.get('/users/:user/publickey', async ctx => { }); // user -async function userInfo(ctx: Router.RouterContext, user: User | undefined) { +async function userInfo(ctx: Router.RouterContext, user: User | undefined | null) { if (user == null) { ctx.status = 404; return; @@ -164,11 +166,12 @@ router.get('/users/:user', async (ctx, next) => { const userId = ctx.params.user; - const user = await Users.findOne({ + // TODO: typeorm 3.0にしたら .then(x => x || null) は消せる + const user = await userCache.fetch(userId, () => Users.findOne({ id: userId, host: null, isSuspended: false, - }); + }).then(x => x || null)); await userInfo(ctx, user); }); diff --git a/packages/backend/src/server/activitypub/cache.ts b/packages/backend/src/server/activitypub/cache.ts new file mode 100644 index 0000000000..4ebec77dc2 --- /dev/null +++ b/packages/backend/src/server/activitypub/cache.ts @@ -0,0 +1,6 @@ +import { Cache } from "@/misc/cache"; +import { Note } from "@/models/entities/note"; +import { User } from "@/models/entities/user"; + +export const userCache = new Cache(1000 * 60 * 30); +export const noteCache = new Cache(1000 * 60 * 30); diff --git a/packages/backend/src/server/activitypub/featured.ts b/packages/backend/src/server/activitypub/featured.ts index 129881a718..d06a28a9ca 100644 --- a/packages/backend/src/server/activitypub/featured.ts +++ b/packages/backend/src/server/activitypub/featured.ts @@ -5,15 +5,16 @@ import renderOrderedCollection from '@/remote/activitypub/renderer/ordered-colle import { setResponseType } from '../activitypub.js'; import renderNote from '@/remote/activitypub/renderer/note.js'; import { Users, Notes, UserNotePinings } from '@/models/index.js'; +import { userCache } from './cache.js'; export default async (ctx: Router.RouterContext) => { const userId = ctx.params.user; - // Verify user - const user = await Users.findOne({ + // TODO: typeorm 3.0にしたら .then(x => x || null) は消せる + const user = await userCache.fetch(userId, () => Users.findOne({ id: userId, host: null, - }); + }).then(x => x || null)); if (user == null) { ctx.status = 404; diff --git a/packages/backend/src/server/activitypub/followers.ts b/packages/backend/src/server/activitypub/followers.ts index 5d1d7c59eb..fdae9dd928 100644 --- a/packages/backend/src/server/activitypub/followers.ts +++ b/packages/backend/src/server/activitypub/followers.ts @@ -10,6 +10,7 @@ import renderFollowUser from '@/remote/activitypub/renderer/follow-user.js'; import { setResponseType } from '../activitypub.js'; import { Users, Followings, UserProfiles } from '@/models/index.js'; import { LessThan } from 'typeorm'; +import { userCache } from './cache.js'; export default async (ctx: Router.RouterContext) => { const userId = ctx.params.user; @@ -27,11 +28,11 @@ export default async (ctx: Router.RouterContext) => { return; } - // Verify user - const user = await Users.findOne({ + // TODO: typeorm 3.0にしたら .then(x => x || null) は消せる + const user = await userCache.fetch(userId, () => Users.findOne({ id: userId, host: null, - }); + }).then(x => x || null)); if (user == null) { ctx.status = 404; diff --git a/packages/backend/src/server/activitypub/following.ts b/packages/backend/src/server/activitypub/following.ts index 23110ce873..eb1b7a9d8c 100644 --- a/packages/backend/src/server/activitypub/following.ts +++ b/packages/backend/src/server/activitypub/following.ts @@ -11,6 +11,7 @@ import { setResponseType } from '../activitypub.js'; import { Users, Followings, UserProfiles } from '@/models/index.js'; import { LessThan, FindConditions } from 'typeorm'; import { Following } from '@/models/entities/following.js'; +import { userCache } from './cache.js'; export default async (ctx: Router.RouterContext) => { const userId = ctx.params.user; @@ -28,11 +29,11 @@ export default async (ctx: Router.RouterContext) => { return; } - // Verify user - const user = await Users.findOne({ + // TODO: typeorm 3.0にしたら .then(x => x || null) は消せる + const user = await userCache.fetch(userId, () => Users.findOne({ id: userId, host: null, - }); + }).then(x => x || null)); if (user == null) { ctx.status = 404; diff --git a/packages/backend/src/server/activitypub/outbox.ts b/packages/backend/src/server/activitypub/outbox.ts index 57c126752a..db2a18efcd 100644 --- a/packages/backend/src/server/activitypub/outbox.ts +++ b/packages/backend/src/server/activitypub/outbox.ts @@ -15,6 +15,7 @@ import { Users, Notes } from '@/models/index.js'; import { makePaginationQuery } from '../api/common/make-pagination-query.js'; import { Brackets } from 'typeorm'; import { Note } from '@/models/entities/note.js'; +import { userCache } from './cache.js'; export default async (ctx: Router.RouterContext) => { const userId = ctx.params.user; @@ -35,11 +36,11 @@ export default async (ctx: Router.RouterContext) => { return; } - // Verify user - const user = await Users.findOne({ + // TODO: typeorm 3.0にしたら .then(x => x || null) は消せる + const user = await userCache.fetch(userId, () => Users.findOne({ id: userId, host: null, - }); + }).then(x => x || null)); if (user == null) { ctx.status = 404;