Refactor video cache

This commit is contained in:
Zed 2019-10-26 15:33:38 +02:00
parent aeeea492c4
commit d179ac547c
5 changed files with 45 additions and 28 deletions

View file

@ -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 import utils, consts
var var
@ -62,7 +62,13 @@ proc getGuestToken(agent: string; force=false): Future[string] {.async.} =
result = json["guest_token"].to(string) result = json["guest_token"].to(string)
guestToken = result 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 if tweet.video.isNone(): return
let let
@ -75,31 +81,23 @@ proc getVideoFetch(tweet: Tweet; agent, token: string) {.async.} =
if getTime() - tokenUpdated > initDuration(seconds=1): if getTime() - tokenUpdated > initDuration(seconds=1):
tokenUpdated = getTime() tokenUpdated = getTime()
discard await getGuestToken(agent, force=true) discard await getGuestToken(agent, force=true)
await getVideoFetch(tweet, agent, guestToken) result = await getVideoFetch(tweet, agent, guestToken)
return return
if tweet.card.isNone: var video = parseVideo(json, tweet.id)
tweet.video = some parseVideo(json, tweet.id) video.title = get(tweet.video).title
else: video.description = get(tweet.video).description
get(tweet.card).video = some parseVideo(json, tweet.id) cache(video)
tweet.video = none Video
result = some video
tokenUses.inc 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.} = proc getVideo*(tweet: Tweet; agent, token: string; force=false) {.async.} =
withCustomDb("cache.db", "", "", ""): var video = getCachedVideo(tweet.id)
try: if video.isNone:
getVideoVar(tweet) = some Video.getOne("videoId = ?", tweet.id) video = await getVideoFetch(tweet, agent, token)
except KeyError: getVideoVar(tweet) = video
await getVideoFetch(tweet, agent, token) if tweet.card.isSome: tweet.video = none Video
var video = getVideoVar(tweet)
if video.isSome():
get(video).insert()
proc getPoll*(tweet: Tweet; agent: string) {.async.} = proc getPoll*(tweet: Tweet; agent: string) {.async.} =
if tweet.poll.isNone(): return if tweet.poll.isNone(): return

View file

@ -1,4 +1,4 @@
import httpclient, asyncdispatch, strutils, uri import asyncdispatch, strutils, uri
import ".."/[types, parser] import ".."/[types, parser]
import utils, consts, media import utils, consts, media

View file

@ -1,5 +1,7 @@
import asyncdispatch, times, strutils import asyncdispatch, times, strutils
import types, api import norm/sqlite
import types, api/profile
dbFromTypes("cache.db", "", "", "", [Profile, Video]) dbFromTypes("cache.db", "", "", "", [Profile, Video])
@ -44,3 +46,20 @@ proc getCachedProfile*(username, agent: string; force=false): Future[Profile] {.
proc setProfileCacheTime*(minutes: int) = proc setProfileCacheTime*(minutes: int) =
profileCacheTime = initDuration(minutes=minutes) 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

View file

@ -1,6 +1,7 @@
import strutils, sequtils, macros import strutils, sequtils, macros
import prefs_impl, types import norm/sqlite
import prefs_impl, types
export genUpdatePrefs export genUpdatePrefs
static: static:

View file

@ -3,7 +3,7 @@ import norm/sqlite
import prefs_impl import prefs_impl
export sqlite, options export options
type type
VideoType* = enum VideoType* = enum
@ -53,7 +53,6 @@ dbTypes:
.}: VideoType .}: VideoType
genPrefsType() genPrefsType()
dbFromTypes("cache.db", "", "", "", [Profile, Video])
type type
QueryKind* = enum QueryKind* = enum