nitter/src/cache.nim

91 lines
2.3 KiB
Nim
Raw Normal View History

2019-09-08 10:22:52 +00:00
import asyncdispatch, times, strutils
2019-10-26 13:33:38 +00:00
import norm/sqlite
import types, api/profile
2019-06-20 14:16:20 +00:00
template safeAddColumn(field: typedesc): untyped =
try: field.addColumn
except DbError: discard
2019-09-08 10:22:52 +00:00
dbFromTypes("cache.db", "", "", "", [Profile, Video])
withDb:
Video.createTable(force=true)
try: Profile.createTable()
except DbError: discard
2019-06-20 18:04:18 +00:00
safeAddColumn Profile.lowername
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-09-08 10:22:52 +00:00
withDb:
2019-06-20 18:04:18 +00:00
try:
let p = Profile.getOne("lowername = ?", profile.lowername)
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] =
2019-09-08 10:22:52 +00:00
withDb:
try:
let p = Profile.getOne("lowername = ?", toLower(username))
doAssert not p.isOutdated
2019-09-18 18:54:07 +00:00
result = some p
except AssertionError, KeyError:
2019-09-18 18:54:07 +00:00
result = none Profile
proc getCachedProfile*(username, agent: string;
force=false): Future[Profile] {.async.} =
2019-09-08 10:22:52 +00:00
withDb:
try:
result.getOne("lowername = ?", toLower(username))
doAssert not result.isOutdated
except AssertionError, KeyError:
2019-10-02 08:13:17 +00:00
result = await getProfileFull(username, agent)
cache(result)
2019-07-31 00:15:43 +00:00
proc setProfileCacheTime*(minutes: int) =
profileCacheTime = initDuration(minutes=minutes)
2019-10-26 13:33:38 +00:00
proc cache*(video: var Video) =
withDb:
try:
let v = Video.getOne("videoId = ?", video.videoId)
video.id = v.id
video.update()
except KeyError:
if video.videoId.len > 0:
video.insert()
proc uncache*(id: int64) =
withDb:
try:
var video = Video.getOne("videoId = ?", $id)
video.delete()
except:
discard
2019-12-09 23:39:12 +00:00
proc getCachedVideo*(id: int64): Option[Video] =
2019-10-26 13:33:38 +00:00
withDb:
try:
return some Video.getOne("videoId = ?", $id)
except KeyError:
return none Video
proc cacheCleaner*() {.async.} =
while true:
await sleepAsync(profileCacheTime.inMilliseconds.int)
withDb:
let up = "updated<" & $toUnix(getTime() - profileCacheTime)
var profiles = Profile.getMany(10000, cond=up)
var videos = Video.getMany(10000, cond=up)
transaction:
for p in profiles.mitems: delete(p)
for v in videos.mitems: delete(v)