Move RSS compression to Redis module, fix crash

This commit is contained in:
Zed 2022-01-27 14:36:12 +01:00
parent 3a076a9b4e
commit 091bb6813d
2 changed files with 11 additions and 9 deletions

View file

@ -102,7 +102,7 @@ proc cache*(data: Tweet) {.async.} =
proc cacheRss*(query: string; rss: Rss) {.async.} = proc cacheRss*(query: string; rss: Rss) {.async.} =
let key = "rss:" & query let key = "rss:" & query
pool.withAcquire(r): pool.withAcquire(r):
dawait r.hSet(key, "rss", rss.feed) dawait r.hSet(key, "rss", compress(rss.feed))
dawait r.hSet(key, "min", rss.cursor) dawait r.hSet(key, "min", rss.cursor)
dawait r.expire(key, rssCacheTime) dawait r.expire(key, rssCacheTime)
@ -182,6 +182,9 @@ proc getCachedRss*(key: string): Future[Rss] {.async.} =
pool.withAcquire(r): pool.withAcquire(r):
result.cursor = await r.hGet(k, "min") result.cursor = await r.hGet(k, "min")
if result.cursor.len > 2: if result.cursor.len > 2:
result.feed = await r.hGet(k, "rss") let feed = await r.hGet(k, "rss")
if feed != redisNil:
try: result.feed = uncompress feed
except: echo "Decompressing RSS failed: ", feed
else: else:
result.cursor.setLen 0 result.cursor.setLen 0

View file

@ -1,14 +1,14 @@
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
import asyncdispatch, strutils, tables, times, hashes, uri import asyncdispatch, strutils, tables, times, hashes, uri
import jester, supersnappy import jester
import router_utils, timeline import router_utils, timeline
import ../query import ../query
include "../views/rss.nimf" include "../views/rss.nimf"
export times, hashes, supersnappy export times, hashes
proc timelineRss*(req: Request; cfg: Config; query: Query): Future[Rss] {.async.} = proc timelineRss*(req: Request; cfg: Config; query: Query): Future[Rss] {.async.} =
var profile: Profile var profile: Profile
@ -36,7 +36,7 @@ proc timelineRss*(req: Request; cfg: Config; query: Query): Future[Rss] {.async.
return Rss(feed: profile.user.username, cursor: "suspended") return Rss(feed: profile.user.username, cursor: "suspended")
if profile.user.fullname.len > 0: if profile.user.fullname.len > 0:
let rss = compress renderTimelineRss(profile, cfg, multi=(names.len > 1)) let rss = renderTimelineRss(profile, cfg, multi=(names.len > 1))
return Rss(feed: rss, cursor: profile.tweets.bottom) return Rss(feed: rss, cursor: profile.tweets.bottom)
template respRss*(rss, page) = template respRss*(rss, page) =
@ -52,7 +52,7 @@ template respRss*(rss, page) =
let headers = {"Content-Type": "application/rss+xml; charset=utf-8", let headers = {"Content-Type": "application/rss+xml; charset=utf-8",
"Min-Id": rss.cursor} "Min-Id": rss.cursor}
resp Http200, headers, uncompress rss.feed resp Http200, headers, rss.feed
proc createRssRouter*(cfg: Config) = proc createRssRouter*(cfg: Config) =
router rss: router rss:
@ -75,8 +75,7 @@ proc createRssRouter*(cfg: Config) =
let tweets = await getSearch[Tweet](query, cursor) let tweets = await getSearch[Tweet](query, cursor)
rss.cursor = tweets.bottom rss.cursor = tweets.bottom
rss.feed = compress renderSearchRss(tweets.content, query.text, rss.feed = renderSearchRss(tweets.content, query.text, genQueryUrl(query), cfg)
genQueryUrl(query), cfg)
await cacheRss(key, rss) await cacheRss(key, rss)
respRss(rss, "Search") respRss(rss, "Search")
@ -157,7 +156,7 @@ proc createRssRouter*(cfg: Config) =
list = await getCachedList(id=(@"id")) list = await getCachedList(id=(@"id"))
timeline = await getListTimeline(list.id, cursor) timeline = await getListTimeline(list.id, cursor)
rss.cursor = timeline.bottom rss.cursor = timeline.bottom
rss.feed = compress renderListRss(timeline.content, list, cfg) rss.feed = renderListRss(timeline.content, list, cfg)
await cacheRss(key, rss) await cacheRss(key, rss)
respRss(rss, "List") respRss(rss, "List")