Skip to content

Conversation

@LuckySilver0021
Copy link

Changes

  • Added backend/src/utils/all-time-leaderboard-cache.ts with 15min TTL
  • backend/src/dal/leaderboards.ts now checks cache HIT/MISS first
  • backend/src/types/result.ts calls cache.clear() on result submit
  • Fixed TypeScript lint errors (interface→type, any→unknown)

Benefits

  • Reduces MongoDB load during peak traffic
  • Cache auto-expires every 15 mins

Closes #
(@LuckySilver0021)

@monkeytypegeorge monkeytypegeorge added the backend Server stuff label Jan 25, 2026
@fehmer fehmer changed the title feat: added 15 minutes of TTL for 1st page of all time leaderboard feat: added 15 minutes of TTL for 1st page of all time leaderboard (@LuckySilver0021) Jan 27, 2026
@@ -0,0 +1,40 @@
type AllTimeCacheKey = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add mode2 to the cache key

};

type CacheEntry = {
data: unknown[];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep the type

type CacheEntry = {
data: unknown[];
count: number;
timestamp: number;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need a timestamp and only clear using .clear()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the timestamp is removed then how will we we keep track of the 15 minutes?
or maybe What Im thinking is youre trying to say that the data will be cached until the data is changed explicitly like someone came into the leaderboard or someone overtook someone's position.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check the discussion in the original pr: #7415 (comment)

We know when the leaderboard is refreshed and can invalidate the cache at this time.

invalidate/empty the cache on LeaderboardDal.update


class AllTimeLeaderboardCache {
private cache = new Map<string, CacheEntry>();
private readonly TTL = 900_000; // == 15 minutes of TTL
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

const cacheKey = this.getKey(key);
const entry = this.cache.get(cacheKey);

if (!entry || Date.now() - entry.timestamp > this.TTL) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

try {
allTimeLeaderboardCache.clear();
console.log("All-time leaderboard cache cleared");
} catch (error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how can this happen?

dailyLeaderboardsConfig,
);
try {
allTimeLeaderboardCache.clear();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not the correct place, this is the dailyLeaderboard.

throw new MonkeyError(500, "Invalid page or pageSize");
}

if (page === 0 && pageSize === 50 && uid === undefined) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets move the cache to the controller/leaderboard and remove unnecessary try/catch and logs

if (e.error === 175) {
if ((e as unknown as { error: number }).error === 175) {
//QueryPlanKilled, collection was removed during the query
return false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert

Copy link
Author

@LuckySilver0021 LuckySilver0021 Jan 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reverting this will scream linting errors and it wont commit the code changes, I tried fixing it but I couldnt make it.
help me with this please, but apart from that I have successfully made the requested changes.

as of now I will just update the pr except this part only becuase I couldnt commit and push the changes,

further let me know how to deal with that.
Thanks

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// oxlint-disable-next-line no-unsafe-member-access is telling the linter to ignore this. npm run lint-be doesn't fail for me.

if (e.error === 175) {
if ((e as unknown as { error: number }).error === 175) {
//QueryPlanKilled, collection was removed during the query
return false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert

@github-actions github-actions bot added the waiting for review Pull requests that require a review before continuing label Jan 27, 2026
@fehmer fehmer marked this pull request as draft January 28, 2026 22:29
@github-actions github-actions bot removed the waiting for review Pull requests that require a review before continuing label Jan 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend Server stuff

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants