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
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

View File

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

View File

@ -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

View File

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

View File

@ -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