Skip to content

Commit 49ef045

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

3 files changed

Lines changed: 31 additions & 17 deletions

File tree

bun.lock

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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)