perf(server): reduce db query
This commit is contained in:
6
packages/backend/src/server/activitypub/cache.ts
Normal file
6
packages/backend/src/server/activitypub/cache.ts
Normal file
@@ -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<User | null>(1000 * 60 * 30);
|
||||
export const noteCache = new Cache<Note | null>(1000 * 60 * 30);
|
@@ -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;
|
||||
|
@@ -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;
|
||||
|
@@ -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;
|
||||
|
@@ -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;
|
||||
|
Reference in New Issue
Block a user