Improve manga memory cache usage

This commit is contained in:
Koitharu
2023-11-26 09:07:39 +02:00
parent 7c1c0a38fa
commit 3e77df20a2
3 changed files with 16 additions and 9 deletions

View File

@@ -1,6 +1,7 @@
package org.koitharu.kotatsu.core.parser package org.koitharu.kotatsu.core.parser
import android.net.Uri import android.net.Uri
import coil.request.CachePolicy
import dagger.Reusable import dagger.Reusable
import org.koitharu.kotatsu.core.model.MangaSource import org.koitharu.kotatsu.core.model.MangaSource
import org.koitharu.kotatsu.core.util.ext.ifNullOrEmpty import org.koitharu.kotatsu.core.util.ext.ifNullOrEmpty
@@ -85,7 +86,7 @@ class MangaLinkResolver @Inject constructor(
private suspend fun MangaRepository.getDetailsNoCache(manga: Manga): Manga { private suspend fun MangaRepository.getDetailsNoCache(manga: Manga): Manga {
return if (this is RemoteMangaRepository) { return if (this is RemoteMangaRepository) {
getDetails(manga, withCache = false) getDetails(manga, CachePolicy.READ_ONLY)
} else { } else {
getDetails(manga) getDetails(manga)
} }

View File

@@ -1,6 +1,7 @@
package org.koitharu.kotatsu.core.parser package org.koitharu.kotatsu.core.parser
import android.util.Log import android.util.Log
import coil.request.CachePolicy
import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@@ -82,7 +83,7 @@ class RemoteMangaRepository(
} }
} }
override suspend fun getDetails(manga: Manga): Manga = getDetails(manga, withCache = true) override suspend fun getDetails(manga: Manga): Manga = getDetails(manga, CachePolicy.ENABLED)
override suspend fun getPages(chapter: MangaChapter): List<MangaPage> { override suspend fun getPages(chapter: MangaChapter): List<MangaPage> {
cache.getPages(source, chapter.url)?.let { return it } cache.getPages(source, chapter.url)?.let { return it }
@@ -116,17 +117,18 @@ class RemoteMangaRepository(
return related.await() return related.await()
} }
suspend fun getDetails(manga: Manga, withCache: Boolean): Manga { suspend fun getDetails(manga: Manga, cachePolicy: CachePolicy): Manga {
if (!withCache) { if (cachePolicy.readEnabled) {
return parser.getDetails(manga) cache.getDetails(source, manga.url)?.let { return it }
} }
cache.getDetails(source, manga.url)?.let { return it }
val details = asyncSafe { val details = asyncSafe {
mirrorSwitchInterceptor.withMirrorSwitching { mirrorSwitchInterceptor.withMirrorSwitching {
parser.getDetails(manga) parser.getDetails(manga)
} }
} }
cache.putDetails(source, manga.url, details) if (cachePolicy.writeEnabled) {
cache.putDetails(source, manga.url, details)
}
return details.await() return details.await()
} }

View File

@@ -1,8 +1,10 @@
package org.koitharu.kotatsu.tracker.domain package org.koitharu.kotatsu.tracker.domain
import androidx.annotation.VisibleForTesting import androidx.annotation.VisibleForTesting
import coil.request.CachePolicy
import org.koitharu.kotatsu.core.model.getPreferredBranch import org.koitharu.kotatsu.core.model.getPreferredBranch
import org.koitharu.kotatsu.core.parser.MangaRepository import org.koitharu.kotatsu.core.parser.MangaRepository
import org.koitharu.kotatsu.core.parser.RemoteMangaRepository
import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.core.prefs.AppSettings
import org.koitharu.kotatsu.history.data.HistoryRepository import org.koitharu.kotatsu.history.data.HistoryRepository
import org.koitharu.kotatsu.parsers.model.Manga import org.koitharu.kotatsu.parsers.model.Manga
@@ -76,7 +78,9 @@ class Tracker @Inject constructor(
} }
suspend fun fetchUpdates(track: MangaTracking, commit: Boolean): MangaUpdates.Success { suspend fun fetchUpdates(track: MangaTracking, commit: Boolean): MangaUpdates.Success {
val manga = mangaRepositoryFactory.create(track.manga.source).getDetails(track.manga) val repo = mangaRepositoryFactory.create(track.manga.source)
require(repo is RemoteMangaRepository) { "Repository ${repo.javaClass.simpleName} is not supported" }
val manga = repo.getDetails(track.manga, CachePolicy.WRITE_ONLY)
val updates = compare(track, manga, getBranch(manga)) val updates = compare(track, manga, getBranch(manga))
if (commit) { if (commit) {
repository.saveUpdates(updates) repository.saveUpdates(updates)
@@ -120,7 +124,7 @@ class Tracker @Inject constructor(
manga = manga, manga = manga,
newChapters = emptyList(), newChapters = emptyList(),
isValid = chapters.lastOrNull()?.id == track.lastChapterId, isValid = chapters.lastOrNull()?.id == track.lastChapterId,
channelId = null channelId = null,
) )
} }