nitter/src/cache.nim

29 lines
705 B
Nim
Raw Normal View History

2019-06-20 18:04:18 +00:00
import asyncdispatch, times
2019-06-20 14:16:20 +00:00
import types, api
2019-06-20 18:04:18 +00:00
withDb:
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
proc outdated(profile: Profile): bool =
getTime() - profile.updated > profileCacheTime
proc getCachedProfile*(username: string; force=false): Future[Profile] {.async.} =
withDb:
try:
result.getOne("username = ?", username)
2019-06-24 22:55:41 +00:00
doAssert not result.outdated()
except AssertionError:
var profile = await getProfile(username)
profile.id = result.id
result = profile
result.update()
except KeyError:
result = await getProfile(username)
if result.username.len > 0:
2019-06-20 18:04:18 +00:00
result.insert()