Jelajahi Sumber

Refactor video cache

Zed 6 tahun lalu
induk
melakukan
d179ac547c
5 mengubah file dengan 45 tambahan dan 28 penghapusan
  1. 21 23
      src/api/media.nim
  2. 1 1
      src/api/tweet.nim
  3. 20 1
      src/cache.nim
  4. 2 1
      src/prefs.nim
  5. 1 2
      src/types.nim

+ 21 - 23
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
-  tokenUses.inc
+  var video = parseVideo(json, tweet.id)
+  video.title = get(tweet.video).title
+  video.description = get(tweet.video).description
+  cache(video)
 
-proc getVideoVar(tweet: Tweet): var Option[Video] =
-  if tweet.card.isSome():
-    return get(tweet.card).video
-  else:
-    return tweet.video
+  result = some video
+  tokenUses.inc
 
 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

+ 1 - 1
src/api/tweet.nim

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

+ 20 - 1
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

+ 2 - 1
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:

+ 1 - 2
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