Skip to content

Commit

Permalink
Improve manga memory cache usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Koitharu committed Nov 26, 2023
1 parent 7c1c0a3 commit 3e77df2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.koitharu.kotatsu.core.parser

import android.net.Uri
import coil.request.CachePolicy
import dagger.Reusable
import org.koitharu.kotatsu.core.model.MangaSource
import org.koitharu.kotatsu.core.util.ext.ifNullOrEmpty
Expand Down Expand Up @@ -85,7 +86,7 @@ class MangaLinkResolver @Inject constructor(

private suspend fun MangaRepository.getDetailsNoCache(manga: Manga): Manga {
return if (this is RemoteMangaRepository) {
getDetails(manga, withCache = false)
getDetails(manga, CachePolicy.READ_ONLY)
} else {
getDetails(manga)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.koitharu.kotatsu.core.parser

import android.util.Log
import coil.request.CachePolicy
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -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> {
cache.getPages(source, chapter.url)?.let { return it }
Expand Down Expand Up @@ -116,17 +117,18 @@ class RemoteMangaRepository(
return related.await()
}

suspend fun getDetails(manga: Manga, withCache: Boolean): Manga {
if (!withCache) {
return parser.getDetails(manga)
suspend fun getDetails(manga: Manga, cachePolicy: CachePolicy): Manga {
if (cachePolicy.readEnabled) {
cache.getDetails(source, manga.url)?.let { return it }
}
cache.getDetails(source, manga.url)?.let { return it }
val details = asyncSafe {
mirrorSwitchInterceptor.withMirrorSwitching {
parser.getDetails(manga)
}
}
cache.putDetails(source, manga.url, details)
if (cachePolicy.writeEnabled) {
cache.putDetails(source, manga.url, details)
}
return details.await()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.koitharu.kotatsu.tracker.domain

import androidx.annotation.VisibleForTesting
import coil.request.CachePolicy
import org.koitharu.kotatsu.core.model.getPreferredBranch
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.history.data.HistoryRepository
import org.koitharu.kotatsu.parsers.model.Manga
Expand Down Expand Up @@ -76,7 +78,9 @@ class Tracker @Inject constructor(
}

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))
if (commit) {
repository.saveUpdates(updates)
Expand Down Expand Up @@ -120,7 +124,7 @@ class Tracker @Inject constructor(
manga = manga,
newChapters = emptyList(),
isValid = chapters.lastOrNull()?.id == track.lastChapterId,
channelId = null
channelId = null,
)
}

Expand Down

0 comments on commit 3e77df2

Please sign in to comment.