From b4fe3d1f6a45069fec2611d1317922fe0a11f656 Mon Sep 17 00:00:00 2001 From: Zed Date: Wed, 3 Jun 2020 00:03:41 +0200 Subject: [PATCH] Prevent unnecessary profile cache --- src/redis_cache.nim | 9 ++++++--- src/routes/timeline.nim | 10 +++++----- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/redis_cache.nim b/src/redis_cache.nim index 39dfb11..bdd02a7 100644 --- a/src/redis_cache.nim +++ b/src/redis_cache.nim @@ -54,6 +54,11 @@ proc cache*(data: Profile) {.async.} = discard await r.hset("p:", toLower(data.username), data.id) discard await r.flushPipeline() +proc cacheProfileId*(username, id: string) {.async.} = + if username.len == 0 or id.len == 0: return + pool.withAcquire(r): + discard await r.hset("p:", toLower(username), id) + proc cacheRss*(query, rss, cursor: string) {.async.} = let key = "rss:" & query pool.withAcquire(r): @@ -73,10 +78,8 @@ proc getCachedProfile*(username: string; fetch=true): Future[Profile] {.async.} let prof = await get("p:" & toLower(username)) if prof != redisNil: result = prof.to(Profile) - else: + elif fetch: result = await getProfile(username) - if result.id.len > 0: - 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 5c4a65c..d7cd85f 100644 --- a/src/routes/timeline.nim +++ b/src/routes/timeline.nim @@ -27,15 +27,16 @@ proc fetchSingleTimeline*(after: string; query: Query; skipRail=false): profileId = await getProfileId(name) if profile.username.len == 0 and profileId.len == 0: - profile = await getCachedProfile(name) + profile = await getProfile(name) profileId = profile.id + await cacheProfileId(profile.username, profile.id) - if profile.suspended or profileId.len == 0: + if profile.suspended or profile.protected or profileId.len == 0: result[0] = profile return var rail: Future[PhotoRail] - if skipRail or query.kind == media or profile.suspended or profile.protected: + if skipRail or query.kind == media: rail = newFuture[PhotoRail]() rail.complete(@[]) else: @@ -50,7 +51,7 @@ proc fetchSingleTimeline*(after: string; query: Query; skipRail=false): timeline.query = query - for tweet in timeline.content: + for tweet in timeline.content.mitems: if tweet.profile.id == profileId or tweet.profile.username.cmpIgnoreCase(name) == 0: profile = tweet.profile @@ -58,7 +59,6 @@ proc fetchSingleTimeline*(after: string; query: Query; skipRail=false): if profile.username.len == 0: profile = await getCachedProfile(name) - else: await cache(profile) return (profile, timeline, await rail)