From 384b0a20330dbf3558f472ae5801fa07fba720a5 Mon Sep 17 00:00:00 2001 From: Zed Date: Thu, 7 Jan 2021 22:04:01 +0100 Subject: [PATCH] Fix profile caching logic to ignore empty profiles --- src/redis_cache.nim | 6 ++---- src/routes/timeline.nim | 8 ++++++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/redis_cache.nim b/src/redis_cache.nim index 17d2c7a..10fe6d7 100644 --- a/src/redis_cache.nim +++ b/src/redis_cache.nim @@ -63,7 +63,7 @@ proc cache*(data: PhotoRail; name: string) {.async.} = await setex("pr:" & name, baseCacheTime, compress(freeze(data))) proc cache*(data: Profile) {.async.} = - if data.username.len == 0: return + if data.username.len == 0 or data.id.len == 0: return let name = toLower(data.username) pool.withAcquire(r): r.startPipelining() @@ -93,14 +93,12 @@ proc getProfileId*(username: string): Future[string] {.async.} = if result == redisNil: result.setLen(0) -proc getCachedProfile*(username: string; fetch=true; - cache=false): Future[Profile] {.async.} = +proc getCachedProfile*(username: string; fetch=true): 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*(name: string): Future[PhotoRail] {.async.} = if name.len == 0: return diff --git a/src/routes/timeline.nim b/src/routes/timeline.nim index 183a1e2..4109cef 100644 --- a/src/routes/timeline.nim +++ b/src/routes/timeline.nim @@ -31,7 +31,10 @@ proc fetchSingleTimeline*(after: string; query: Query; skipRail=false): profile = await getCachedProfile(name) profileId = if profile.suspended: "s" else: profile.id - await cacheProfileId(profile.username, profileId) + + if profileId.len > 0: + await cacheProfileId(profile.username, profileId) + fetched = true if profileId.len == 0 or profile.protected: @@ -66,7 +69,8 @@ proc fetchSingleTimeline*(after: string; query: Query; skipRail=false): break if profile.username.len == 0: - profile = await getCachedProfile(name, cache=true) + profile = await getCachedProfile(name) + fetched = true if fetched and not found: await cache(profile)