From 3e663f975e562c1ebcdb545eebb77d48ed17a2d9 Mon Sep 17 00:00:00 2001 From: naskya Date: Sun, 17 Sep 2023 08:04:22 +0900 Subject: [PATCH] perf: cache stats (close #75) --- packages/backend/src/server/api/endpoints/stats.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/backend/src/server/api/endpoints/stats.ts b/packages/backend/src/server/api/endpoints/stats.ts index 9d15774b..737873be 100644 --- a/packages/backend/src/server/api/endpoints/stats.ts +++ b/packages/backend/src/server/api/endpoints/stats.ts @@ -1,6 +1,7 @@ import { Instances, Notes, Users } from "@/models/index.js"; import define from "../define.js"; import { IsNull } from "typeorm"; +import { Cache } from "@/misc/cache.js"; export const meta = { requireCredential: false, @@ -60,7 +61,7 @@ export const paramDef = { required: [], } as const; -export default define(meta, paramDef, async () => { +const stats = async () => { const [ notesCount, originalNotesCount, @@ -107,4 +108,10 @@ export default define(meta, paramDef, async () => { driveUsageLocal, driveUsageRemote, }; +}; + +const cache = new Cache>>("stats", 60 * 10); + +export default define(meta, paramDef, async () => { + return await cache.fetch(null, () => stats()); });