From 586b1573286b52319eac2239047c6cf758caa419 Mon Sep 17 00:00:00 2001 From: Xwite <1797350009@qq.com> Date: Fri, 7 Jan 2022 09:35:39 +0800 Subject: [PATCH] =?UTF-8?q?cacheFile:=E9=A6=96=E6=AC=A1=E4=B8=8B=E6=B2=A1?= =?UTF-8?q?=E6=9C=89=E8=BF=94=E5=9B=9E=E4=B8=8B=E8=BD=BD=E5=86=85=E5=AE=B9?= =?UTF-8?q?;=E6=AD=A3=E7=A1=AE=E5=A4=84=E7=90=86saveTime=3D0=E7=9A=84?= =?UTF-8?q?=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/legado/app/help/CacheManager.kt | 11 +++++++++++ .../main/java/io/legado/app/help/JsExtensions.kt | 15 +++++++-------- 2 files changed, 18 insertions(+), 8 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 60627ee35..cc1ce71f1 100644 --- a/app/src/main/java/io/legado/app/help/CacheManager.kt +++ b/app/src/main/java/io/legado/app/help/CacheManager.kt @@ -28,6 +28,13 @@ object CacheManager { } } + fun putString2File(key: String, value: String, saveTime: Int = 0) { + if (saveTime == 0) { + //不设置过期时间 + ACache.get(appCtx).put(key, value) + } else ACache.get(appCtx).put(key, value, saveTime) + } + fun get(key: String): String? { return appDb.cacheDao.get(key, System.currentTimeMillis()) } @@ -52,6 +59,10 @@ object CacheManager { return ACache.get(appCtx).getAsBinary(key) } + fun getString(key: String): String? { + return ACache.get(appCtx).getAsString(key) + } + fun getQueryTTF(key: String): QueryTTF? { val cache = queryTTFMap[key] ?: return null if (cache.first == 0L || cache.first > System.currentTimeMillis()) { diff --git a/app/src/main/java/io/legado/app/help/JsExtensions.kt b/app/src/main/java/io/legado/app/help/JsExtensions.kt index adde0e962..69a21ae79 100644 --- a/app/src/main/java/io/legado/app/help/JsExtensions.kt +++ b/app/src/main/java/io/legado/app/help/JsExtensions.kt @@ -138,15 +138,14 @@ interface JsExtensions { * @param saveTime 缓存时间,单位:秒 * @return 返回缓存后的文件内容 */ - fun cacheFile(url: String, saveTime: Int = 0): String? { - val key = md5Encode16(url) - val cache = ACache.get(appCtx).getAsString(key) + fun cacheFile(urlStr: String, saveTime: Int = 0): String? { + val key = md5Encode16(urlStr) + val cache = CacheManager.getString(key) if(cache.isNullOrBlank()) { - log("首次下载${url}...") - val value = ajax(url) ?: "" - if (saveTime == 0) { - ACache.get(appCtx).put(key, value) - } else ACache.get(appCtx).put(key, value, saveTime) + log("首次下载 ${urlStr}") + val value = ajax(urlStr) ?: return null + CacheManager.putString2File(key, value, saveTime) + return value } return cache }