Optimize profile caching strategy

This commit is contained in:
Zed 2020-06-09 18:19:20 +02:00
parent ba9ba3a55e
commit 61d27efd69
2 changed files with 20 additions and 8 deletions

View File

@ -85,12 +85,14 @@ proc getProfileId*(username: string): Future[string] {.async.} =
if result == redisNil: if result == redisNil:
result.setLen(0) 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)) let prof = await get("p:" & toLower(username))
if prof != redisNil: if prof != redisNil:
uncompress(prof).thaw(result) uncompress(prof).thaw(result)
elif fetch: elif fetch:
result = await getProfile(username) result = await getProfile(username)
if cache: await cache(result)
proc getCachedPhotoRail*(id: string): Future[PhotoRail] {.async.} = proc getCachedPhotoRail*(id: string): Future[PhotoRail] {.async.} =
if id.len == 0: return if id.len == 0: return

View File

@ -23,17 +23,23 @@ proc fetchSingleTimeline*(after: string; query: Query; skipRail=false):
let name = query.fromUser[0] let name = query.fromUser[0]
var var
profile = await getCachedProfile(name, fetch=false) profile: Profile
profileId = await getProfileId(name) profileId = await getProfileId(name)
fetched = false
if profile.username.len == 0 and profileId.len == 0: if profileId.len == 0:
profile = await getProfile(name) profile = await getCachedProfile(name)
profileId = profile.id profileId = if profile.suspended: "s"
await cacheProfileId(profile.username, profile.id) 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 result[0] = profile
return return
elif profileId == "s":
result[0] = Profile(username: name, suspended: true)
return
var rail: Future[PhotoRail] var rail: Future[PhotoRail]
if skipRail or query.kind == media: if skipRail or query.kind == media:
@ -51,14 +57,18 @@ proc fetchSingleTimeline*(after: string; query: Query; skipRail=false):
timeline.query = query timeline.query = query
var found = false
for tweet in timeline.content.mitems: for tweet in timeline.content.mitems:
if tweet.profile.id == profileId or if tweet.profile.id == profileId or
tweet.profile.username.cmpIgnoreCase(name) == 0: tweet.profile.username.cmpIgnoreCase(name) == 0:
profile = tweet.profile profile = tweet.profile
found = true
break break
if profile.username.len == 0: if profile.username.len == 0:
profile = await getCachedProfile(name) profile = await getCachedProfile(name, cache=true)
if fetched and not found:
await cache(profile) await cache(profile)
return (profile, timeline, await rail) return (profile, timeline, await rail)