2021-12-27 01:37:38 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
2021-01-03 02:51:15 +00:00
|
|
|
import asyncdispatch, times, strutils, tables, hashes
|
2021-12-26 22:01:19 +00:00
|
|
|
import redis, redpool, flatty, supersnappy
|
2020-06-01 00:16:24 +00:00
|
|
|
|
|
|
|
import types, api
|
|
|
|
|
|
|
|
const redisNil = "\0\0"
|
|
|
|
|
|
|
|
var
|
2020-06-22 01:50:16 +00:00
|
|
|
pool {.threadvar.}: RedisPool
|
2020-06-01 00:16:24 +00:00
|
|
|
baseCacheTime = 60 * 60
|
|
|
|
rssCacheTime: int
|
|
|
|
listCacheTime*: int
|
|
|
|
|
2021-12-26 22:01:19 +00:00
|
|
|
# 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()
|
|
|
|
|
2020-06-01 00:16:24 +00:00
|
|
|
proc setCacheTimes*(cfg: Config) =
|
|
|
|
rssCacheTime = cfg.rssCacheTime * 60
|
|
|
|
listCacheTime = cfg.listCacheTime * 60
|
|
|
|
|
2020-06-07 06:50:40 +00:00
|
|
|
proc migrate*(key, match: string) {.async.} =
|
|
|
|
pool.withAcquire(r):
|
|
|
|
let hasKey = await r.get(key)
|
|
|
|
if hasKey == redisNil:
|
|
|
|
let list = await r.scan(newCursor(0), match, 100000)
|
|
|
|
r.startPipelining()
|
|
|
|
for item in list:
|
2021-12-26 21:59:27 +00:00
|
|
|
discard await r.del(item)
|
2020-06-07 06:50:40 +00:00
|
|
|
await r.setk(key, "true")
|
|
|
|
discard await r.flushPipeline()
|
|
|
|
|
2020-06-06 07:27:25 +00:00
|
|
|
proc initRedisPool*(cfg: Config) {.async.} =
|
2020-06-01 11:36:20 +00:00
|
|
|
try:
|
2021-12-26 22:01:19 +00:00
|
|
|
pool = await newRedisPool(cfg.redisConns, cfg.redisMaxConns,
|
|
|
|
host=cfg.redisHost, port=cfg.redisPort,
|
|
|
|
password=cfg.redisPassword)
|
2020-06-06 07:27:25 +00:00
|
|
|
|
2021-12-26 22:01:19 +00:00
|
|
|
await migrate("flatty", "*:*")
|
2020-06-07 06:50:40 +00:00
|
|
|
await migrate("snappyRss", "rss:*")
|
2021-12-26 21:59:27 +00:00
|
|
|
await migrate("userBuckets", "p:*")
|
|
|
|
await migrate("profileDates", "p:*")
|
2021-01-03 02:51:15 +00:00
|
|
|
|
|
|
|
pool.withAcquire(r):
|
2021-12-26 21:59:27 +00:00
|
|
|
# optimize memory usage for profile ID buckets
|
2021-01-03 02:51:15 +00:00
|
|
|
await r.configSet("hash-max-ziplist-entries", "1000")
|
2020-06-07 06:50:40 +00:00
|
|
|
|
2020-06-01 11:36:20 +00:00
|
|
|
except OSError:
|
2020-06-07 07:18:40 +00:00
|
|
|
stdout.write "Failed to connect to Redis.\n"
|
|
|
|
stdout.flushFile
|
2020-06-01 11:48:28 +00:00
|
|
|
quit(1)
|
2020-06-01 00:16:24 +00:00
|
|
|
|
2021-01-03 02:51:15 +00:00
|
|
|
template pidKey(name: string): string = "pid:" & $(hash(name) div 1_000_000)
|
|
|
|
template profileKey(name: string): string = "p:" & name
|
|
|
|
template listKey(l: List): string = toLower("l:" & l.username & '/' & l.name)
|
2020-06-01 00:16:24 +00:00
|
|
|
|
|
|
|
proc get(query: string): Future[string] {.async.} =
|
|
|
|
pool.withAcquire(r):
|
|
|
|
result = await r.get(query)
|
|
|
|
|
2020-06-02 20:36:02 +00:00
|
|
|
proc setex(key: string; time: int; data: string) {.async.} =
|
2020-06-01 00:16:24 +00:00
|
|
|
pool.withAcquire(r):
|
2020-06-02 20:36:02 +00:00
|
|
|
discard await r.setex(key, time, data)
|
2020-06-01 00:16:24 +00:00
|
|
|
|
2020-06-02 20:36:02 +00:00
|
|
|
proc cache*(data: List) {.async.} =
|
2021-12-26 22:01:19 +00:00
|
|
|
await setex(data.listKey, listCacheTime, compress(toFlatty(data)))
|
2020-06-01 00:16:24 +00:00
|
|
|
|
2020-06-16 22:20:34 +00:00
|
|
|
proc cache*(data: PhotoRail; name: string) {.async.} =
|
2021-12-26 22:01:19 +00:00
|
|
|
await setex("pr:" & name, baseCacheTime, compress(toFlatty(data)))
|
2020-06-01 00:16:24 +00:00
|
|
|
|
2020-06-02 20:36:02 +00:00
|
|
|
proc cache*(data: Profile) {.async.} =
|
2021-01-07 21:04:01 +00:00
|
|
|
if data.username.len == 0 or data.id.len == 0: return
|
2021-01-03 02:51:15 +00:00
|
|
|
let name = toLower(data.username)
|
2020-06-01 00:16:24 +00:00
|
|
|
pool.withAcquire(r):
|
|
|
|
r.startPipelining()
|
2021-12-26 22:01:19 +00:00
|
|
|
discard await r.setex(name.profileKey, baseCacheTime, compress(toFlatty(data)))
|
2021-10-04 10:03:40 +00:00
|
|
|
discard await r.setex("i:" & data.id , baseCacheTime, data.username)
|
2021-01-03 02:51:15 +00:00
|
|
|
discard await r.hset(name.pidKey, name, data.id)
|
2020-06-01 00:16:24 +00:00
|
|
|
discard await r.flushPipeline()
|
|
|
|
|
2020-06-02 22:03:41 +00:00
|
|
|
proc cacheProfileId*(username, id: string) {.async.} =
|
|
|
|
if username.len == 0 or id.len == 0: return
|
2021-01-03 02:51:15 +00:00
|
|
|
let name = toLower(username)
|
2020-06-02 22:03:41 +00:00
|
|
|
pool.withAcquire(r):
|
2021-01-03 02:51:15 +00:00
|
|
|
discard await r.hset(name.pidKey, name, id)
|
2020-06-02 22:03:41 +00:00
|
|
|
|
2020-06-06 07:27:25 +00:00
|
|
|
proc cacheRss*(query: string; rss: Rss) {.async.} =
|
2020-06-01 00:16:24 +00:00
|
|
|
let key = "rss:" & query
|
|
|
|
pool.withAcquire(r):
|
|
|
|
r.startPipelining()
|
2020-06-06 07:27:25 +00:00
|
|
|
discard await r.hset(key, "rss", rss.feed)
|
|
|
|
discard await r.hset(key, "min", rss.cursor)
|
2020-06-01 00:16:24 +00:00
|
|
|
discard await r.expire(key, rssCacheTime)
|
|
|
|
discard await r.flushPipeline()
|
|
|
|
|
|
|
|
proc getProfileId*(username: string): Future[string] {.async.} =
|
2021-01-03 02:51:15 +00:00
|
|
|
let name = toLower(username)
|
2020-06-01 00:16:24 +00:00
|
|
|
pool.withAcquire(r):
|
2021-01-03 02:51:15 +00:00
|
|
|
result = await r.hget(name.pidKey, name)
|
2020-06-01 00:16:24 +00:00
|
|
|
if result == redisNil:
|
|
|
|
result.setLen(0)
|
|
|
|
|
2021-01-07 21:04:01 +00:00
|
|
|
proc getCachedProfile*(username: string; fetch=true): Future[Profile] {.async.} =
|
2020-06-01 00:16:24 +00:00
|
|
|
let prof = await get("p:" & toLower(username))
|
|
|
|
if prof != redisNil:
|
2021-12-26 22:01:19 +00:00
|
|
|
result = fromFlatty(uncompress(prof), Profile)
|
2020-06-02 22:03:41 +00:00
|
|
|
elif fetch:
|
2020-06-01 07:46:17 +00:00
|
|
|
result = await getProfile(username)
|
2020-06-01 00:16:24 +00:00
|
|
|
|
2021-12-30 00:48:48 +00:00
|
|
|
proc getCachedProfileUsername*(userId: string): Future[string] {.async.} =
|
2021-10-04 10:03:40 +00:00
|
|
|
let username = await get("i:" & userId)
|
|
|
|
if username != redisNil:
|
2021-12-30 00:48:48 +00:00
|
|
|
result = username
|
2021-10-04 10:03:40 +00:00
|
|
|
else:
|
|
|
|
let profile = await getProfileById(userId)
|
|
|
|
result = profile.username
|
|
|
|
await cache(profile)
|
|
|
|
|
2020-06-16 22:20:34 +00:00
|
|
|
proc getCachedPhotoRail*(name: string): Future[PhotoRail] {.async.} =
|
|
|
|
if name.len == 0: return
|
|
|
|
let rail = await get("pr:" & toLower(name))
|
2020-06-01 00:16:24 +00:00
|
|
|
if rail != redisNil:
|
2021-12-26 22:01:19 +00:00
|
|
|
result = fromFlatty(uncompress(rail), PhotoRail)
|
2020-06-01 00:16:24 +00:00
|
|
|
else:
|
2020-06-16 22:20:34 +00:00
|
|
|
result = await getPhotoRail(name)
|
|
|
|
await cache(result, name)
|
2020-06-01 00:16:24 +00:00
|
|
|
|
|
|
|
proc getCachedList*(username=""; name=""; id=""): Future[List] {.async.} =
|
|
|
|
let list = if id.len > 0: redisNil
|
|
|
|
else: await get(toLower("l:" & username & '/' & name))
|
|
|
|
|
|
|
|
if list != redisNil:
|
2021-12-26 22:01:19 +00:00
|
|
|
result = fromFlatty(uncompress(list), List)
|
2020-06-01 00:16:24 +00:00
|
|
|
else:
|
|
|
|
if id.len > 0:
|
|
|
|
result = await getGraphListById(id)
|
|
|
|
else:
|
|
|
|
result = await getGraphList(username, name)
|
2020-06-02 20:36:02 +00:00
|
|
|
await cache(result)
|
2020-06-01 00:16:24 +00:00
|
|
|
|
2020-06-06 07:27:25 +00:00
|
|
|
proc getCachedRss*(key: string): Future[Rss] {.async.} =
|
|
|
|
let k = "rss:" & key
|
2020-06-01 00:16:24 +00:00
|
|
|
pool.withAcquire(r):
|
2020-06-06 07:27:25 +00:00
|
|
|
result.cursor = await r.hget(k, "min")
|
|
|
|
if result.cursor.len > 2:
|
|
|
|
result.feed = await r.hget(k, "rss")
|
|
|
|
else:
|
|
|
|
result.cursor.setLen 0
|