cacheFile:首次下没有返回下载内容;正确处理saveTime=0的情况

pull/1502/head
Xwite 3 years ago
parent 099d9a81bb
commit 586b157328
  1. 11
      app/src/main/java/io/legado/app/help/CacheManager.kt
  2. 15
      app/src/main/java/io/legado/app/help/JsExtensions.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()) {

@ -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
}

Loading…
Cancel
Save