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

This commit is contained in:
jake committed 2025-03-28 11:26:51 -04:00
1 parent 64caf0f88b
commit 77a44fdea7
3 files changed
+15 -17

No files matched your search

+9 -11
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