Add periodic cache cleaner prevent slowdowns

This commit is contained in:
Zed 2020-03-09 00:17:42 +01:00
parent 211cd5964a
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]) dbFromTypes("cache.db", "", "", "", [Profile, Video])
withDb: withDb:
try: Video.createTable(force=true)
createTables() try: Profile.createTable()
except DbError: except DbError: discard
discard
Video.title.safeAddColumn
Video.description.safeAddColumn
safeAddColumn Profile.lowername safeAddColumn Profile.lowername
@ -80,3 +77,14 @@ proc getCachedVideo*(id: int64): Option[Video] =
return some Video.getOne("videoId = ?", $id) return some Video.getOne("videoId = ?", $id)
except KeyError: except KeyError:
return none Video 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)

View file

@ -3,7 +3,7 @@ from net import Port
import jester import jester
import types, config, prefs, formatters import types, config, prefs, formatters, cache
import views/[general, about] import views/[general, about]
import routes/[ import routes/[
preferences, timeline, status, media, search, rss, list, preferences, timeline, status, media, search, rss, list,
@ -25,6 +25,8 @@ createMediaRouter(cfg)
createEmbedRouter(cfg) createEmbedRouter(cfg)
createRssRouter(cfg) createRssRouter(cfg)
asyncCheck cacheCleaner()
settings: settings:
port = Port(cfg.port) port = Port(cfg.port)
staticDir = cfg.staticDir staticDir = cfg.staticDir

View file

@ -52,6 +52,11 @@ dbTypes:
parseIt: parseEnum[VideoType](it.s) parseIt: parseEnum[VideoType](it.s)
formatIt: dbValue($it) formatIt: dbValue($it)
.}: VideoType .}: VideoType
updated* {.
dbType: "INTEGER"
parseIt: it.i.fromUnix()
formatIt: dbValue(getTime().toUnix())
.}: Time
genPrefsType() genPrefsType()