Http cache

This commit is contained in:
Koitharu
2020-02-26 21:13:39 +02:00
parent 0a4d58b5bd
commit 60567757ae
4 changed files with 22 additions and 1 deletions

View File

@@ -19,6 +19,7 @@ import org.koitharu.kotatsu.core.local.CbzFetcher
import org.koitharu.kotatsu.core.local.PagesCache import org.koitharu.kotatsu.core.local.PagesCache
import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.core.prefs.AppSettings
import org.koitharu.kotatsu.domain.MangaLoaderContext import org.koitharu.kotatsu.domain.MangaLoaderContext
import org.koitharu.kotatsu.utils.CacheUtils
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
class KotatsuApp : Application() { class KotatsuApp : Application() {
@@ -41,7 +42,9 @@ class KotatsuApp : Application() {
modules(listOf( modules(listOf(
module { module {
factory { factory {
okHttp().build() okHttp()
.cache(CacheUtils.createHttpCache(applicationContext))
.build()
} }
}, module { }, module {
single { single {

View File

@@ -18,6 +18,7 @@ import org.koitharu.kotatsu.core.model.Manga
import org.koitharu.kotatsu.domain.MangaProviderFactory import org.koitharu.kotatsu.domain.MangaProviderFactory
import org.koitharu.kotatsu.domain.local.MangaZip import org.koitharu.kotatsu.domain.local.MangaZip
import org.koitharu.kotatsu.ui.common.BaseService 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.await
import org.koitharu.kotatsu.utils.ext.retryUntilSuccess import org.koitharu.kotatsu.utils.ext.retryUntilSuccess
import org.koitharu.kotatsu.utils.ext.safe import org.koitharu.kotatsu.utils.ext.safe
@@ -123,6 +124,7 @@ class DownloadService : BaseService() {
private suspend fun downloadPage(url: String, destination: File): File { private suspend fun downloadPage(url: String, destination: File): File {
val request = Request.Builder() val request = Request.Builder()
.url(url) .url(url)
.cacheControl(CacheUtils.CONTROL_DISABLED)
.get() .get()
.build() .build()
return retryUntilSuccess(3) { return retryUntilSuccess(3) {

View File

@@ -7,6 +7,7 @@ import okhttp3.Request
import org.koin.core.KoinComponent import org.koin.core.KoinComponent
import org.koin.core.inject import org.koin.core.inject
import org.koitharu.kotatsu.core.local.PagesCache import org.koitharu.kotatsu.core.local.PagesCache
import org.koitharu.kotatsu.utils.CacheUtils
import org.koitharu.kotatsu.utils.ext.await import org.koitharu.kotatsu.utils.ext.await
import java.io.File import java.io.File
import java.util.zip.ZipFile import java.util.zip.ZipFile
@@ -41,6 +42,7 @@ class PageLoader : KoinComponent, CoroutineScope, DisposableHandle {
val request = Request.Builder() val request = Request.Builder()
.url(url) .url(url)
.get() .get()
.cacheControl(CacheUtils.CONTROL_DISABLED)
.build() .build()
okHttp.newCall(request).await().use { response -> okHttp.newCall(request).await().use { response ->
cache.put(url) { out -> cache.put(url) { out ->

View File

@@ -2,12 +2,20 @@ package org.koitharu.kotatsu.utils
import android.content.Context import android.content.Context
import androidx.annotation.WorkerThread import androidx.annotation.WorkerThread
import okhttp3.Cache
import okhttp3.CacheControl
import org.koitharu.kotatsu.utils.ext.computeSize import org.koitharu.kotatsu.utils.ext.computeSize
import org.koitharu.kotatsu.utils.ext.sub import org.koitharu.kotatsu.utils.ext.sub
import org.koitharu.kotatsu.utils.ext.sumByLong import org.koitharu.kotatsu.utils.ext.sumByLong
object CacheUtils { object CacheUtils {
@JvmStatic
val CONTROL_DISABLED = CacheControl.Builder()
.noCache()
.noStore()
.build()
@JvmStatic @JvmStatic
fun getCacheDirs(context: Context) = (context.externalCacheDirs + context.cacheDir) fun getCacheDirs(context: Context) = (context.externalCacheDirs + context.cacheDir)
.filterNotNull() .filterNotNull()
@@ -24,4 +32,10 @@ object CacheUtils {
fun clearCache(context: Context, name: String) = getCacheDirs(context) fun clearCache(context: Context, name: String) = getCacheDirs(context)
.map { it.sub(name) } .map { it.sub(name) }
.forEach { it.deleteRecursively() } .forEach { it.deleteRecursively() }
@JvmStatic
fun createHttpCache(context: Context) = Cache(
directory = (context.externalCacheDir ?: context.cacheDir).sub("http"),
maxSize = FileSizeUtils.mbToBytes(60)
)
} }