feat: middle-most caching

This commit is contained in:
TrèsAbhi committed 2023-11-14 21:13:09 -06:00
1 parent 171da2a194
commit 2909e61721
1 file changed
+24 -5
+24 -5
View File
@@ -206,10 +206,22 @@ export default function Page() {
if (!ratingsInfo || ratingsInfo?.detail) return; if (!ratingsInfo || ratingsInfo?.detail) return;
if (season === 0) { if (season === 0) {
const middleIndex = Math.round(page * ROWS_PER_PAGE + ROWS_PER_PAGE / 2); const middleIndex = page * ROWS_PER_PAGE + ROWS_PER_PAGE / 2;
if (!(middleIndex in leaderboard[region][season])) return; const best = range(
const middleId = leaderboard[region][season][middleIndex]; page * ROWS_PER_PAGE,
cacheNeighbors(middleId, ROWS_PER_PAGE * 3); page * ROWS_PER_PAGE + ROWS_PER_PAGE + 1,
)
.filter((index) => index in leaderboard[region][season])
.map((index) => ({ index, distance: Math.abs(index - middleIndex) }))
.sort((a, b) => b.distance - a.distance)
.at(0);
if (best === undefined) return;
cacheNeighbors(
leaderboard[region][season][best.index],
ROWS_PER_PAGE * 3,
);
} else { } else {
const ids = range( const ids = range(
Math.max(0, (page - 1) * ROWS_PER_PAGE), Math.max(0, (page - 1) * ROWS_PER_PAGE),
@@ -258,6 +270,8 @@ export default function Page() {
id: number, id: number,
size: number, size: number,
): Promise<RatingsPlayer[]> { ): Promise<RatingsPlayer[]> {
console.log(id, 'working');
const radius = Math.round(size / 2); const radius = Math.round(size / 2);
const targetPosition = Object.entries(leaderboard[region][season]).find( const targetPosition = Object.entries(leaderboard[region][season]).find(
([, player]) => id === player, ([, player]) => id === player,
@@ -276,7 +290,10 @@ export default function Page() {
if (isMissing) break; if (isMissing) break;
} }
if (!isMissing) return []; if (!isMissing) {
console.log(id, 'skipping');
return [];
}
} }
const seedingPlayerPosition = parseInt( const seedingPlayerPosition = parseInt(
@@ -319,6 +336,8 @@ export default function Page() {
}), }),
); );
console.log(id, 'cached');
return neighbors; return neighbors;
} }
async function jumpToPosition(position: number) { async function jumpToPosition(position: number) {