1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-07-03 15:16:40 -04:00

dramatically reduce the number of redis commands used for /api/hits

This commit is contained in:
2025-03-28 11:26:51 -04:00
parent 64caf0f88b
commit 77a44fdea7
3 changed files with 15 additions and 17 deletions

View File

@ -22,18 +22,16 @@ export const GET = async (): Promise<
count: 99,
});
// get the value (number of hits) for each key (the slug of the page) and pair them together
const pages = await Promise.all(
slugs[1].map(async (slug) => {
const hits = (await redis.get(slug)) as number;
return {
slug,
hits,
};
})
);
// get the value (number of hits) for each key (the slug of the page)
const values = await redis.mget<string[]>(...slugs[1]);
// sort by hits
// pair the slugs with their hit values
const pages = slugs[1].map((slug, index) => ({
slug,
hits: parseInt(values[index], 10),
}));
// sort descending by hits
pages.sort((a, b) => b.hits - a.hits);
// calculate total hits