From e7bd74429ef92c36bc1036c1538b9625c0544f5a Mon Sep 17 00:00:00 2001 From: Koitharu Date: Tue, 18 Jul 2023 15:43:28 +0300 Subject: [PATCH] Cache related manga lists --- .../kotatsu/core/cache/ContentCache.kt | 25 +++++++++++++++++-- .../kotatsu/core/cache/MemoryContentCache.kt | 10 ++++++++ .../kotatsu/core/cache/StubContentCache.kt | 4 +++ .../core/parser/RemoteMangaRepository.kt | 7 +++++- 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/cache/ContentCache.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/cache/ContentCache.kt index 791ce9358..0992360a8 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/cache/ContentCache.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/cache/ContentCache.kt @@ -16,8 +16,29 @@ interface ContentCache { fun putPages(source: MangaSource, url: String, pages: SafeDeferred>) - data class Key( + suspend fun getRelatedManga(source: MangaSource, url: String): List? + + fun putRelatedManga(source: MangaSource, url: String, related: SafeDeferred>) + + class Key( val source: MangaSource, val url: String, - ) + ) { + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + + other as Key + + if (source != other.source) return false + return url == other.url + } + + override fun hashCode(): Int { + var result = source.hashCode() + result = 31 * result + url.hashCode() + return result + } + } } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/cache/MemoryContentCache.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/cache/MemoryContentCache.kt index 722b06d41..986b61947 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/cache/MemoryContentCache.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/cache/MemoryContentCache.kt @@ -16,6 +16,7 @@ class MemoryContentCache(application: Application) : ContentCache, ComponentCall private val detailsCache = ExpiringLruCache>(4, 5, TimeUnit.MINUTES) private val pagesCache = ExpiringLruCache>>(4, 10, TimeUnit.MINUTES) + private val relatedMangaCache = ExpiringLruCache>>(4, 10, TimeUnit.MINUTES) override val isCachingEnabled: Boolean = true @@ -35,6 +36,14 @@ class MemoryContentCache(application: Application) : ContentCache, ComponentCall pagesCache[ContentCache.Key(source, url)] = pages } + override suspend fun getRelatedManga(source: MangaSource, url: String): List? { + return relatedMangaCache[ContentCache.Key(source, url)]?.awaitOrNull() + } + + override fun putRelatedManga(source: MangaSource, url: String, related: SafeDeferred>) { + relatedMangaCache[ContentCache.Key(source, url)] = related + } + override fun onConfigurationChanged(newConfig: Configuration) = Unit override fun onLowMemory() = Unit @@ -42,6 +51,7 @@ class MemoryContentCache(application: Application) : ContentCache, ComponentCall override fun onTrimMemory(level: Int) { trimCache(detailsCache, level) trimCache(pagesCache, level) + trimCache(relatedMangaCache, level) } private fun trimCache(cache: ExpiringLruCache<*>, level: Int) { diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/cache/StubContentCache.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/cache/StubContentCache.kt index 4750deb98..ade77ea97 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/cache/StubContentCache.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/cache/StubContentCache.kt @@ -15,4 +15,8 @@ class StubContentCache : ContentCache { override suspend fun getPages(source: MangaSource, url: String): List? = null override fun putPages(source: MangaSource, url: String, pages: SafeDeferred>) = Unit + + override suspend fun getRelatedManga(source: MangaSource, url: String): List? = null + + override fun putRelatedManga(source: MangaSource, url: String, related: SafeDeferred>) = Unit } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/RemoteMangaRepository.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/RemoteMangaRepository.kt index 9e7f59daf..84ad71d5f 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/RemoteMangaRepository.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/RemoteMangaRepository.kt @@ -95,7 +95,12 @@ class RemoteMangaRepository( suspend fun getFavicons(): Favicons = parser.getFavicons() override suspend fun getRelated(seed: Manga): List { - return parser.getRelatedManga(seed).filterNot { it.id == seed.id } + cache.getRelatedManga(source, seed.url)?.let { return it } + val related = asyncSafe { + parser.getRelatedManga(seed).filterNot { it.id == seed.id } + } + cache.putRelatedManga(source, seed.url, related) + return related.await() } fun getAuthProvider(): MangaParserAuthProvider? = parser as? MangaParserAuthProvider