Rearchitect profile, support pins, Profile -> User

This commit is contained in:
Zed
2022-01-23 07:04:50 +01:00
parent 79b98a8081
commit 51ae076ea0
23 changed files with 374 additions and 285 deletions

View File

@@ -1,5 +1,5 @@
# SPDX-License-Identifier: AGPL-3.0-only
import asyncdispatch, times, strutils, tables, hashes
import asyncdispatch, times, strformat, strutils, tables, hashes
import redis, redpool, flatty, supersnappy
import types, api
@@ -51,9 +51,10 @@ proc initRedisPool*(cfg: Config) {.async.} =
await migrate("userBuckets", "p:*")
await migrate("profileDates", "p:*")
await migrate("profileStats", "p:*")
await migrate("userType", "p:*")
pool.withAcquire(r):
# optimize memory usage for profile ID buckets
# optimize memory usage for user ID buckets
await r.configSet("hash-max-ziplist-entries", "1000")
except OSError:
@@ -61,9 +62,10 @@ proc initRedisPool*(cfg: Config) {.async.} =
stdout.flushFile
quit(1)
template pidKey(name: string): string = "pid:" & $(hash(name) div 1_000_000)
template profileKey(name: string): string = "p:" & name
template uidKey(name: string): string = "pid:" & $(hash(name) div 1_000_000)
template userKey(name: string): string = "p:" & name
template listKey(l: List): string = "l:" & l.id
template tweetKey(id: int64): string = "t:" & $id
proc get(query: string): Future[string] {.async.} =
pool.withAcquire(r):
@@ -73,25 +75,29 @@ proc setEx(key: string; time: int; data: string) {.async.} =
pool.withAcquire(r):
dawait r.setEx(key, time, data)
proc cacheUserId(username, id: string) {.async.} =
if username.len == 0 or id.len == 0: return
let name = toLower(username)
pool.withAcquire(r):
dawait r.hSet(name.uidKey, name, id)
proc cache*(data: List) {.async.} =
await setEx(data.listKey, listCacheTime, compress(toFlatty(data)))
proc cache*(data: PhotoRail; name: string) {.async.} =
await setEx("pr:" & toLower(name), baseCacheTime, compress(toFlatty(data)))
proc cache*(data: Profile) {.async.} =
proc cache*(data: User) {.async.} =
if data.username.len == 0: return
let name = toLower(data.username)
await cacheUserId(name, data.id)
pool.withAcquire(r):
dawait r.setEx(name.profileKey, baseCacheTime, compress(toFlatty(data)))
if data.id.len > 0:
dawait r.hSet(name.pidKey, name, data.id)
dawait r.setEx(name.userKey, baseCacheTime, compress(toFlatty(data)))
proc cacheProfileId(username, id: string) {.async.} =
if username.len == 0 or id.len == 0: return
let name = toLower(username)
proc cache*(data: Tweet) {.async.} =
if data.isNil or data.id == 0: return
pool.withAcquire(r):
dawait r.hSet(name.pidKey, name, id)
dawait r.setEx(data.id.tweetKey, baseCacheTime, compress(toFlatty(data)))
proc cacheRss*(query: string; rss: Rss) {.async.} =
let key = "rss:" & query
@@ -100,24 +106,34 @@ proc cacheRss*(query: string; rss: Rss) {.async.} =
dawait r.hSet(key, "min", rss.cursor)
dawait r.expire(key, rssCacheTime)
proc getProfileId*(username: string): Future[string] {.async.} =
template deserialize(data, T) =
try:
result = fromFlatty(uncompress(data), T)
except:
echo "Decompression failed($#): '$#'" % [astToStr(T), data]
proc getUserId*(username: string): Future[string] {.async.} =
let name = toLower(username)
pool.withAcquire(r):
result = await r.hGet(name.pidKey, name)
result = await r.hGet(name.uidKey, name)
if result == redisNil:
result.setLen(0)
let user = await getUser(username)
if user.suspended:
return "suspended"
else:
await cacheUserId(name, user.id)
return user.id
proc getCachedProfile*(username: string; fetch=true): Future[Profile] {.async.} =
proc getCachedUser*(username: string; fetch=true): Future[User] {.async.} =
let prof = await get("p:" & toLower(username))
if prof != redisNil:
result = fromFlatty(uncompress(prof), Profile)
prof.deserialize(User)
elif fetch:
result = await getProfile(username)
await cacheProfileId(result.username, result.id)
if result.suspended:
await cache(result)
let userId = await getUserId(username)
result = await getGraphUser(userId)
await cache(result)
proc getCachedProfileUsername*(userId: string): Future[string] {.async.} =
proc getCachedUsername*(userId: string): Future[string] {.async.} =
let
key = "i:" & userId
username = await get(key)
@@ -125,15 +141,26 @@ proc getCachedProfileUsername*(userId: string): Future[string] {.async.} =
if username != redisNil:
result = username
else:
let profile = await getProfileById(userId)
result = profile.username
let user = await getUserById(userId)
result = user.username
await setEx(key, baseCacheTime, result)
proc getCachedTweet*(id: int64): Future[Tweet] {.async.} =
if id == 0: return
let tweet = await get(id.tweetKey)
if tweet != redisNil:
tweet.deserialize(Tweet)
else:
let conv = await getTweet($id)
if not conv.isNil:
result = conv.tweet
await cache(result)
proc getCachedPhotoRail*(name: string): Future[PhotoRail] {.async.} =
if name.len == 0: return
let rail = await get("pr:" & toLower(name))
if rail != redisNil:
result = fromFlatty(uncompress(rail), PhotoRail)
rail.deserialize(PhotoRail)
else:
result = await getPhotoRail(name)
await cache(result, name)
@@ -143,7 +170,7 @@ proc getCachedList*(username=""; slug=""; id=""): Future[List] {.async.} =
else: await get("l:" & id)
if list != redisNil:
result = fromFlatty(uncompress(list), List)
list.deserialize(List)
else:
if id.len > 0:
result = await getGraphList(id)