Skip to content

perf: cache guid -> userid #658

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion bun.lock
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"dotenv": "^16.4.5",
"dotenv-cli": "^7.4.2",
"firebase-admin": "^12.2.0",
"lru-cache": "^11.0.2",
"hono": "^4.7.1",
"sharp": "^0.33.5",
"socket.io": "^4.7.5",
Expand Down Expand Up @@ -902,7 +903,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=="],

Expand Down Expand Up @@ -1274,6 +1275,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=="],
Expand Down
1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"dotenv": "^16.4.5",
"dotenv-cli": "^7.4.2",
"firebase-admin": "^12.2.0",
"lru-cache": "^11.0.2",
"hono": "^4.7.1",
"sharp": "^0.33.5",
"socket.io": "^4.7.5",
Expand Down
42 changes: 26 additions & 16 deletions server/src/firebase/auth/db.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
import type { IDToken, UserID } from "common/types";
import { getGUID, getGUIDFromToken } from "./lib";

import type { GUID, IDToken, UserID } from "common/types";
import type { Context } from "hono";
import { LRUCache } from "lru-cache";
import { prisma } from "../../database/client";
import { error } from "../../lib/error";
/**
* REQUIRE: cookieParser middleware before this
* THROWS: if idToken is not present in request cookie, or when the token is not valid.
* Expected use case:
* ```js
* let userId: number;
* try {
* userId = await getUserId(req);
* } catch {
* return res.status(401).send("auth error");
* }
* ```
**/
import { getGUID, getGUIDFromToken } from "./lib";

const guid_userid_cache = new LRUCache<GUID, UserID>({
max: 100,
});

export async function getUserId(c: Context): Promise<UserID> {
const guid = await getGUID(c);

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,
Expand All @@ -28,17 +27,28 @@ export async function getUserId(c: Context): Promise<UserID> {
},
});
if (!user) error("auth error: unauthorized", 401);
guid_userid_cache.set(guid, user.id);
return user.id;
}

export async function getUserIdFromToken(token: IDToken): Promise<UserID> {
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) error("User not found!", 401);
guid_userid_cache.set(guid, user.id);
return user.id;
}

Expand Down