Skip to content

Commit

Permalink
Speed-up main feed rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
TimDaub committed Jan 22, 2025
1 parent c3ef1cf commit 74a81d8
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions src/views/feed.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,6 @@ const thresholdKarma = 3;
export function identityClassifier(upvoter) {
let balance = 0;

// TODO: Change to neynar score
const cacheKey = `neynar-score-${upvoter.identity}`;
if (cache.has(cacheKey)) {
balance = cache.get(cacheKey);
Expand Down Expand Up @@ -558,18 +557,26 @@ export async function topstories(leaves) {
}

async function addMetadata(post) {
let result;
try {
result = await metadata(post.href);
} catch (err) {
return null;
const url = post.href;
const metadataCacheKey = `metadata-${url}`;
const cached = cache.get(metadataCacheKey);
if (cached) {
if (!cached.image) return null;
return {
...post,
metadata: cached,
};
}
if (result && !result.image) return;

return {
...post,
metadata: result,
};
const metadataTTLSeconds = 60 * 60; // 1 hour
metadata(post.href)
.then((result) => {
if (result && result.image) {
cache.set(metadataCacheKey, result, [metadataTTLSeconds]);
}
})
.catch((err) => log(`Metadata fetch failed: ${err.stack}`));
return null;
}

export async function index(
Expand Down Expand Up @@ -680,8 +687,15 @@ export async function index(

let resolvedContestStories;
if (showContest) {
const contestStories = await getContestStories();
resolvedContestStories = await resolveIds(contestStories);
const contestCacheKey = "contest-stories-cache";
resolvedContestStories = cache.get(contestCacheKey);
if (!resolvedContestStories) {
const contestTTL = 60 * 5;
getContestStories()
.then((stories) => resolveIds(stories))
.then((resolved) => cache.set(contestCacheKey, resolved, [contestTTL]))
.catch((err) => log(`Error loading contest stories: ${err.stack}`));
}
}
return {
contestStories: resolvedContestStories,
Expand Down

0 comments on commit 74a81d8

Please sign in to comment.