Skip to content

Commit bb21193

Browse files
checkpoint
1 parent df7684c commit bb21193

5 files changed

Lines changed: 21 additions & 29 deletions

File tree

backend/src/api/controllers/leaderboard.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
} from "@monkeytype/util/date-and-time";
2828
import { MonkeyRequest } from "../types";
2929
import { omit } from "../../utils/misc";
30+
import { allTimeLeaderboardCache } from "../../utils/all-time-leaderboard-cache";
3031

3132
export async function getLeaderboard(
3233
req: MonkeyRequest<GetLeaderboardQuery>,
@@ -68,6 +69,11 @@ export async function getLeaderboard(
6869
language,
6970
friendsOnlyUid,
7071
);
72+
73+
if (page === 0 && pageSize === 50 && uid === undefined) {
74+
allTimeLeaderboardCache.set({ mode, language, mode2 }, leaderboard, count);
75+
}
76+
7177
const normalizedLeaderboard = leaderboard.map((it) => omit(it, ["_id"]));
7278

7379
return new MonkeyResponse("Leaderboard retrieved", {

backend/src/api/controllers/result.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ import { MonkeyRequest } from "../types";
6464
import { getFunbox, checkCompatibility } from "@monkeytype/funbox";
6565
import { tryCatch } from "@monkeytype/util/trycatch";
6666
import { getCachedConfiguration } from "../../init/configuration";
67-
import { allTimeLeaderboardCache } from "../../utils/all-time-leaderboard-cache";
6867

6968
try {
7069
if (!anticheatImplemented()) throw new Error("undefined");
@@ -535,12 +534,6 @@ export async function addResult(
535534
},
536535
dailyLeaderboardsConfig,
537536
);
538-
try {
539-
allTimeLeaderboardCache.clear();
540-
console.log("All-time leaderboard cache cleared");
541-
} catch (error) {
542-
console.warn("Cache clear failed (non-critical):", error);
543-
}
544537

545538
if (
546539
dailyLeaderboardRank >= 1 &&

backend/src/credentials/.gitkeep

Whitespace-only changes.

backend/src/dal/leaderboards.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export async function get(
4949
}
5050

5151
if (page === 0 && pageSize === 50 && uid === undefined) {
52-
const cached = allTimeLeaderboardCache.get({ mode, language });
52+
const cached = allTimeLeaderboardCache.get({ mode, language, mode2 });
5353
if (cached) {
5454
console.log("✅ Cache HIT - leaderboards");
5555
return cached.data as DBLeaderboardEntry[];
@@ -94,22 +94,19 @@ export async function get(
9494
}
9595

9696
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-
}
97+
allTimeLeaderboardCache.set(
98+
{ mode, language, mode2 },
99+
leaderboard,
100+
await getCount(mode, mode2, language),
101+
);
102+
console.log(" Cache SET - leaderboards");
107103
}
108104

109105
return leaderboard;
110106
} catch (e) {
111107
if ((e as unknown as { error: number }).error === 175) {
112108
//QueryPlanKilled, collection was removed during the query
109+
return false;
113110
}
114111
throw e;
115112
}
@@ -185,6 +182,7 @@ export async function getRank(
185182
} catch (e) {
186183
if ((e as unknown as { error: number }).error === 175) {
187184
//QueryPlanKilled, collection was removed during the query
185+
return false;
188186
}
189187
throw e;
190188
}
@@ -303,6 +301,7 @@ export async function update(
303301
const end2 = performance.now();
304302

305303
cachedCounts.delete(`${language}_${mode}_${mode2}`);
304+
allTimeLeaderboardCache.clear();
306305

307306
//update speedStats
308307
const boundaries = [...Array(32).keys()].map((it) => it * 10);

backend/src/utils/all-time-leaderboard-cache.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,29 @@
11
type AllTimeCacheKey = {
22
mode: string;
33
language: string;
4+
mode2: string;
45
};
56

67
type CacheEntry = {
78
data: unknown[];
89
count: number;
9-
timestamp: number;
1010
};
1111

1212
class AllTimeLeaderboardCache {
1313
private cache = new Map<string, CacheEntry>();
14-
private readonly TTL = 900_000; // == 15 minutes of TTL
1514

16-
private getKey({ mode, language }: AllTimeCacheKey): string {
17-
return `alltime-lb:${mode}:${language}`;
15+
private getKey({ mode, language, mode2 }: AllTimeCacheKey): string {
16+
return `alltime-lb:${mode}:${language}:${mode2}`;
1817
}
1918

2019
get(key: AllTimeCacheKey): CacheEntry | null {
2120
const cacheKey = this.getKey(key);
2221
const entry = this.cache.get(cacheKey);
23-
24-
if (!entry || Date.now() - entry.timestamp > this.TTL) {
25-
if (entry) this.cache.delete(cacheKey);
26-
return null;
27-
}
28-
return entry;
22+
return entry ?? null;
2923
}
3024

3125
set(key: AllTimeCacheKey, data: unknown[], count: number): void {
32-
this.cache.set(this.getKey(key), { data, count, timestamp: Date.now() });
26+
this.cache.set(this.getKey(key), { data, count });
3327
}
3428

3529
clear(): void {

0 commit comments

Comments
 (0)