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])
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)

View File

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

View File

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