pull/497/head
gedoor 4 years ago
parent e7de2536fe
commit 73691d7fd4
  1. 26
      app/src/main/java/io/legado/app/help/CacheManager.kt

@ -2,22 +2,28 @@ package io.legado.app.help
import io.legado.app.App
import io.legado.app.data.entities.Cache
import io.legado.app.model.analyzeRule.QueryTTF
import io.legado.app.utils.ACache
@Suppress("unused")
object CacheManager {
private val queryTTFMap = hashMapOf<String, Pair<Long, QueryTTF>>()
/**
* saveTime 单位为秒
*/
@JvmOverloads
fun put(key: String, value: Any, saveTime: Int = 0) {
if (value is ByteArray) {
ACache.get(App.INSTANCE).put(key, value, saveTime)
} else {
val deadline = if (saveTime == 0) 0 else System.currentTimeMillis() + saveTime * 1000
val cache = Cache(key, value.toString(), deadline)
App.db.cacheDao().insert(cache)
val deadline =
if (saveTime == 0) 0 else System.currentTimeMillis() + saveTime * 1000
when (value) {
is QueryTTF -> queryTTFMap[key] = Pair(deadline, value)
is ByteArray -> ACache.get(App.INSTANCE).put(key, value, saveTime)
else -> {
val cache = Cache(key, value.toString(), deadline)
App.db.cacheDao().insert(cache)
}
}
}
@ -44,4 +50,12 @@ object CacheManager {
fun getByteArray(key: String): ByteArray? {
return ACache.get(App.INSTANCE).getAsBinary(key)
}
fun getQueryTTF(key: String): QueryTTF? {
val cache = queryTTFMap[key] ?: return null
if (cache.first == 0L || cache.first > System.currentTimeMillis()) {
return cache.second
}
return null
}
}
Loading…
Cancel
Save