Add periodic cache cleaner prevent slowdowns

This commit is contained in:
Zed 2020-03-09 00:17:42 +01:00
commit 8c50692299
3 changed files with 22 additions and 7 deletions

View file

@ -10,12 +10,9 @@ template safeAddColumn(field: typedesc): untyped =
dbFromTypes("cache.db", "", "", "", [Profile, Video])
withDb:
try:
createTables()
except DbError:
discard
Video.title.safeAddColumn
Video.description.safeAddColumn
Video.createTable(force=true)
try: Profile.createTable()
except DbError: discard
safeAddColumn Profile.lowername
@ -80,3 +77,14 @@ proc getCachedVideo*(id: int64): Option[Video] =
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)