feat: added 15 minutes of TTL for 1st page of all time leaderboard (@LuckySilver0021)#7447
feat: added 15 minutes of TTL for 1st page of all time leaderboard (@LuckySilver0021)#7447LuckySilver0021 wants to merge 1 commit intomonkeytypegame:masterfrom
Conversation
| @@ -0,0 +1,40 @@ | |||
| type AllTimeCacheKey = { | |||
| }; | ||
|
|
||
| type CacheEntry = { | ||
| data: unknown[]; |
| type CacheEntry = { | ||
| data: unknown[]; | ||
| count: number; | ||
| timestamp: number; |
There was a problem hiding this comment.
we don't need a timestamp and only clear using .clear()
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
| const cacheKey = this.getKey(key); | ||
| const entry = this.cache.get(cacheKey); | ||
|
|
||
| if (!entry || Date.now() - entry.timestamp > this.TTL) { |
| try { | ||
| allTimeLeaderboardCache.clear(); | ||
| console.log("All-time leaderboard cache cleared"); | ||
| } catch (error) { |
| dailyLeaderboardsConfig, | ||
| ); | ||
| try { | ||
| allTimeLeaderboardCache.clear(); |
There was a problem hiding this comment.
this is not the correct place, this is the dailyLeaderboard.
backend/src/dal/leaderboards.ts
Outdated
| throw new MonkeyError(500, "Invalid page or pageSize"); | ||
| } | ||
|
|
||
| if (page === 0 && pageSize === 50 && uid === undefined) { |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
// 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; |
f2d73ea to
a8fd1fb
Compare
|
Continuous integration check(s) failed. Please review the failing check's logs and make the necessary changes. |
Changes
backend/src/utils/all-time-leaderboard-cache.tswith 15min TTLbackend/src/dal/leaderboards.tsnow checks cache HIT/MISS firstbackend/src/types/result.tscallscache.clear()on result submitBenefits
Closes #
(@LuckySilver0021)