Rework profile cache behavior, fix suspended cache

Fixes #480
This commit is contained in:
Zed 2022-01-16 20:32:45 +01:00
parent 23f87c115a
commit f3d6f53f6d
4 changed files with 20 additions and 21 deletions

View file

@ -34,7 +34,7 @@ proc getProfile*(username: string): Future[Profile] {.async.} =
let let
ps = genParams({"screen_name": username}) ps = genParams({"screen_name": username})
json = await fetchRaw(userShow ? ps, Api.userShow) json = await fetchRaw(userShow ? ps, Api.userShow)
result = parseUser(json) result = parseUser(json, username)
proc getProfileById*(userId: string): Future[Profile] {.async.} = proc getProfileById*(userId: string): Future[Profile] {.async.} =
let let

View file

@ -38,10 +38,10 @@ proc getBanner(user: User): string =
return '#' & user.profileLinkColor return '#' & user.profileLinkColor
return "#161616" return "#161616"
proc parseUser*(json: string): Profile = proc parseUser*(json: string; username=""): Profile =
handleErrors: handleErrors:
case error.code case error.code
of suspended: return Profile(suspended: true) of suspended: return Profile(username: username, suspended: true)
of userNotFound: return of userNotFound: return
else: echo "[error - parseUser]: ", error else: echo "[error - parseUser]: ", error

View file

@ -80,16 +80,16 @@ proc cache*(data: PhotoRail; name: string) {.async.} =
await setEx("pr:" & toLower(name), baseCacheTime, compress(toFlatty(data))) await setEx("pr:" & toLower(name), baseCacheTime, compress(toFlatty(data)))
proc cache*(data: Profile) {.async.} = proc cache*(data: Profile) {.async.} =
if data.username.len == 0 or data.id.len == 0: return if data.username.len == 0: return
let name = toLower(data.username) let name = toLower(data.username)
pool.withAcquire(r): pool.withAcquire(r):
r.startPipelining() r.startPipelining()
dawait r.setEx(name.profileKey, baseCacheTime, compress(toFlatty(data))) dawait r.setEx(name.profileKey, baseCacheTime, compress(toFlatty(data)))
dawait r.setEx("i:" & data.id , baseCacheTime, data.username) if data.id.len > 0:
dawait r.hSet(name.pidKey, name, data.id) dawait r.hSet(name.pidKey, name, data.id)
dawait r.flushPipeline() dawait r.flushPipeline()
proc cacheProfileId*(username, id: string) {.async.} = proc cacheProfileId(username, id: string) {.async.} =
if username.len == 0 or id.len == 0: return if username.len == 0 or id.len == 0: return
let name = toLower(username) let name = toLower(username)
pool.withAcquire(r): pool.withAcquire(r):
@ -117,15 +117,21 @@ proc getCachedProfile*(username: string; fetch=true): Future[Profile] {.async.}
result = fromFlatty(uncompress(prof), Profile) result = fromFlatty(uncompress(prof), Profile)
elif fetch: elif fetch:
result = await getProfile(username) result = await getProfile(username)
await cacheProfileId(result.username, result.id)
if result.suspended:
await cache(result)
proc getCachedProfileUsername*(userId: string): Future[string] {.async.} = proc getCachedProfileUsername*(userId: string): Future[string] {.async.} =
let username = await get("i:" & userId) let
key = "i:" & userId
username = await get(key)
if username != redisNil: if username != redisNil:
result = username result = username
else: else:
let profile = await getProfileById(userId) let profile = await getProfileById(userId)
result = profile.username result = profile.username
await cache(profile) await setEx(key, baseCacheTime, result)
proc getCachedPhotoRail*(name: string): Future[PhotoRail] {.async.} = proc getCachedPhotoRail*(name: string): Future[PhotoRail] {.async.} =
if name.len == 0: return if name.len == 0: return

View file

@ -30,20 +30,13 @@ proc fetchTimeline*(after: string; query: Query; skipRail=false):
if profileId.len == 0: if profileId.len == 0:
profile = await getCachedProfile(name) profile = await getCachedProfile(name)
profileId = if profile.suspended: "s" profileId = profile.id
else: profile.id
if profileId.len > 0:
await cacheProfileId(profile.username, profileId)
fetched = true fetched = true
if profileId.len == 0 or profile.protected: if profile.protected or profile.suspended:
result[0] = profile return (profile, Timeline(), @[])
return elif profileId.len == 0:
elif profileId == "s": return (Profile(username: name), Timeline(), @[])
result[0] = Profile(username: name, suspended: true)
return
var rail: Future[PhotoRail] var rail: Future[PhotoRail]
if skipRail or profile.protected or query.kind == media: if skipRail or profile.protected or query.kind == media: