From 6b753b05d61f8583ea9b9f04cacb0d7cf5a69764 Mon Sep 17 00:00:00 2001 From: syuilo Date: Mon, 22 Mar 2021 22:28:09 +0900 Subject: [PATCH] perf(server): Cache user instance actor --- src/services/instance-actor.ts | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/services/instance-actor.ts b/src/services/instance-actor.ts index 74591846f..a6dadb0cd 100644 --- a/src/services/instance-actor.ts +++ b/src/services/instance-actor.ts @@ -1,17 +1,27 @@ import { createSystemUser } from './create-system-user'; import { ILocalUser } from '../models/entities/user'; import { Users } from '../models'; +import { Cache } from '../misc/cache'; const ACTOR_USERNAME = 'instance.actor' as const; +const cache = new Cache(Infinity); + export async function getInstanceActor(): Promise { + const cached = cache.get(null); + if (cached) return cached; + const user = await Users.findOne({ host: null, username: ACTOR_USERNAME - }); + }) as ILocalUser | undefined; - if (user) return user as ILocalUser; - - const created = await createSystemUser(ACTOR_USERNAME); - return created as ILocalUser; + if (user) { + cache.set(null, user); + return user; + } else { + const created = await createSystemUser(ACTOR_USERNAME) as ILocalUser; + cache.set(null, created); + return created; + } }