From 61d27efd69985debe2d1dfd0ae438688e15f6150 Mon Sep 17 00:00:00 2001 From: Zed Date: Tue, 9 Jun 2020 18:19:20 +0200 Subject: [PATCH] Optimize profile caching strategy --- src/redis_cache.nim | 4 +++- src/routes/timeline.nim | 24 +++++++++++++++++------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/redis_cache.nim b/src/redis_cache.nim index fafd2eb..660b0e3 100644 --- a/src/redis_cache.nim +++ b/src/redis_cache.nim @@ -85,12 +85,14 @@ proc getProfileId*(username: string): Future[string] {.async.} = if result == redisNil: result.setLen(0) -proc getCachedProfile*(username: string; fetch=true): Future[Profile] {.async.} = +proc getCachedProfile*(username: string; fetch=true; + cache=false): Future[Profile] {.async.} = let prof = await get("p:" & toLower(username)) if prof != redisNil: uncompress(prof).thaw(result) elif fetch: result = await getProfile(username) + if cache: await cache(result) proc getCachedPhotoRail*(id: string): Future[PhotoRail] {.async.} = if id.len == 0: return diff --git a/src/routes/timeline.nim b/src/routes/timeline.nim index 2d415f3..ff08ee6 100644 --- a/src/routes/timeline.nim +++ b/src/routes/timeline.nim @@ -23,17 +23,23 @@ proc fetchSingleTimeline*(after: string; query: Query; skipRail=false): let name = query.fromUser[0] var - profile = await getCachedProfile(name, fetch=false) + profile: Profile profileId = await getProfileId(name) + fetched = false - if profile.username.len == 0 and profileId.len == 0: - profile = await getProfile(name) - profileId = profile.id - await cacheProfileId(profile.username, profile.id) + if profileId.len == 0: + profile = await getCachedProfile(name) + profileId = if profile.suspended: "s" + else: profile.id + await cacheProfileId(profile.username, profileId) + fetched = true - if profile.suspended or profile.protected or profileId.len == 0: + if profileId.len == 0 or profile.protected: result[0] = profile return + elif profileId == "s": + result[0] = Profile(username: name, suspended: true) + return var rail: Future[PhotoRail] if skipRail or query.kind == media: @@ -51,14 +57,18 @@ proc fetchSingleTimeline*(after: string; query: Query; skipRail=false): timeline.query = query + var found = false for tweet in timeline.content.mitems: if tweet.profile.id == profileId or tweet.profile.username.cmpIgnoreCase(name) == 0: profile = tweet.profile + found = true break if profile.username.len == 0: - profile = await getCachedProfile(name) + profile = await getCachedProfile(name, cache=true) + + if fetched and not found: await cache(profile) return (profile, timeline, await rail)