From 836ae732f639e6ec5e486bebe8458adb6033137c Mon Sep 17 00:00:00 2001
From: syuilo <Syuilotan@yahoo.co.jp>
Date: Mon, 21 Mar 2022 05:26:42 +0900
Subject: [PATCH] perf(server): cache nodeinfo to reduce db query

---
 packages/backend/src/server/nodeinfo.ts | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/packages/backend/src/server/nodeinfo.ts b/packages/backend/src/server/nodeinfo.ts
index f4b56fc8a5..a6cb69aab2 100644
--- a/packages/backend/src/server/nodeinfo.ts
+++ b/packages/backend/src/server/nodeinfo.ts
@@ -4,6 +4,7 @@ import { fetchMeta } from '@/misc/fetch-meta.js';
 import { Users, Notes } from '@/models/index.js';
 import { MoreThan } from 'typeorm';
 import { MAX_NOTE_TEXT_LENGTH } from '@/const.js';
+import { Cache } from '@/misc/cache';
 
 const router = new Router();
 
@@ -81,15 +82,17 @@ const nodeinfo2 = async () => {
 	};
 };
 
+const cache = new Cache<Awaited<ReturnType<typeof nodeinfo2>>>(1000 * 60 * 10);
+
 router.get(nodeinfo2_1path, async ctx => {
-	const base = await nodeinfo2();
+	const base = await cache.fetch(null, () => nodeinfo2());
 
 	ctx.body = { version: '2.1', ...base };
 	ctx.set('Cache-Control', 'public, max-age=600');
 });
 
 router.get(nodeinfo2_0path, async ctx => {
-	const base = await nodeinfo2();
+	const base = await cache.fetch(null, () => nodeinfo2());
 
 	delete base.software.repository;