From d179ac547c8c32d88bdb1f87f6106c469bf397dc Mon Sep 17 00:00:00 2001 From: Zed Date: Sat, 26 Oct 2019 15:33:38 +0200 Subject: [PATCH] Refactor video cache --- src/api/media.nim | 44 +++++++++++++++++++++----------------------- src/api/tweet.nim | 2 +- src/cache.nim | 21 ++++++++++++++++++++- src/prefs.nim | 3 ++- src/types.nim | 3 +-- 5 files changed, 45 insertions(+), 28 deletions(-) diff --git a/src/api/media.nim b/src/api/media.nim index 42b2462..1b95fda 100644 --- a/src/api/media.nim +++ b/src/api/media.nim @@ -1,6 +1,6 @@ -import httpclient, asyncdispatch, times, sequtils, strutils, json, uri +import httpclient, asyncdispatch, times, sequtils, strutils, json, uri, macros -import ".."/[types, parser, formatters] +import ".."/[types, parser, formatters, cache] import utils, consts var @@ -62,7 +62,13 @@ proc getGuestToken(agent: string; force=false): Future[string] {.async.} = result = json["guest_token"].to(string) guestToken = result -proc getVideoFetch(tweet: Tweet; agent, token: string) {.async.} = +proc getVideoVar(tweet: Tweet): var Option[Video] = + if tweet.card.isSome(): + return get(tweet.card).video + else: + return tweet.video + +proc getVideoFetch(tweet: Tweet; agent, token: string): Future[Option[Video]] {.async.} = if tweet.video.isNone(): return let @@ -75,31 +81,23 @@ proc getVideoFetch(tweet: Tweet; agent, token: string) {.async.} = if getTime() - tokenUpdated > initDuration(seconds=1): tokenUpdated = getTime() discard await getGuestToken(agent, force=true) - await getVideoFetch(tweet, agent, guestToken) + result = await getVideoFetch(tweet, agent, guestToken) return - if tweet.card.isNone: - tweet.video = some parseVideo(json, tweet.id) - else: - get(tweet.card).video = some parseVideo(json, tweet.id) - tweet.video = none Video + var video = parseVideo(json, tweet.id) + video.title = get(tweet.video).title + video.description = get(tweet.video).description + cache(video) + + result = some video tokenUses.inc -proc getVideoVar(tweet: Tweet): var Option[Video] = - if tweet.card.isSome(): - return get(tweet.card).video - else: - return tweet.video - proc getVideo*(tweet: Tweet; agent, token: string; force=false) {.async.} = - withCustomDb("cache.db", "", "", ""): - try: - getVideoVar(tweet) = some Video.getOne("videoId = ?", tweet.id) - except KeyError: - await getVideoFetch(tweet, agent, token) - var video = getVideoVar(tweet) - if video.isSome(): - get(video).insert() + var video = getCachedVideo(tweet.id) + if video.isNone: + video = await getVideoFetch(tweet, agent, token) + getVideoVar(tweet) = video + if tweet.card.isSome: tweet.video = none Video proc getPoll*(tweet: Tweet; agent: string) {.async.} = if tweet.poll.isNone(): return diff --git a/src/api/tweet.nim b/src/api/tweet.nim index 4a58c61..8f5ff17 100644 --- a/src/api/tweet.nim +++ b/src/api/tweet.nim @@ -1,4 +1,4 @@ -import httpclient, asyncdispatch, strutils, uri +import asyncdispatch, strutils, uri import ".."/[types, parser] import utils, consts, media diff --git a/src/cache.nim b/src/cache.nim index 3aa9aca..92a3d3e 100644 --- a/src/cache.nim +++ b/src/cache.nim @@ -1,5 +1,7 @@ import asyncdispatch, times, strutils -import types, api +import norm/sqlite + +import types, api/profile dbFromTypes("cache.db", "", "", "", [Profile, Video]) @@ -44,3 +46,20 @@ proc getCachedProfile*(username, agent: string; force=false): Future[Profile] {. proc setProfileCacheTime*(minutes: int) = profileCacheTime = initDuration(minutes=minutes) + +proc cache*(video: var Video) = + withDb: + try: + let v = Video.getOne("videoId = ?", video.videoId) + video.id = v.id + video.update() + except KeyError: + if video.videoId.len > 0: + video.insert() + +proc getCachedVideo*(id: int): Option[Video] = + withDb: + try: + return some Video.getOne("videoId = ?", $id) + except KeyError: + return none Video diff --git a/src/prefs.nim b/src/prefs.nim index 8abba26..2b27e57 100644 --- a/src/prefs.nim +++ b/src/prefs.nim @@ -1,6 +1,7 @@ import strutils, sequtils, macros -import prefs_impl, types +import norm/sqlite +import prefs_impl, types export genUpdatePrefs static: diff --git a/src/types.nim b/src/types.nim index 3b34e16..eeb10a1 100644 --- a/src/types.nim +++ b/src/types.nim @@ -3,7 +3,7 @@ import norm/sqlite import prefs_impl -export sqlite, options +export options type VideoType* = enum @@ -53,7 +53,6 @@ dbTypes: .}: VideoType genPrefsType() -dbFromTypes("cache.db", "", "", "", [Profile, Video]) type QueryKind* = enum