feat(gamification): cache leaderboard results and clamp max limit - #1191
Merged
RUKAYAT-CODER merged 1 commit intoAug 7, 2026
Merged
Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Implements #1159 — caches the top-N leaderboard in Redis with a short TTL and clamps the caller-supplied
limitto a documented maximum, so repeated reads skip the expensiveORDER BY ... DESCsort and a caller can never force an unbounded full-table sort / oversized response.Changes
src/gamification/leaderboards/leaderboards.service.tsgetTopPlayers()andgetBadgeLeaderboard()now coercelimitinto the safe range[1, MAX_LEADERBOARD_LIMIT](MAX_LEADERBOARD_LIMIT = 100, matching the cap already used by the paginatedgetLeaderboard) instead of passing it straight intotake()/limit(). Non-finite values fall back to the documented maximum.REDIS_CLIENTconnection with a 60-second TTL (LEADERBOARD_CACHE_TTL_SECONDS). Cache keys are scoped by leaderboard type, badge category, and clamped limit. A Redis read/write failure fails open to the database, so leaderboard reads never break.@Optional(), so the service keeps working when Redis is not wired up.Tests
src/gamification/leaderboards/leaderboards.service.spec.ts(13 cases) covering:Notes
user_progress.totalPointsis already@Index()-backed, satisfying the index-backed ordering criterion.maincurrently carries pre-existing lint/typecheck/build failures in unrelated files (e.g.src/rbac/roles/roles.controller.ts); this PR introduces no new failures.closes #1159