Skip to content

feat(perf): cache leaderboard API response for 5 minutes and invalidate on opt-out (#1908)#1986

Open
harshitanagpal05 wants to merge 3 commits into
Priyanshu-byte-coder:mainfrom
harshitanagpal05:fix/cache-leaderboard-1908
Open

feat(perf): cache leaderboard API response for 5 minutes and invalidate on opt-out (#1908)#1986
harshitanagpal05 wants to merge 3 commits into
Priyanshu-byte-coder:mainfrom
harshitanagpal05:fix/cache-leaderboard-1908

Conversation

@harshitanagpal05
Copy link
Copy Markdown
Contributor

Summary

Closes #1908

This PR implements a 5-minute caching mechanism for the leaderboard using Next.js unstable_cache and 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

  • Added invalidateLeaderboardCache() in src/lib/metrics-cache.ts to clear leaderboard entries from both the in-process memory cache and Redis.

2. Next.js unstable_cache Integration

  • Modified src/lib/leaderboard.ts:
    • Updated CACHE_REFRESH_SECONDS to 300 (5 minutes).
    • Wrapped buildLeaderboard() in Next.js unstable_cache dynamically keyed by filters (["leaderboard", period]) and configured with a 300-second revalidation time.
    • Modified getLeaderboardData() to try unstable_cache first and fall back to the custom memory/Redis caches on failure (ensuring resilience in non-Next environments/tests).
    • Modified clearLeaderboardCache() to call invalidateLeaderboardCache() and revalidateTag("leaderboard") for instant purge on settings opt-out.

3. API Route Refactoring

  • Modified src/app/api/leaderboard/route.ts:
    • Set 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

  • Verified that all cache validation and settings invalidation test suites pass successfully:
    npx vitest run test/leaderboard-cache.test.ts test/leaderboard-cache-invalidation.test.ts

@vercel
Copy link
Copy Markdown

vercel Bot commented Jun 3, 2026

@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.

@github-actions github-actions Bot added gssoc26 GSSoC 2026 contribution type:feature GSSoC type bonus: new feature type:performance GSSoC type bonus: performance (+15 pts) labels Jun 3, 2026
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 3, 2026

GSSoC Label Checklist 🏷️

@Priyanshu-byte-coder — please apply the appropriate labels before merging:

Difficulty (pick one):

  • level:beginner — 20 pts
  • level:intermediate — 35 pts
  • level:advanced — 55 pts
  • level:critical — 80 pts

Quality (optional):

  • quality:clean — ×1.2 multiplier
  • quality:exceptional — ×1.5 multiplier

Validation (required to score):

  • gssoc:approved — counts for points
  • gssoc:invalid / gssoc:spam / gssoc:ai-slop — does not score

Type labels (type:*) are auto-detected from files and title. Review and adjust if needed.
Points formula: (difficulty × quality_multiplier) + type_bonus

@github-actions github-actions Bot added the type:devops GSSoC type bonus: devops (+15 pts) label Jun 3, 2026
@Priyanshu-byte-coder
Copy link
Copy Markdown
Owner

The .github/workflows/e2e.yml change has a YAML indentation error. The two new steps (Start Next.js server and Wait for server) were added inside the run: | block of the Build Next.js app step, rather than as separate top-level steps:

# 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 playwright.config.mjs's webServer config to start the server automatically — so these two steps may not be needed at all. Please either:

  1. Remove the two malformed steps entirely, or
  2. Fix their indentation to be proper YAML steps (dedented to align with other steps)

The leaderboard caching changes look good — just fix the e2e.yml and push.

@Priyanshu-byte-coder Priyanshu-byte-coder added the needs-triage Needs maintainer triage label Jun 4, 2026
@harshitanagpal05
Copy link
Copy Markdown
Contributor Author

Hi @Priyanshu-byte-coder, I’ve resolved the merge conflicts and addressed the requested e2e.yml workflow fixes.
The PR branch has been updated. Could you please review it whenever you get a chance?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gssoc26 GSSoC 2026 contribution needs-triage Needs maintainer triage type:devops GSSoC type bonus: devops (+15 pts) type:feature GSSoC type bonus: new feature type:performance GSSoC type bonus: performance (+15 pts)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[PERF] Cache leaderboard API response for 5 minutes to reduce database load

2 participants