Skip to content

perf: cache leaderboard aggregation result for 5 minutes#535

Open
xpoes123 wants to merge 1 commit into
qbreader:mainfrom
xpoes123:perf/cache-leaderboard
Open

perf: cache leaderboard aggregation result for 5 minutes#535
xpoes123 wants to merge 1 commit into
qbreader:mainfrom
xpoes123:perf/cache-leaderboard

Conversation

@xpoes123

@xpoes123 xpoes123 commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

The leaderboard endpoint ran two full-collection $unwind aggregations on every request. Since the leaderboard changes infrequently, this is wasteful. This PR adds a 5-minute in-process cache in database/account-info/leaderboard.js so repeated calls — including all limit variants — share a single computed result.

Problem

leaderboard() in database/account-info/leaderboard.js invokes two $unwind aggregation pipelines on every HTTP request with no caching. Under any moderate load the leaderboard endpoint becomes a repeated full-collection scan.

Changes

  • Adds two module-level variables (cachedOverall, cachedAt) and a CACHE_TTL constant (5 minutes).
  • Returns the cached slice early if the cache is still fresh; recomputes and repopulates on expiry.

Risk & testing

No new dependencies. The cache is module-scoped so it resets on server restart — this matches expected behavior. The 5-minute TTL is consistent with how infrequently leaderboard standings change. semistandard and node --check pass.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@xpoes123 xpoes123 changed the title perf: cache leaderboard result with a short TTL perf: cache leaderboard aggregation result for 5 minutes Jun 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant