Scheduling infrastructure for absolutely everyone.
perf: use LRU cache for getServerSession (#8339)
Nafees Nazikf467bb795715
diff --git a/packages/features/auth/lib/getServerSession.ts b/packages/features/auth/lib/getServerSession.tsindex 6a43c8fe3aa..5368484be33 100644--- a/packages/features/auth/lib/getServerSession.ts+++ b/packages/features/auth/lib/getServerSession.ts@@ -1,3 +1,4 @@+import { LRUCache } from "lru-cache";import type { GetServerSidePropsContext, NextApiRequest, NextApiResponse } from "next";import type { AuthOptions, Session } from "next-auth";import { getToken } from "next-auth/jwt";@@ -9,13 +10,8 @@ import prisma from "@calcom/prisma";/*** Stores the session in memory using the stringified token as the key.*- * This is fine for production as each lambda will be recycled before this- * becomes large enough to cause issues.- *- * If we want to get extra spicy we could store this in edge config or similar- * so we can TTL things and benefit from faster retrievals than prisma.*/-const UNSTABLE_SESSION_CACHE = new Map<string, Session>();+const CACHE = new LRUCache<string, Session>({ max: 1000 });/*** This is a slimmed down version of the `getServerSession` function from@@ -44,7 +40,7 @@ export async function getServerSession(options: {return null;}- const cachedSession = UNSTABLE_SESSION_CACHE.get(JSON.stringify(token));+ const cachedSession = CACHE.get(JSON.stringify(token));if (cachedSession) {return cachedSession;@@ -79,7 +75,7 @@ export async function getServerSession(options: {},};- UNSTABLE_SESSION_CACHE.set(JSON.stringify(token), session);+ CACHE.set(JSON.stringify(token), session);return session;}diff --git a/packages/features/auth/package.json b/packages/features/auth/package.jsonindex 35d7df8de53..e4a1262564d 100644--- a/packages/features/auth/package.json+++ b/packages/features/auth/package.json@@ -14,6 +14,7 @@"bcryptjs": "^2.4.3","handlebars": "^4.7.7","jose": "^4.13.1",+ "lru-cache": "^9.0.3","next-auth": "^4.20.1","nodemailer": "^6.7.8","otplib": "^12.0.1"diff --git a/yarn.lock b/yarn.lockindex 9f86ec4ba69..dbe41bd94dc 100644--- a/yarn.lock+++ b/yarn.lock@@ -4332,6 +4332,7 @@ __metadata:bcryptjs: ^2.4.3handlebars: ^4.7.7jose: ^4.13.1+ lru-cache: ^9.0.3next-auth: ^4.20.1nodemailer: ^6.7.8otplib: ^12.0.1@@ -27369,6 +27370,13 @@ __metadata:languageName: nodelinkType: hard+"lru-cache@npm:^9.0.3":+ version: 9.0.3+ resolution: "lru-cache@npm:9.0.3"+ checksum: 218415714d44c113bc3a8d12ceca6dedf310915aa861f9e0df13df1fe6db9833e1b5f1f656e9a117ac82677ad3e499be23bff2f670f2a7c5c186d64a92596b68+ languageName: node+ linkType: hard+"lru-cache@npm:~4.0.0":version: 4.0.2resolution: "lru-cache@npm:4.0.2"