Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions database/account-info/leaderboard.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { perTossupData, perBonusData } from '../../database/qbreader/collections.js';
import mergeTwoSortedArrays from '../../server/merge-two-sorted-arrays.js';

const CACHE_TTL = 5 * 60 * 1000; // 5 minutes
let cachedOverall = null;
let cachedAt = 0;

export default async function leaderboard (limit) {
if (cachedOverall && Date.now() - cachedAt < CACHE_TTL) {
return cachedOverall.slice(0, limit);
}

const tossupLeaderboard = await helper('tossup');
const bonusLeaderboard = await helper('bonus');
const overall = mergeTwoSortedArrays(
Expand All @@ -12,6 +20,8 @@ export default async function leaderboard (limit) {
);
// sort from most to least
overall.sort((a, b) => b.total - a.total);
cachedOverall = overall;
cachedAt = Date.now();
return overall.slice(0, limit);
}

Expand Down
Loading