From d3c8368b0c3f037a87a2a8c9acfc5a54cf1d06e1 Mon Sep 17 00:00:00 2001 From: syuilo Date: Thu, 18 Mar 2021 10:19:30 +0900 Subject: [PATCH] Improve API performance --- src/server/api/authenticate.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/server/api/authenticate.ts b/src/server/api/authenticate.ts index 0374ca35ea..7404c477fd 100644 --- a/src/server/api/authenticate.ts +++ b/src/server/api/authenticate.ts @@ -3,12 +3,18 @@ import { User } from '../../models/entities/user'; import { Users, AccessTokens, Apps } from '../../models'; import { AccessToken } from '../../models/entities/access-token'; +const cache = {} as Record; + export default async (token: string): Promise<[User | null | undefined, AccessToken | null | undefined]> => { if (token == null) { return [null, null]; } if (isNativeToken(token)) { + if (cache[token]) { // TODO: キャッシュされてから一定時間経過していたら破棄する + return [cache[token], null]; + } + // Fetch user const user = await Users .findOne({ token }); @@ -17,8 +23,11 @@ export default async (token: string): Promise<[User | null | undefined, AccessTo throw new Error('user not found'); } + cache[token] = user; + return [user, null]; } else { + // TODO: cache const accessToken = await AccessTokens.findOne({ where: [{ hash: token.toLowerCase() // app