2019-06-20 20:04:18 +02:00
|
|
|
import asyncdispatch, times
|
2019-06-20 16:16:20 +02:00
|
|
|
import types, api
|
|
|
|
|
2019-06-20 20:04:18 +02:00
|
|
|
withDb:
|
|
|
|
try:
|
|
|
|
createTables()
|
|
|
|
except DbError:
|
|
|
|
discard
|
|
|
|
|
2019-06-25 01:00:23 +02:00
|
|
|
var profileCacheTime = initDuration(minutes=10)
|
2019-06-20 20:04:18 +02:00
|
|
|
|
|
|
|
proc outdated(profile: Profile): bool =
|
|
|
|
getTime() - profile.updated > profileCacheTime
|
|
|
|
|
2019-07-31 08:36:24 +02:00
|
|
|
proc getCachedProfile*(username, agent: string; force=false): Future[Profile] {.async.} =
|
2019-06-20 20:04:18 +02:00
|
|
|
withDb:
|
|
|
|
try:
|
|
|
|
result.getOne("username = ?", username)
|
2019-06-25 00:55:41 +02:00
|
|
|
doAssert not result.outdated()
|
|
|
|
except AssertionError:
|
2019-07-31 08:36:24 +02:00
|
|
|
var profile = await getProfile(username, agent)
|
2019-06-25 00:55:41 +02:00
|
|
|
profile.id = result.id
|
|
|
|
result = profile
|
|
|
|
result.update()
|
|
|
|
except KeyError:
|
2019-07-31 08:36:24 +02:00
|
|
|
result = await getProfile(username, agent)
|
2019-06-25 00:55:41 +02:00
|
|
|
if result.username.len > 0:
|
2019-06-20 20:04:18 +02:00
|
|
|
result.insert()
|
2019-07-31 02:15:43 +02:00
|
|
|
|
|
|
|
proc setProfileCacheTime*(minutes: int) =
|
|
|
|
profileCacheTime = initDuration(minutes=minutes)
|