1
0
Fork 1
mirror of https://gitlab.com/mangadex-pub/mangadex_at_home.git synced 2024-01-19 02:48:37 +00:00

Reduce DB load

This commit is contained in:
carbotaniuman 2021-02-17 15:57:47 -06:00
parent 0c19bb5c4c
commit 187b089946
2 changed files with 20 additions and 22 deletions

View file

@ -130,18 +130,17 @@ class ImageStorage(
LOGGER.info { "Cache at $size out of $maxSize bytes" }
// we need to prune the cache now
if (size > maxSize * 0.95) {
val toClear = size - (maxSize * 0.9).toLong()
var toClear = size - (maxSize * 0.9).toLong()
LOGGER.info { "Evicting at least $toClear bytes from cache" }
while (toClear > 0) {
val list = database.useConnection { conn ->
conn.prepareStatement(IMAGES_TO_PRUNE).apply {
setLong(1, toClear)
}.use { stmt ->
conn.prepareStatement(IMAGES_TO_PRUNE).use { stmt ->
stmt.executeQuery().let {
val ret = ArrayList<String>()
val ret = ArrayList<Pair<String, Int>>()
while (it.next()) {
ret.add(it.getString(1))
ret.add(it.getString(1) to it.getInt(2))
}
ret
@ -149,9 +148,12 @@ class ImageStorage(
}
}
for (id in list) {
LOGGER.info { "Evicting images $id from cache" }
val count = list.fold(0) { sum, (id, num) ->
deleteImage(id)
sum + num
}
LOGGER.info { "Evicting $count bytes from cache this loop" }
toClear -= count
}
}
}

View file

@ -34,16 +34,12 @@ create table if not exists Images(
accessed timestamp not null default CURRENT_TIMESTAMP,
disk_size integer as ((size + 4095) / 4096 * 4096)
);
create index if not exists Images_lastAccessed_idx on Images(accessed, disk_size, id);
drop index if exists Images_lastAccessed_idx;
create index if not exists Images_accessed on Images(accessed);
"""
const val SIZE_TAKEN_SQL = "select sum(disk_size) from Images"
const val IMAGES_TO_PRUNE = """
select id from (
select id, sum(disk_size)
OVER (order by accessed rows unbounded preceding exclude current row)
as RunningTotal from Images
) as X
WHERE coalesce(X.RunningTotal, 0) <= ?;
select id, disk_size from Images order by accessed asc limit 1000
"""