feat(perf): cache leaderboard API response for 5 minutes and invalidate on opt-out (#1908)#1986
Conversation
|
@harshitanagpal05 is attempting to deploy a commit to the PRIYANSHU DOSHI's projects Team on Vercel. A member of the Team first needs to authorize it. |
GSSoC Label Checklist 🏷️@Priyanshu-byte-coder — please apply the appropriate labels before merging: Difficulty (pick one):
Quality (optional):
Validation (required to score):
|
|
The # Current (broken) — these lines are literally inside the shell script:
- name: Build Next.js app
run: |
...
npm run build
- name: Start Next.js server # ← this is a shell command, not a YAML step
run: npm start &
# Should be (if needed):
- name: Build Next.js app
run: |
...
npm run build
- name: Start Next.js server
run: npm start &However, the e2e workflow already uses
The leaderboard caching changes look good — just fix the |
…YAML syntax in e2e.yml
|
Hi @Priyanshu-byte-coder, I’ve resolved the merge conflicts and addressed the requested |
Summary
Closes #1908
This PR implements a 5-minute caching mechanism for the leaderboard using Next.js
unstable_cacheand Redis (Upstash) to prevent re-aggregating metrics for opted-in users on every request. It also ensures that the cached leaderboard is instantly cleared when a user updates their settings to opt out.Proposed Changes
1. Unified Cache Helper
invalidateLeaderboardCache()insrc/lib/metrics-cache.tsto clear leaderboard entries from both the in-process memory cache and Redis.2. Next.js
unstable_cacheIntegrationsrc/lib/leaderboard.ts:CACHE_REFRESH_SECONDSto300(5 minutes).buildLeaderboard()in Next.jsunstable_cachedynamically keyed by filters (["leaderboard", period]) and configured with a 300-second revalidation time.getLeaderboardData()to tryunstable_cachefirst and fall back to the custom memory/Redis caches on failure (ensuring resilience in non-Next environments/tests).clearLeaderboardCache()to callinvalidateLeaderboardCache()andrevalidateTag("leaderboard")for instant purge on settings opt-out.3. API Route Refactoring
src/app/api/leaderboard/route.ts:export const dynamic = "force-dynamic"to ensure settings modifications trigger instant invalidation, while using the internal 5-minute cache logic to prevent DB loads.Verification