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
32
src/main/kotlin/mdnet/cache/ImageStorage.kt
vendored
32
src/main/kotlin/mdnet/cache/ImageStorage.kt
vendored
|
@ -130,28 +130,30 @@ 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" }
|
||||
|
||||
val list = database.useConnection { conn ->
|
||||
conn.prepareStatement(IMAGES_TO_PRUNE).apply {
|
||||
setLong(1, toClear)
|
||||
}.use { stmt ->
|
||||
stmt.executeQuery().let {
|
||||
val ret = ArrayList<String>()
|
||||
while (toClear > 0) {
|
||||
val list = database.useConnection { conn ->
|
||||
conn.prepareStatement(IMAGES_TO_PRUNE).use { stmt ->
|
||||
stmt.executeQuery().let {
|
||||
val ret = ArrayList<Pair<String, Int>>()
|
||||
|
||||
while (it.next()) {
|
||||
ret.add(it.getString(1))
|
||||
while (it.next()) {
|
||||
ret.add(it.getString(1) to it.getInt(2))
|
||||
}
|
||||
|
||||
ret
|
||||
}
|
||||
|
||||
ret
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (id in list) {
|
||||
LOGGER.info { "Evicting images $id from cache" }
|
||||
deleteImage(id)
|
||||
val count = list.fold(0) { sum, (id, num) ->
|
||||
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,
|
||||
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
|
||||
"""
|
||||
|
|
Loading…
Reference in a new issue