From cd4df400351998039016e11e707093231caef7d0 Mon Sep 17 00:00:00 2001 From: aster <137767097+aster-void@users.noreply.github.com> Date: Tue, 18 Feb 2025 16:53:45 +0900 Subject: [PATCH 1/3] perf: cache guid -> userid --- bun.lock | 5 ++++- server/package.json | 1 + server/src/firebase/auth/db.ts | 33 +++++++++++++++++++++++++++++---- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/bun.lock b/bun.lock index 1f8b457e..7c0ffb53 100644 --- a/bun.lock +++ b/bun.lock @@ -32,6 +32,7 @@ "express": "^4.18.2", "express-async-errors": "^3.1.1", "firebase-admin": "^12.2.0", + "lru-cache": "^11.0.2", "sharp": "^0.33.5", "socket.io": "^4.7.5", "sql-formatter": "^15.4.10", @@ -957,7 +958,7 @@ "loose-envify": ["loose-envify@1.4.0", "", { "dependencies": { "js-tokens": "^3.0.0 || ^4.0.0" }, "bin": { "loose-envify": "cli.js" } }, "sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q=="], - "lru-cache": ["lru-cache@6.0.0", "", { "dependencies": { "yallist": "^4.0.0" } }, "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA=="], + "lru-cache": ["lru-cache@11.0.2", "", {}, "sha512-123qHRfJBmo2jXDbo/a5YOQrJoHF/GNQTLzQ5+IdK5pWpceK17yRc6ozlWd25FxvGKQbIUs91fDFkXmDHTKcyA=="], "lru-memoizer": ["lru-memoizer@2.3.0", "", { "dependencies": { "lodash.clonedeep": "^4.5.0", "lru-cache": "6.0.0" } }, "sha512-GXn7gyHAMhO13WSKrIiNfztwxodVsP8IoZ3XfrJV4yH2x0/OeTO/FIaAHTY5YekdGgW94njfuKmyyt1E0mR6Ug=="], @@ -1389,6 +1390,8 @@ "log-update/slice-ansi": ["slice-ansi@7.1.0", "", { "dependencies": { "ansi-styles": "^6.2.1", "is-fullwidth-code-point": "^5.0.0" } }, "sha512-bSiSngZ/jWeX93BqeIAbImyTbEihizcwNjFoRUIY/T1wWQsfsm2Vw1agPKylXvQTU7iASGdHhyqRlqQzfz+Htg=="], + "lru-memoizer/lru-cache": ["lru-cache@6.0.0", "", { "dependencies": { "yallist": "^4.0.0" } }, "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA=="], + "nearley/commander": ["commander@2.20.3", "", {}, "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ=="], "next/postcss": ["postcss@8.4.31", "", { "dependencies": { "nanoid": "^3.3.6", "picocolors": "^1.0.0", "source-map-js": "^1.0.2" } }, "sha512-PS08Iboia9mts/2ygV3eLpY5ghnUcfLV/EXTOW1E2qYxJKGGBUtNjN76FYHnMs36RmARn41bC0AZmn+rR0OVpQ=="], diff --git a/server/package.json b/server/package.json index c08b3ae4..6fcd460d 100644 --- a/server/package.json +++ b/server/package.json @@ -25,6 +25,7 @@ "express": "^4.18.2", "express-async-errors": "^3.1.1", "firebase-admin": "^12.2.0", + "lru-cache": "^11.0.2", "sharp": "^0.33.5", "socket.io": "^4.7.5", "sql-formatter": "^15.4.10", diff --git a/server/src/firebase/auth/db.ts b/server/src/firebase/auth/db.ts index 2344c5d8..f3a511f6 100644 --- a/server/src/firebase/auth/db.ts +++ b/server/src/firebase/auth/db.ts @@ -1,9 +1,9 @@ -import type { IDToken, UserID } from "common/types"; -import type { Request } from "express"; -import { getGUID, getGUIDFromToken } from "./lib"; - import { error } from "common/lib/panic"; +import type { GUID, IDToken, UserID } from "common/types"; +import type { Request } from "express"; +import { LRUCache } from "lru-cache"; import { prisma } from "../../database/client"; +import { getGUID, getGUIDFromToken } from "./lib"; /** * REQUIRE: cookieParser middleware before this * THROWS: if idToken is not present in request cookie, or when the token is not valid. @@ -17,8 +17,20 @@ import { prisma } from "../../database/client"; * } * ``` **/ + +const guid_userid_cache = new LRUCache({ + max: 100, +}); + export async function getUserId(req: Request): Promise { const guid = await getGUID(req); + + const cache = guid_userid_cache.get(guid); + if (cache) { + console.log(`[CACHE HIT] ${guid} -> ${cache}`); + return cache; + } + const user = await prisma.user.findUnique({ where: { guid: guid, @@ -28,17 +40,30 @@ export async function getUserId(req: Request): Promise { }, }); if (!user) error("auth error: unauthorized", 401); + + guid_userid_cache.set(guid, user.id); return user.id; } export async function getUserIdFromToken(token: IDToken): Promise { const guid = await getGUIDFromToken(token); + + const cache = guid_userid_cache.get(guid); + if (cache) { + return cache; + } + const user = await prisma.user.findUnique({ where: { guid: guid, }, + select: { + id: true, + }, }); if (!user) throw new Error("User not found!"); + + guid_userid_cache.set(guid, user.id); return user.id; } From 20a5562988d9473f8f1e23301aaf0a6ecbbe00a4 Mon Sep 17 00:00:00 2001 From: Yuki Kobayashi <137767097+aster-void@users.noreply.github.com> Date: Wed, 19 Feb 2025 14:24:39 +0900 Subject: [PATCH 2/3] Fix merge error --- server/src/firebase/auth/db.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/src/firebase/auth/db.ts b/server/src/firebase/auth/db.ts index cc60ea06..df8d23a8 100644 --- a/server/src/firebase/auth/db.ts +++ b/server/src/firebase/auth/db.ts @@ -1,7 +1,7 @@ import type { GUID, IDToken, UserID } from "common/types"; import { LRUCache } from "lru-cache"; import { prisma } from "../../database/client"; -import { getGUID, getGUIDFromToken } from "./lib +import { getGUID, getGUIDFromToken } from "./lib"; import type { Context } from "hono"; import { error } from "../../lib/error"; From 6713626f61a0d6fa0816fe4ee628bc0010692ab5 Mon Sep 17 00:00:00 2001 From: Yuki Kobayashi <137767097+aster-void@users.noreply.github.com> Date: Wed, 19 Feb 2025 14:27:54 +0900 Subject: [PATCH 3/3] style: sort imports --- server/src/firebase/auth/db.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/src/firebase/auth/db.ts b/server/src/firebase/auth/db.ts index df8d23a8..9f4865a5 100644 --- a/server/src/firebase/auth/db.ts +++ b/server/src/firebase/auth/db.ts @@ -1,9 +1,9 @@ import type { GUID, IDToken, UserID } from "common/types"; +import type { Context } from "hono"; import { LRUCache } from "lru-cache"; import { prisma } from "../../database/client"; -import { getGUID, getGUIDFromToken } from "./lib"; -import type { Context } from "hono"; import { error } from "../../lib/error"; +import { getGUID, getGUIDFromToken } from "./lib"; const guid_userid_cache = new LRUCache({ max: 100,