Skip to content

Commit c1dfc1d

Browse files
authored
perf: cache guid -> userid (#658)
# PRの概要 ## 具体的な変更内容 ## 影響範囲 ## 動作要件 ## 補足 ## レビューリクエストを出す前にチェック! - [x] 改めてセルフレビューしたか - [x] 手動での動作検証を行ったか - [ ] server の機能追加ならば、テストを書いたか - 理由: 書いた | server の機能追加ではない - [ ] 間違った使い方が存在するならば、それのドキュメントをコメントで書いたか - 理由: 書いた | 間違った使い方は存在しない - [ ] わかりやすいPRになっているか <!-- レビューリクエスト後は、Slackでもメンションしてお願いすることを推奨します。 -->
1 parent da11920 commit c1dfc1d

File tree

3 files changed

+31
-17
lines changed

3 files changed

+31
-17
lines changed

bun.lock

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"dotenv": "^16.4.5",
3232
"dotenv-cli": "^7.4.2",
3333
"firebase-admin": "^12.2.0",
34+
"lru-cache": "^11.0.2",
3435
"hono": "^4.7.1",
3536
"sharp": "^0.33.5",
3637
"socket.io": "^4.7.5",
@@ -902,7 +903,7 @@
902903

903904
"loose-envify": ["[email protected]", "", { "dependencies": { "js-tokens": "^3.0.0 || ^4.0.0" }, "bin": { "loose-envify": "cli.js" } }, "sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q=="],
904905

905-
"lru-cache": ["lru-cache@6.0.0", "", { "dependencies": { "yallist": "^4.0.0" } }, "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA=="],
906+
"lru-cache": ["lru-cache@11.0.2", "", {}, "sha512-123qHRfJBmo2jXDbo/a5YOQrJoHF/GNQTLzQ5+IdK5pWpceK17yRc6ozlWd25FxvGKQbIUs91fDFkXmDHTKcyA=="],
906907

907908
"lru-memoizer": ["[email protected]", "", { "dependencies": { "lodash.clonedeep": "^4.5.0", "lru-cache": "6.0.0" } }, "sha512-GXn7gyHAMhO13WSKrIiNfztwxodVsP8IoZ3XfrJV4yH2x0/OeTO/FIaAHTY5YekdGgW94njfuKmyyt1E0mR6Ug=="],
908909

@@ -1274,6 +1275,8 @@
12741275

12751276
"log-update/slice-ansi": ["[email protected]", "", { "dependencies": { "ansi-styles": "^6.2.1", "is-fullwidth-code-point": "^5.0.0" } }, "sha512-bSiSngZ/jWeX93BqeIAbImyTbEihizcwNjFoRUIY/T1wWQsfsm2Vw1agPKylXvQTU7iASGdHhyqRlqQzfz+Htg=="],
12761277

1278+
"lru-memoizer/lru-cache": ["[email protected]", "", { "dependencies": { "yallist": "^4.0.0" } }, "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA=="],
1279+
12771280
"nearley/commander": ["[email protected]", "", {}, "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ=="],
12781281

12791282
"next/postcss": ["[email protected]", "", { "dependencies": { "nanoid": "^3.3.6", "picocolors": "^1.0.0", "source-map-js": "^1.0.2" } }, "sha512-PS08Iboia9mts/2ygV3eLpY5ghnUcfLV/EXTOW1E2qYxJKGGBUtNjN76FYHnMs36RmARn41bC0AZmn+rR0OVpQ=="],

server/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"dotenv": "^16.4.5",
2525
"dotenv-cli": "^7.4.2",
2626
"firebase-admin": "^12.2.0",
27+
"lru-cache": "^11.0.2",
2728
"hono": "^4.7.1",
2829
"sharp": "^0.33.5",
2930
"socket.io": "^4.7.5",

server/src/firebase/auth/db.ts

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
1-
import type { IDToken, UserID } from "common/types";
2-
import { getGUID, getGUIDFromToken } from "./lib";
3-
1+
import type { GUID, IDToken, UserID } from "common/types";
42
import type { Context } from "hono";
3+
import { LRUCache } from "lru-cache";
54
import { prisma } from "../../database/client";
65
import { error } from "../../lib/error";
7-
/**
8-
* REQUIRE: cookieParser middleware before this
9-
* THROWS: if idToken is not present in request cookie, or when the token is not valid.
10-
* Expected use case:
11-
* ```js
12-
* let userId: number;
13-
* try {
14-
* userId = await getUserId(req);
15-
* } catch {
16-
* return res.status(401).send("auth error");
17-
* }
18-
* ```
19-
**/
6+
import { getGUID, getGUIDFromToken } from "./lib";
7+
8+
const guid_userid_cache = new LRUCache<GUID, UserID>({
9+
max: 100,
10+
});
11+
2012
export async function getUserId(c: Context): Promise<UserID> {
2113
const guid = await getGUID(c);
14+
15+
const cache = guid_userid_cache.get(guid);
16+
if (cache) {
17+
console.log(`[CACHE HIT] ${guid} -> ${cache}`);
18+
return cache;
19+
}
20+
2221
const user = await prisma.user.findUnique({
2322
where: {
2423
guid: guid,
@@ -28,17 +27,28 @@ export async function getUserId(c: Context): Promise<UserID> {
2827
},
2928
});
3029
if (!user) error("auth error: unauthorized", 401);
30+
guid_userid_cache.set(guid, user.id);
3131
return user.id;
3232
}
3333

3434
export async function getUserIdFromToken(token: IDToken): Promise<UserID> {
3535
const guid = await getGUIDFromToken(token);
36+
37+
const cache = guid_userid_cache.get(guid);
38+
if (cache) {
39+
return cache;
40+
}
41+
3642
const user = await prisma.user.findUnique({
3743
where: {
3844
guid: guid,
3945
},
46+
select: {
47+
id: true,
48+
},
4049
});
4150
if (!user) error("User not found!", 401);
51+
guid_userid_cache.set(guid, user.id);
4252
return user.id;
4353
}
4454

0 commit comments

Comments
 (0)