nitter/src/cache.nim

45 lines
1.2 KiB
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 isOutdated*(profile: Profile): bool =
2019-06-20 18:04:18 +00:00
getTime() - profile.updated > profileCacheTime
proc cache*(profile: var Profile) =
2019-06-20 18:04:18 +00:00
withDb:
try:
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:
if profile.username.len > 0:
profile.insert()
proc hasCachedProfile*(username: string): Option[Profile] =
withDb:
try:
let p = Profile.getOne("lower(username) = ?", toLower(username))
doAssert not p.isOutdated
result = some(p)
except AssertionError, KeyError:
result = none(Profile)
proc getCachedProfile*(username, agent: string; force=false): Future[Profile] {.async.} =
withDb:
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)