Replace Frosty with Flatty for de/serialization

This commit is contained in:
Zed 2021-12-26 23:01:19 +01:00
parent ca867be915
commit a7e0f817c9
2 changed files with 19 additions and 11 deletions

View File

@ -21,8 +21,8 @@ requires "packedjson#d11d167"
requires "supersnappy#2.1.1" requires "supersnappy#2.1.1"
requires "redpool#f880f49" requires "redpool#f880f49"
requires "https://github.com/zedeus/redis#d0a0e6f" requires "https://github.com/zedeus/redis#d0a0e6f"
requires "https://github.com/disruptek/frosty#0.3.1"
requires "zippy#0.7.3" requires "zippy#0.7.3"
requires "flatty#0.2.3"
# Tasks # Tasks

View File

@ -1,5 +1,5 @@
import asyncdispatch, times, strutils, tables, hashes import asyncdispatch, times, strutils, tables, hashes
import redis, redpool, frosty, supersnappy import redis, redpool, flatty, supersnappy
import types, api import types, api
@ -11,6 +11,13 @@ var
rssCacheTime: int rssCacheTime: int
listCacheTime*: int listCacheTime*: int
# flatty can't serialize DateTime, so we need to define this
proc toFlatty*(s: var string, x: DateTime) =
s.toFlatty(x.toTime().toUnix())
proc fromFlatty*(s: string, i: var int, x: var DateTime) =
x = fromUnix(s.fromFlatty(int64)).utc()
proc setCacheTimes*(cfg: Config) = proc setCacheTimes*(cfg: Config) =
rssCacheTime = cfg.rssCacheTime * 60 rssCacheTime = cfg.rssCacheTime * 60
listCacheTime = cfg.listCacheTime * 60 listCacheTime = cfg.listCacheTime * 60
@ -28,11 +35,12 @@ proc migrate*(key, match: string) {.async.} =
proc initRedisPool*(cfg: Config) {.async.} = proc initRedisPool*(cfg: Config) {.async.} =
try: try:
pool = await newRedisPool(cfg.redisConns, maxConns=cfg.redisMaxConns, pool = await newRedisPool(cfg.redisConns, cfg.redisMaxConns,
host=cfg.redisHost, port=cfg.redisPort, password=cfg.redisPassword) host=cfg.redisHost, port=cfg.redisPort,
password=cfg.redisPassword)
await migrate("flatty", "*:*")
await migrate("snappyRss", "rss:*") await migrate("snappyRss", "rss:*")
await migrate("oldFrosty", "*")
await migrate("userBuckets", "p:*") await migrate("userBuckets", "p:*")
await migrate("profileDates", "p:*") await migrate("profileDates", "p:*")
@ -58,17 +66,17 @@ proc setex(key: string; time: int; data: string) {.async.} =
discard await r.setex(key, time, data) discard await r.setex(key, time, data)
proc cache*(data: List) {.async.} = proc cache*(data: List) {.async.} =
await setex(data.listKey, listCacheTime, compress(freeze(data))) await setex(data.listKey, listCacheTime, compress(toFlatty(data)))
proc cache*(data: PhotoRail; name: string) {.async.} = proc cache*(data: PhotoRail; name: string) {.async.} =
await setex("pr:" & name, baseCacheTime, compress(freeze(data))) await setex("pr:" & 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 or data.id.len == 0: return
let name = toLower(data.username) let name = toLower(data.username)
pool.withAcquire(r): pool.withAcquire(r):
r.startPipelining() r.startPipelining()
discard await r.setex(name.profileKey, baseCacheTime, compress(freeze(data))) discard await r.setex(name.profileKey, baseCacheTime, compress(toFlatty(data)))
discard await r.hset(name.pidKey, name, data.id) discard await r.hset(name.pidKey, name, data.id)
discard await r.flushPipeline() discard await r.flushPipeline()
@ -97,7 +105,7 @@ proc getProfileId*(username: string): Future[string] {.async.} =
proc getCachedProfile*(username: string; fetch=true): Future[Profile] {.async.} = proc getCachedProfile*(username: string; fetch=true): 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) result = fromFlatty(uncompress(prof), Profile)
elif fetch: elif fetch:
result = await getProfile(username) result = await getProfile(username)
@ -105,7 +113,7 @@ proc getCachedPhotoRail*(name: string): Future[PhotoRail] {.async.} =
if name.len == 0: return if name.len == 0: return
let rail = await get("pr:" & toLower(name)) let rail = await get("pr:" & toLower(name))
if rail != redisNil: if rail != redisNil:
uncompress(rail).thaw(result) result = fromFlatty(uncompress(rail), PhotoRail)
else: else:
result = await getPhotoRail(name) result = await getPhotoRail(name)
await cache(result, name) await cache(result, name)
@ -115,7 +123,7 @@ proc getCachedList*(username=""; name=""; id=""): Future[List] {.async.} =
else: await get(toLower("l:" & username & '/' & name)) else: await get(toLower("l:" & username & '/' & name))
if list != redisNil: if list != redisNil:
uncompress(list).thaw(result) result = fromFlatty(uncompress(list), List)
else: else:
if id.len > 0: if id.len > 0:
result = await getGraphListById(id) result = await getGraphListById(id)