@@ -15,6 +15,8 @@ import { DBUser, getUsersCollection } from "./user";
1515import MonkeyError from "../utils/error" ;
1616import { aggregateWithAcceptedConnections } from "./connections" ;
1717
18+ import { allTimeLeaderboardCache } from "../utils/all-time-leaderboard-cache" ;
19+
1820export type DBLeaderboardEntry = LeaderboardEntry & {
1921 _id : ObjectId ;
2022} ;
@@ -46,6 +48,14 @@ export async function get(
4648 throw new MonkeyError ( 500 , "Invalid page or pageSize" ) ;
4749 }
4850
51+ if ( page === 0 && pageSize === 50 && uid === undefined ) {
52+ const cached = allTimeLeaderboardCache . get ( { mode, language } ) ;
53+ if ( cached ) {
54+ console . log ( "✅ Cache HIT - leaderboards" ) ;
55+ return cached . data as DBLeaderboardEntry [ ] ;
56+ }
57+ }
58+
4959 const skip = page * pageSize ;
5060 const limit = pageSize ;
5161
@@ -83,12 +93,23 @@ export async function get(
8393 leaderboard = leaderboard . map ( ( it ) => omit ( it , [ "isPremium" ] ) ) ;
8494 }
8595
96+ if ( page === 0 && pageSize === 50 && uid === undefined ) {
97+ try {
98+ allTimeLeaderboardCache . set (
99+ { mode, language } ,
100+ leaderboard ,
101+ await getCount ( mode , mode2 , language ) ,
102+ ) ;
103+ console . log ( " Cache SET - leaderboards" ) ;
104+ } catch ( error ) {
105+ console . warn ( "Cache set failed:" , error ) ;
106+ }
107+ }
108+
86109 return leaderboard ;
87110 } catch ( e ) {
88- // oxlint-disable-next-line no-unsafe-member-access
89- if ( e . error === 175 ) {
111+ if ( ( e as unknown as { error : number } ) . error === 175 ) {
90112 //QueryPlanKilled, collection was removed during the query
91- return false ;
92113 }
93114 throw e ;
94115 }
@@ -162,10 +183,8 @@ export async function getRank(
162183 return results [ 0 ] ?? null ;
163184 }
164185 } catch ( e ) {
165- // oxlint-disable-next-line no-unsafe-member-access
166- if ( e . error === 175 ) {
186+ if ( ( e as unknown as { error : number } ) . error === 175 ) {
167187 //QueryPlanKilled, collection was removed during the query
168- return false ;
169188 }
170189 throw e ;
171190 }
@@ -393,8 +412,8 @@ async function createIndex(
393412 Logger . warning ( `Index ${ key } not matching, dropping and recreating...` ) ;
394413
395414 const existingIndex = ( await getUsersCollection ( ) . listIndexes ( ) . toArray ( ) )
396- // oxlint-disable-next-line no-unsafe-member-access
397- . map ( ( it ) => it . name as string )
415+
416+ . map ( ( it : unknown ) => ( it as { name : string } ) . name )
398417 . find ( ( it ) => it . startsWith ( key ) ) ;
399418
400419 if ( existingIndex !== undefined && existingIndex !== null ) {
0 commit comments