Skip to content

Commit f467bb7

Browse files
G3rootzomars
andauthored
perf: use LRU cache for getServerSession (#8339)
* chore: add lru package * feat: use LRU cache * fix: type * Updates package json --------- Co-authored-by: zomars <[email protected]>
1 parent 597b14c commit f467bb7

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

packages/features/auth/lib/getServerSession.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { LRUCache } from "lru-cache";
12
import type { GetServerSidePropsContext, NextApiRequest, NextApiResponse } from "next";
23
import type { AuthOptions, Session } from "next-auth";
34
import { getToken } from "next-auth/jwt";
@@ -9,13 +10,8 @@ import prisma from "@calcom/prisma";
910
/**
1011
* Stores the session in memory using the stringified token as the key.
1112
*
12-
* This is fine for production as each lambda will be recycled before this
13-
* becomes large enough to cause issues.
14-
*
15-
* If we want to get extra spicy we could store this in edge config or similar
16-
* so we can TTL things and benefit from faster retrievals than prisma.
1713
*/
18-
const UNSTABLE_SESSION_CACHE = new Map<string, Session>();
14+
const CACHE = new LRUCache<string, Session>({ max: 1000 });
1915

2016
/**
2117
* This is a slimmed down version of the `getServerSession` function from
@@ -44,7 +40,7 @@ export async function getServerSession(options: {
4440
return null;
4541
}
4642

47-
const cachedSession = UNSTABLE_SESSION_CACHE.get(JSON.stringify(token));
43+
const cachedSession = CACHE.get(JSON.stringify(token));
4844

4945
if (cachedSession) {
5046
return cachedSession;
@@ -79,7 +75,7 @@ export async function getServerSession(options: {
7975
},
8076
};
8177

82-
UNSTABLE_SESSION_CACHE.set(JSON.stringify(token), session);
78+
CACHE.set(JSON.stringify(token), session);
8379

8480
return session;
8581
}

packages/features/auth/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"bcryptjs": "^2.4.3",
1515
"handlebars": "^4.7.7",
1616
"jose": "^4.13.1",
17+
"lru-cache": "^9.0.3",
1718
"next-auth": "^4.20.1",
1819
"nodemailer": "^6.7.8",
1920
"otplib": "^12.0.1"

yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4332,6 +4332,7 @@ __metadata:
43324332
bcryptjs: ^2.4.3
43334333
handlebars: ^4.7.7
43344334
jose: ^4.13.1
4335+
lru-cache: ^9.0.3
43354336
next-auth: ^4.20.1
43364337
nodemailer: ^6.7.8
43374338
otplib: ^12.0.1
@@ -27369,6 +27370,13 @@ __metadata:
2736927370
languageName: node
2737027371
linkType: hard
2737127372

27373+
"lru-cache@npm:^9.0.3":
27374+
version: 9.0.3
27375+
resolution: "lru-cache@npm:9.0.3"
27376+
checksum: 218415714d44c113bc3a8d12ceca6dedf310915aa861f9e0df13df1fe6db9833e1b5f1f656e9a117ac82677ad3e499be23bff2f670f2a7c5c186d64a92596b68
27377+
languageName: node
27378+
linkType: hard
27379+
2737227380
"lru-cache@npm:~4.0.0":
2737327381
version: 4.0.2
2737427382
resolution: "lru-cache@npm:4.0.2"

0 commit comments

Comments
 (0)