Http cache
This commit is contained in:
@@ -19,6 +19,7 @@ import org.koitharu.kotatsu.core.local.CbzFetcher
|
||||
import org.koitharu.kotatsu.core.local.PagesCache
|
||||
import org.koitharu.kotatsu.core.prefs.AppSettings
|
||||
import org.koitharu.kotatsu.domain.MangaLoaderContext
|
||||
import org.koitharu.kotatsu.utils.CacheUtils
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
class KotatsuApp : Application() {
|
||||
@@ -41,7 +42,9 @@ class KotatsuApp : Application() {
|
||||
modules(listOf(
|
||||
module {
|
||||
factory {
|
||||
okHttp().build()
|
||||
okHttp()
|
||||
.cache(CacheUtils.createHttpCache(applicationContext))
|
||||
.build()
|
||||
}
|
||||
}, module {
|
||||
single {
|
||||
|
||||
@@ -18,6 +18,7 @@ import org.koitharu.kotatsu.core.model.Manga
|
||||
import org.koitharu.kotatsu.domain.MangaProviderFactory
|
||||
import org.koitharu.kotatsu.domain.local.MangaZip
|
||||
import org.koitharu.kotatsu.ui.common.BaseService
|
||||
import org.koitharu.kotatsu.utils.CacheUtils
|
||||
import org.koitharu.kotatsu.utils.ext.await
|
||||
import org.koitharu.kotatsu.utils.ext.retryUntilSuccess
|
||||
import org.koitharu.kotatsu.utils.ext.safe
|
||||
@@ -123,6 +124,7 @@ class DownloadService : BaseService() {
|
||||
private suspend fun downloadPage(url: String, destination: File): File {
|
||||
val request = Request.Builder()
|
||||
.url(url)
|
||||
.cacheControl(CacheUtils.CONTROL_DISABLED)
|
||||
.get()
|
||||
.build()
|
||||
return retryUntilSuccess(3) {
|
||||
|
||||
@@ -7,6 +7,7 @@ import okhttp3.Request
|
||||
import org.koin.core.KoinComponent
|
||||
import org.koin.core.inject
|
||||
import org.koitharu.kotatsu.core.local.PagesCache
|
||||
import org.koitharu.kotatsu.utils.CacheUtils
|
||||
import org.koitharu.kotatsu.utils.ext.await
|
||||
import java.io.File
|
||||
import java.util.zip.ZipFile
|
||||
@@ -41,6 +42,7 @@ class PageLoader : KoinComponent, CoroutineScope, DisposableHandle {
|
||||
val request = Request.Builder()
|
||||
.url(url)
|
||||
.get()
|
||||
.cacheControl(CacheUtils.CONTROL_DISABLED)
|
||||
.build()
|
||||
okHttp.newCall(request).await().use { response ->
|
||||
cache.put(url) { out ->
|
||||
|
||||
@@ -2,12 +2,20 @@ package org.koitharu.kotatsu.utils
|
||||
|
||||
import android.content.Context
|
||||
import androidx.annotation.WorkerThread
|
||||
import okhttp3.Cache
|
||||
import okhttp3.CacheControl
|
||||
import org.koitharu.kotatsu.utils.ext.computeSize
|
||||
import org.koitharu.kotatsu.utils.ext.sub
|
||||
import org.koitharu.kotatsu.utils.ext.sumByLong
|
||||
|
||||
object CacheUtils {
|
||||
|
||||
@JvmStatic
|
||||
val CONTROL_DISABLED = CacheControl.Builder()
|
||||
.noCache()
|
||||
.noStore()
|
||||
.build()
|
||||
|
||||
@JvmStatic
|
||||
fun getCacheDirs(context: Context) = (context.externalCacheDirs + context.cacheDir)
|
||||
.filterNotNull()
|
||||
@@ -24,4 +32,10 @@ object CacheUtils {
|
||||
fun clearCache(context: Context, name: String) = getCacheDirs(context)
|
||||
.map { it.sub(name) }
|
||||
.forEach { it.deleteRecursively() }
|
||||
|
||||
@JvmStatic
|
||||
fun createHttpCache(context: Context) = Cache(
|
||||
directory = (context.externalCacheDir ?: context.cacheDir).sub("http"),
|
||||
maxSize = FileSizeUtils.mbToBytes(60)
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user