nitter/src/cache.nim

32 lines
820 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
2019-07-31 06:36:24 +00:00
proc getCachedProfile*(username, agent: string; force=false): Future[Profile] {.async.} =
2019-06-20 18:04:18 +00:00
withDb:
try:
result.getOne("username = ?", username)
2019-06-24 22:55:41 +00:00
doAssert not result.outdated()
except AssertionError:
2019-07-31 06:36:24 +00:00
var profile = await getProfile(username, agent)
2019-06-24 22:55:41 +00:00
profile.id = result.id
result = profile
result.update()
except KeyError:
2019-07-31 06:36:24 +00:00
result = await getProfile(username, agent)
2019-06-24 22:55:41 +00:00
if result.username.len > 0:
2019-06-20 18:04:18 +00:00
result.insert()
2019-07-31 00:15:43 +00:00
proc setProfileCacheTime*(minutes: int) =
profileCacheTime = initDuration(minutes=minutes)