2019-09-08 10:22:52 +00:00
|
|
|
import asyncdispatch, times, strutils
|
2019-10-26 13:33:38 +00:00
|
|
|
import norm/sqlite
|
|
|
|
|
|
|
|
import types, api/profile
|
2019-06-20 14:16:20 +00:00
|
|
|
|
2019-10-26 13:50:42 +00:00
|
|
|
template safeAddColumn(field: typedesc): untyped =
|
|
|
|
try: field.addColumn
|
|
|
|
except DbError: discard
|
|
|
|
|
2019-09-08 10:22:52 +00:00
|
|
|
dbFromTypes("cache.db", "", "", "", [Profile, Video])
|
|
|
|
|
|
|
|
withDb:
|
2019-06-20 18:04:18 +00:00
|
|
|
try:
|
|
|
|
createTables()
|
|
|
|
except DbError:
|
|
|
|
discard
|
2019-10-26 13:50:42 +00:00
|
|
|
Video.title.safeAddColumn
|
|
|
|
Video.description.safeAddColumn
|
2019-06-20 18:04:18 +00:00
|
|
|
|
2019-06-24 23:00:23 +00:00
|
|
|
var profileCacheTime = initDuration(minutes=10)
|
2019-06-20 18:04:18 +00:00
|
|
|
|
2019-08-11 19:26:55 +00:00
|
|
|
proc isOutdated*(profile: Profile): bool =
|
2019-06-20 18:04:18 +00:00
|
|
|
getTime() - profile.updated > profileCacheTime
|
|
|
|
|
2019-08-11 19:26:55 +00:00
|
|
|
proc cache*(profile: var Profile) =
|
2019-09-08 10:22:52 +00:00
|
|
|
withDb:
|
2019-06-20 18:04:18 +00:00
|
|
|
try:
|
2019-08-11 19:26:55 +00:00
|
|
|
let p = Profile.getOne("lower(username) = ?", toLower(profile.username))
|
|
|
|
profile.id = p.id
|
|
|
|
profile.update()
|
2019-06-24 22:55:41 +00:00
|
|
|
except KeyError:
|
2019-08-11 19:26:55 +00:00
|
|
|
if profile.username.len > 0:
|
|
|
|
profile.insert()
|
|
|
|
|
|
|
|
proc hasCachedProfile*(username: string): Option[Profile] =
|
2019-09-08 10:22:52 +00:00
|
|
|
withDb:
|
2019-08-11 19:26:55 +00:00
|
|
|
try:
|
|
|
|
let p = Profile.getOne("lower(username) = ?", toLower(username))
|
|
|
|
doAssert not p.isOutdated
|
2019-09-18 18:54:07 +00:00
|
|
|
result = some p
|
2019-08-11 19:26:55 +00:00
|
|
|
except AssertionError, KeyError:
|
2019-09-18 18:54:07 +00:00
|
|
|
result = none Profile
|
2019-08-11 19:26:55 +00:00
|
|
|
|
2020-01-19 07:34:32 +00:00
|
|
|
proc getCachedProfile*(username, agent: string;
|
|
|
|
force=false): Future[Profile] {.async.} =
|
2019-09-08 10:22:52 +00:00
|
|
|
withDb:
|
2019-08-11 19:26:55 +00:00
|
|
|
try:
|
|
|
|
result.getOne("lower(username) = ?", toLower(username))
|
|
|
|
doAssert not result.isOutdated
|
|
|
|
except AssertionError, KeyError:
|
2019-10-02 08:13:17 +00:00
|
|
|
result = await getProfileFull(username, agent)
|
2019-08-11 19:26:55 +00:00
|
|
|
cache(result)
|
2019-07-31 00:15:43 +00:00
|
|
|
|
|
|
|
proc setProfileCacheTime*(minutes: int) =
|
|
|
|
profileCacheTime = initDuration(minutes=minutes)
|
2019-10-26 13:33:38 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
2019-12-22 00:31:50 +00:00
|
|
|
proc uncache*(id: int64) =
|
|
|
|
withDb:
|
|
|
|
try:
|
|
|
|
var video = Video.getOne("videoId = ?", $id)
|
|
|
|
video.delete()
|
|
|
|
except:
|
|
|
|
discard
|
|
|
|
|
2019-12-09 23:39:12 +00:00
|
|
|
proc getCachedVideo*(id: int64): Option[Video] =
|
2019-10-26 13:33:38 +00:00
|
|
|
withDb:
|
|
|
|
try:
|
|
|
|
return some Video.getOne("videoId = ?", $id)
|
|
|
|
except KeyError:
|
|
|
|
return none Video
|