From da0e451233106536c7031603c6efad9684280110 Mon Sep 17 00:00:00 2001 From: Xwite <1797350009@qq.com> Date: Sat, 7 May 2022 08:35:19 +0800 Subject: [PATCH] =?UTF-8?q?fix:okhttp=E8=87=AA=E5=8A=A8=E4=BF=9D=E5=AD=98?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E7=9A=84cookie?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/help/CacheManager.kt | 28 ++++++------------- .../io/legado/app/help/http/CookieStore.kt | 7 ++++- .../io/legado/app/help/http/HttpHelper.kt | 2 +- 3 files changed, 15 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/CacheManager.kt b/app/src/main/java/io/legado/app/help/CacheManager.kt index e9e597648..f628cb599 100644 --- a/app/src/main/java/io/legado/app/help/CacheManager.kt +++ b/app/src/main/java/io/legado/app/help/CacheManager.kt @@ -11,11 +11,7 @@ import splitties.init.appCtx object CacheManager { private val queryTTFMap = hashMapOf>() - private val memoryLruCache = object : LruCache(100) { - override fun sizeOf(key: String, value: Cache): Int { - return 1 - } - } + private val memoryLruCache = object : LruCache(100) /** * saveTime 单位为秒 @@ -29,15 +25,14 @@ object CacheManager { is ByteArray -> ACache.get(appCtx).put(key, value, saveTime) else -> { val cache = Cache(key, value.toString(), deadline) - memoryLruCache.put(key, cache) + putMemory(key, value.toString()) appDb.cacheDao.insert(cache) } } } - fun putMemory(key: String, value: Any) { - val cache = Cache(key, value.toString(), 0) - memoryLruCache.put(key, cache) + fun putMemory(key: String, value: String) { + memoryLruCache.put(key, value) } fun get(key: String): String? { @@ -46,22 +41,15 @@ object CacheManager { } val cache = appDb.cacheDao.get(key) if (cache != null && (cache.deadline == 0L || cache.deadline > System.currentTimeMillis())) { - memoryLruCache.put(key, cache) + putMemory(key, cache.value ?: "") return cache.value } return null } - //从内存中获取数据 使用lruCache 支持过期功能 - private fun getFromMemory(key: String): String? { - val cache = memoryLruCache.get(key) ?: return null - val deadline = cache.deadline - return if (deadline == 0L || deadline > System.currentTimeMillis()) { - cache.value - } else { - memoryLruCache.remove(key) - null - } + //从内存中获取数据 使用lruCache + fun getFromMemory(key: String): String? { + return memoryLruCache.get(key) } fun getInt(key: String): Int? { diff --git a/app/src/main/java/io/legado/app/help/http/CookieStore.kt b/app/src/main/java/io/legado/app/help/http/CookieStore.kt index 0da6c0d97..b0d892b36 100644 --- a/app/src/main/java/io/legado/app/help/http/CookieStore.kt +++ b/app/src/main/java/io/legado/app/help/http/CookieStore.kt @@ -6,6 +6,7 @@ import android.text.TextUtils import io.legado.app.data.appDb import io.legado.app.data.entities.Cookie import io.legado.app.help.http.api.CookieManager +import io.legado.app.help.CacheManager import io.legado.app.utils.NetworkUtils object CookieStore : CookieManager { @@ -14,6 +15,7 @@ object CookieStore : CookieManager { *保存cookie到数据库,会自动识别url的二级域名 */ override fun setCookie(url: String, cookie: String?) { + CacheManager.putMemory(url, cookie ?: "") val cookieBean = Cookie(NetworkUtils.getSubDomain(url), cookie ?: "") appDb.cookieDao.insert(cookieBean) } @@ -37,8 +39,11 @@ object CookieStore : CookieManager { *获取url所属的二级域名的cookie */ override fun getCookie(url: String): String { + CacheManager.getFromMemory(url)?.let { return it } val cookieBean = appDb.cookieDao.get(NetworkUtils.getSubDomain(url)) - return cookieBean?.cookie ?: "" + val cookie = cookieBean?.cookie ?: "" + CacheManager.putMemory(url, cookie ?: "") + return cookie } fun getKey(url: String, key: String): String { diff --git a/app/src/main/java/io/legado/app/help/http/HttpHelper.kt b/app/src/main/java/io/legado/app/help/http/HttpHelper.kt index a1db99199..a19e2b03c 100644 --- a/app/src/main/java/io/legado/app/help/http/HttpHelper.kt +++ b/app/src/main/java/io/legado/app/help/http/HttpHelper.kt @@ -35,7 +35,7 @@ val okHttpClient: OkHttpClient by lazy { .cookieJar(object : CookieJar { override fun saveFromResponse(url: HttpUrl, cookies: List) { cookies.forEach { - CookieStore.setCookie(url.toString(), it.value) + CookieStore.replaceCookie(url.toString(), "${it.name}=${it.value}") } } override fun loadForRequest(url: HttpUrl): List {