From 23f87c115a2fc8c14b2dbb735426bb50d078daa8 Mon Sep 17 00:00:00 2001 From: Zed Date: Sun, 16 Jan 2022 19:22:27 +0100 Subject: [PATCH] Add template to make Redis usage cleaner --- src/redis_cache.nim | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/redis_cache.nim b/src/redis_cache.nim index 7a921c8..ea1df26 100644 --- a/src/redis_cache.nim +++ b/src/redis_cache.nim @@ -13,6 +13,9 @@ var rssCacheTime: int listCacheTime*: int +template dawait(future) = + discard await future + # flatty can't serialize DateTime, so we need to define this proc toFlatty*(s: var string, x: DateTime) = s.toFlatty(x.toTime().toUnix()) @@ -33,9 +36,9 @@ proc migrate*(key, match: string) {.async.} = let list = await r.scan(newCursor(0), match, 100000) r.startPipelining() for item in list: - discard await r.del(item) + dawait r.del(item) await r.setk(key, "true") - discard await r.flushPipeline() + dawait r.flushPipeline() proc initRedisPool*(cfg: Config) {.async.} = try: @@ -68,7 +71,7 @@ proc get(query: string): Future[string] {.async.} = proc setEx(key: string; time: int; data: string) {.async.} = pool.withAcquire(r): - discard await r.setEx(key, time, data) + dawait r.setEx(key, time, data) proc cache*(data: List) {.async.} = await setEx(data.listKey, listCacheTime, compress(toFlatty(data))) @@ -81,25 +84,25 @@ proc cache*(data: Profile) {.async.} = let name = toLower(data.username) pool.withAcquire(r): r.startPipelining() - discard await r.setEx(name.profileKey, baseCacheTime, compress(toFlatty(data))) - discard await r.setEx("i:" & data.id , baseCacheTime, data.username) - discard await r.hSet(name.pidKey, name, data.id) - discard await r.flushPipeline() + dawait r.setEx(name.profileKey, baseCacheTime, compress(toFlatty(data))) + dawait r.setEx("i:" & data.id , baseCacheTime, data.username) + dawait r.hSet(name.pidKey, name, data.id) + dawait r.flushPipeline() proc cacheProfileId*(username, id: string) {.async.} = if username.len == 0 or id.len == 0: return let name = toLower(username) pool.withAcquire(r): - discard await r.hSet(name.pidKey, name, id) + dawait r.hSet(name.pidKey, name, id) proc cacheRss*(query: string; rss: Rss) {.async.} = let key = "rss:" & query pool.withAcquire(r): r.startPipelining() - discard await r.hSet(key, "rss", rss.feed) - discard await r.hSet(key, "min", rss.cursor) - discard await r.expire(key, rssCacheTime) - discard await r.flushPipeline() + dawait r.hSet(key, "rss", rss.feed) + dawait r.hSet(key, "min", rss.cursor) + dawait r.expire(key, rssCacheTime) + dawait r.flushPipeline() proc getProfileId*(username: string): Future[string] {.async.} = let name = toLower(username)