forked from calcom/cal.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Optimize organization membership queries #13
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
Open
ShashankFC
wants to merge
1
commit into
main
Choose a base branch
from
feature/organization-query-performance
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
packages/lib/server/queries/organisations/membershipCache.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import type { MembershipRole } from "@calcom/prisma/enums"; | ||
|
|
||
| /** | ||
| * In-memory cache for organization membership queries | ||
| * This helps reduce database load for frequently accessed membership data | ||
| */ | ||
|
|
||
| interface MembershipCacheEntry { | ||
| userId: number; | ||
| teamId: number; | ||
| role: MembershipRole; | ||
| timestamp: number; | ||
| } | ||
|
|
||
| const CACHE_TTL_MS = 5 * 60 * 1000; // 5 minutes | ||
| const membershipCache = new Map<string, MembershipCacheEntry>(); | ||
|
|
||
| /** | ||
| * Generates cache key for membership lookup | ||
| */ | ||
| function getCacheKey(userId: number, teamId: number): string { | ||
| return `${userId}:${teamId}`; | ||
| } | ||
|
|
||
| /** | ||
| * Checks if cache entry is still valid | ||
| */ | ||
| function isCacheValid(entry: MembershipCacheEntry): boolean { | ||
| return Date.now() - entry.timestamp <= CACHE_TTL_MS; | ||
| } | ||
|
|
||
| /** | ||
| * Gets cached membership data if available and valid | ||
| */ | ||
| export function getCachedMembership(userId: number, teamId: number): MembershipCacheEntry | null { | ||
| const key = getCacheKey(userId, teamId); | ||
| const entry = membershipCache.get(key); | ||
|
|
||
| if (!entry) { | ||
| return null; | ||
| } | ||
|
|
||
| if (!isCacheValid(entry)) { | ||
| membershipCache.delete(key); | ||
| return null; | ||
| } | ||
|
|
||
| return entry; | ||
| } | ||
|
|
||
| /** | ||
| * Caches membership data | ||
| */ | ||
| export function setCachedMembership( | ||
| userId: number, | ||
| teamId: number, | ||
| role: MembershipRole | ||
| ): void { | ||
| const key = getCacheKey(userId, teamId); | ||
| membershipCache.set(key, { | ||
| userId, | ||
| teamId, | ||
| role, | ||
| timestamp: Date.now(), | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Invalidates cached membership for a user-team pair | ||
| */ | ||
| export function invalidateMembershipCache(userId: number, teamId: number): void { | ||
| const key = getCacheKey(userId, teamId); | ||
| membershipCache.delete(key); | ||
| } | ||
|
|
||
| /** | ||
| * Clears all expired cache entries | ||
| */ | ||
| export function cleanupExpiredCache(): number { | ||
| let removedCount = 0; | ||
|
|
||
| for (const [key, entry] of membershipCache.entries()) { | ||
| if (!isCacheValid(entry)) { | ||
| membershipCache.delete(key); | ||
| removedCount++; | ||
| } | ||
| } | ||
|
|
||
| return removedCount; | ||
| } | ||
|
|
||
| /** | ||
| * Gets current cache statistics | ||
| */ | ||
| export function getCacheStats() { | ||
| return { | ||
| size: membershipCache.size, | ||
| ttlMs: CACHE_TTL_MS, | ||
| }; | ||
| } | ||
|
Comment on lines
+1
to
+100
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correctness: 🟠 [LangGraph v3] The |
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correctness: 🟠 [LangGraph v3] The async functions
isOrganisationAdmin,isOrganisationOwner, andisOrganisationMemberlack error handling. Without try/catch blocks, any errors during the database query will be unhandled, potentially causing the application to crash or behave unpredictably. Wrap the async operations in try/catch to handle potential errors gracefully.📝 Committable Code Suggestion