diff --git a/packages/api/src/jobs/update_home.ts b/packages/api/src/jobs/update_home.ts index cd3afdb40..68fe052c6 100644 --- a/packages/api/src/jobs/update_home.ts +++ b/packages/api/src/jobs/update_home.ts @@ -250,23 +250,25 @@ const appendSectionsToHome = async ( // store candidates in redis sorted set const pipeline = redisClient.pipeline() - // sections expire in 24 hours - const ttl = 86_400_000 + const now = Date.now() const batchSize = sections.length - const savedAt = cursor ? cursor - batchSize - ttl : Date.now() + const savedAt = cursor ? cursor - batchSize : now const scoreMembers = sections.flatMap((section, index) => [ - savedAt + index + ttl, // score for the section is the savedAt + index + ttl + savedAt + index, // score for the section is the savedAt + index otherwise it will be the same for all sections JSON.stringify(section), ]) // add section to the sorted set pipeline.zadd(key, ...scoreMembers) - // remove expired sections and keep only the top 500 + // remove expired sections and sections expire in 24 hours + const ttl = 86_400_000 + pipeline.zremrangebyscore(key, '-inf', Date.now() - ttl) + + // keep only the top MAX_FEED_ITEMS items pipeline.zremrangebyrank(key, 0, -(MAX_FEED_ITEMS + 1)) - pipeline.zremrangebyscore(key, '-inf', Date.now()) logger.info('Adding home sections to redis') await pipeline.exec() diff --git a/packages/api/src/resolvers/home/index.ts b/packages/api/src/resolvers/home/index.ts index acb7f89e5..0434250e7 100644 --- a/packages/api/src/resolvers/home/index.ts +++ b/packages/api/src/resolvers/home/index.ts @@ -29,7 +29,9 @@ export const homeResolver = authorized< QueryHomeArgs >(async (_, { first, after }, { uid, log }) => { const limit = first || 6 - const cursor = after ? parseInt(after) : undefined + // cursor is the timestamp of the last item in the feed + // if cursor is not provided, it defaults to the current time + const cursor = after ? parseInt(after) : Date.now() const sections = await getHomeSections(uid, limit, cursor) log.info('Just read feed sections fetched')