Manage prefetch cache memory

pull/293/head
Koitharu 3 years ago
parent 3413fe6943
commit bdb2ae9c2f

@ -190,12 +190,12 @@ interface AppModule {
@Provides @Provides
@Singleton @Singleton
fun provideContentCache( fun provideContentCache(
@ApplicationContext context: Context, application: Application,
): ContentCache { ): ContentCache {
return if (context.activityManager?.isLowRamDevice == true) { return if (application.activityManager?.isLowRamDevice == true) {
StubContentCache() StubContentCache()
} else { } else {
MemoryContentCache() MemoryContentCache(application)
} }
} }
} }

@ -7,6 +7,8 @@ import org.koitharu.kotatsu.parsers.model.MangaSource
interface ContentCache { interface ContentCache {
val isCachingEnabled: Boolean
suspend fun getDetails(source: MangaSource, url: String): Manga? suspend fun getDetails(source: MangaSource, url: String): Manga?
fun putDetails(source: MangaSource, url: String, details: Deferred<Manga>) fun putDetails(source: MangaSource, url: String, details: Deferred<Manga>)

@ -1,15 +1,24 @@
package org.koitharu.kotatsu.core.cache package org.koitharu.kotatsu.core.cache
import android.app.Application
import android.content.ComponentCallbacks2
import android.content.res.Configuration
import kotlinx.coroutines.Deferred import kotlinx.coroutines.Deferred
import org.koitharu.kotatsu.parsers.model.Manga import org.koitharu.kotatsu.parsers.model.Manga
import org.koitharu.kotatsu.parsers.model.MangaPage import org.koitharu.kotatsu.parsers.model.MangaPage
import org.koitharu.kotatsu.parsers.model.MangaSource import org.koitharu.kotatsu.parsers.model.MangaSource
@Suppress("DeferredResultUnused") @Suppress("DeferredResultUnused")
class MemoryContentCache : ContentCache { class MemoryContentCache(application: Application) : ContentCache, ComponentCallbacks2 {
private val detailsCache = DeferredLruCache<Manga>(10) init {
private val pagesCache = DeferredLruCache<List<MangaPage>>(10) application.registerComponentCallbacks(this)
}
private val detailsCache = DeferredLruCache<Manga>(4)
private val pagesCache = DeferredLruCache<List<MangaPage>>(4)
override val isCachingEnabled: Boolean = true
override suspend fun getDetails(source: MangaSource, url: String): Manga? { override suspend fun getDetails(source: MangaSource, url: String): Manga? {
return detailsCache[ContentCache.Key(source, url)]?.await() return detailsCache[ContentCache.Key(source, url)]?.await()
@ -26,4 +35,27 @@ class MemoryContentCache : ContentCache {
override fun putPages(source: MangaSource, url: String, pages: Deferred<List<MangaPage>>) { override fun putPages(source: MangaSource, url: String, pages: Deferred<List<MangaPage>>) {
pagesCache.put(ContentCache.Key(source, url), pages) pagesCache.put(ContentCache.Key(source, url), pages)
} }
override fun onConfigurationChanged(newConfig: Configuration) = Unit
override fun onLowMemory() = Unit
override fun onTrimMemory(level: Int) {
trimCache(detailsCache, level)
trimCache(pagesCache, level)
}
private fun trimCache(cache: DeferredLruCache<*>, level: Int) {
when (level) {
ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL,
ComponentCallbacks2.TRIM_MEMORY_COMPLETE,
ComponentCallbacks2.TRIM_MEMORY_MODERATE -> cache.evictAll()
ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN,
ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW,
ComponentCallbacks2.TRIM_MEMORY_BACKGROUND -> cache.trimToSize(1)
else -> cache.trimToSize(cache.maxSize() / 2)
}
}
} }

@ -7,6 +7,8 @@ import org.koitharu.kotatsu.parsers.model.MangaSource
class StubContentCache : ContentCache { class StubContentCache : ContentCache {
override val isCachingEnabled: Boolean = false
override suspend fun getDetails(source: MangaSource, url: String): Manga? = null override suspend fun getDetails(source: MangaSource, url: String): Manga? = null
override fun putDetails(source: MangaSource, url: String, details: Deferred<Manga>) = Unit override fun putDetails(source: MangaSource, url: String, details: Deferred<Manga>) = Unit

@ -9,7 +9,6 @@ import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import org.koitharu.kotatsu.base.ui.CoroutineIntentService import org.koitharu.kotatsu.base.ui.CoroutineIntentService
import org.koitharu.kotatsu.core.cache.ContentCache import org.koitharu.kotatsu.core.cache.ContentCache
import org.koitharu.kotatsu.core.cache.StubContentCache
import org.koitharu.kotatsu.core.model.parcelable.ParcelableManga import org.koitharu.kotatsu.core.model.parcelable.ParcelableManga
import org.koitharu.kotatsu.core.model.parcelable.ParcelableMangaChapters import org.koitharu.kotatsu.core.model.parcelable.ParcelableMangaChapters
import org.koitharu.kotatsu.core.parser.MangaRepository import org.koitharu.kotatsu.core.parser.MangaRepository
@ -87,10 +86,7 @@ class MangaPrefetchService : CoroutineIntentService() {
return false return false
} }
val entryPoint = EntryPointAccessors.fromApplication(context, PrefetchCompanionEntryPoint::class.java) val entryPoint = EntryPointAccessors.fromApplication(context, PrefetchCompanionEntryPoint::class.java)
if (entryPoint.contentCache is StubContentCache) { return entryPoint.contentCache.isCachingEnabled && entryPoint.settings.isContentPrefetchEnabled()
return false
}
return entryPoint.settings.isContentPrefetchEnabled()
} }
} }
} }

Loading…
Cancel
Save