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:
parent
0c19bb5c4c
commit
187b089946
18
src/main/kotlin/mdnet/cache/ImageStorage.kt
vendored
18
src/main/kotlin/mdnet/cache/ImageStorage.kt
vendored
|
@ -130,18 +130,17 @@ class ImageStorage(
|
||||||
LOGGER.info { "Cache at $size out of $maxSize bytes" }
|
LOGGER.info { "Cache at $size out of $maxSize bytes" }
|
||||||
// we need to prune the cache now
|
// we need to prune the cache now
|
||||||
if (size > maxSize * 0.95) {
|
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" }
|
LOGGER.info { "Evicting at least $toClear bytes from cache" }
|
||||||
|
|
||||||
|
while (toClear > 0) {
|
||||||
val list = database.useConnection { conn ->
|
val list = database.useConnection { conn ->
|
||||||
conn.prepareStatement(IMAGES_TO_PRUNE).apply {
|
conn.prepareStatement(IMAGES_TO_PRUNE).use { stmt ->
|
||||||
setLong(1, toClear)
|
|
||||||
}.use { stmt ->
|
|
||||||
stmt.executeQuery().let {
|
stmt.executeQuery().let {
|
||||||
val ret = ArrayList<String>()
|
val ret = ArrayList<Pair<String, Int>>()
|
||||||
|
|
||||||
while (it.next()) {
|
while (it.next()) {
|
||||||
ret.add(it.getString(1))
|
ret.add(it.getString(1) to it.getInt(2))
|
||||||
}
|
}
|
||||||
|
|
||||||
ret
|
ret
|
||||||
|
@ -149,9 +148,12 @@ class ImageStorage(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (id in list) {
|
val count = list.fold(0) { sum, (id, num) ->
|
||||||
LOGGER.info { "Evicting images $id from cache" }
|
|
||||||
deleteImage(id)
|
deleteImage(id)
|
||||||
|
sum + num
|
||||||
|
}
|
||||||
|
LOGGER.info { "Evicting $count bytes from cache this loop" }
|
||||||
|
toClear -= count
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
10
src/main/kotlin/mdnet/cache/metadata.kt
vendored
10
src/main/kotlin/mdnet/cache/metadata.kt
vendored
|
@ -34,16 +34,12 @@ create table if not exists Images(
|
||||||
accessed timestamp not null default CURRENT_TIMESTAMP,
|
accessed timestamp not null default CURRENT_TIMESTAMP,
|
||||||
disk_size integer as ((size + 4095) / 4096 * 4096)
|
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 SIZE_TAKEN_SQL = "select sum(disk_size) from Images"
|
||||||
|
|
||||||
const val IMAGES_TO_PRUNE = """
|
const val IMAGES_TO_PRUNE = """
|
||||||
select id from (
|
select id, disk_size from Images order by accessed asc limit 1000
|
||||||
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) <= ?;
|
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in a new issue