2019-09-08 10:22:52 +00:00
|
|
|
import asyncdispatch, times, strutils
|
2019-06-20 14:16:20 +00:00
|
|
|
import types, api
|
|
|
|
|
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-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
|
|
|
|
|
|
|
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:
|
|
|
|
result = await getProfileFull(username)
|
|
|
|
cache(result)
|
2019-07-31 00:15:43 +00:00
|
|
|
|
|
|
|
proc setProfileCacheTime*(minutes: int) =
|
|
|
|
profileCacheTime = initDuration(minutes=minutes)
|