From 2e57ceba3ac86881ea2e3706ccb342ae5daa8950 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Tue, 28 May 2024 17:07:59 +0800 Subject: [PATCH 1/2] fix removing new items from redis --- packages/api/src/jobs/update_home.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/api/src/jobs/update_home.ts b/packages/api/src/jobs/update_home.ts index dd09639dc..d60d4a3e4 100644 --- a/packages/api/src/jobs/update_home.ts +++ b/packages/api/src/jobs/update_home.ts @@ -209,7 +209,7 @@ const rankCandidates = async ( return candidates } -const redisKey = (userId: string) => `just-read-feed:${userId}` +const redisKey = (userId: string) => `home:${userId}` const MAX_FEED_ITEMS = 500 export const getHomeSections = async ( @@ -251,7 +251,7 @@ export const getHomeSections = async ( const appendSectionsToHome = async ( userId: string, sections: Array
, - cursor = Date.now() + cursor?: number ) => { const redisClient = redisDataSource.redisClient if (!redisClient) { @@ -263,11 +263,14 @@ const appendSectionsToHome = async ( // store candidates in redis sorted set const pipeline = redisClient.pipeline() - const offset = sections.length + 86_400_000 - cursor = cursor - offset + // sections expire in 24 hours + const ttl = 86_400_000 + + const batchSize = sections.length + const savedAt = cursor ? cursor - batchSize - ttl : Date.now() const scoreMembers = sections.flatMap((section, index) => [ - cursor + index + 86_400_000, // sections expire in 24 hours + savedAt + index + ttl, // score for the section is the savedAt + index + ttl JSON.stringify(section), ]) From 4d5a26de2cfaf58eca5093ec88e8f61471c64f0a Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Tue, 28 May 2024 17:08:13 +0800 Subject: [PATCH 2/2] add start and end cursor to the pageinfo --- packages/api/src/resolvers/home/index.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/api/src/resolvers/home/index.ts b/packages/api/src/resolvers/home/index.ts index 64a14503d..acb7f89e5 100644 --- a/packages/api/src/resolvers/home/index.ts +++ b/packages/api/src/resolvers/home/index.ts @@ -56,6 +56,8 @@ export const homeResolver = authorized< } } + const endCursor = sections[sections.length - 1].score.toString() + const edges = sections.map((section) => ({ cursor: section.score.toString(), node: section.member, @@ -64,8 +66,10 @@ export const homeResolver = authorized< return { edges, pageInfo: { - hasPreviousPage: true, // there is always a previous page for new items - hasNextPage: true, // there is always a next page for old items + startCursor: after, + endCursor, + hasPreviousPage: true, // there is always a previous page for newer items + hasNextPage: true, // there is always a next page for older items }, } })