Files
QieKan-3.0/app/src/main/java/io/legado/app/help/CacheManager.kt
T

75 lines
2.0 KiB
Kotlin

package io.legado.app.help
import io.legado.app.data.appDb
import io.legado.app.data.entities.Cache
import io.legado.app.model.analyzeRule.QueryTTF
import io.legado.app.utils.ACache
import splitties.init.appCtx
@Suppress("unused")
object CacheManager {
private val queryTTFMap = hashMapOf<String, Pair<Long, QueryTTF>>()
/**
* saveTime 单位为秒
*/
@JvmOverloads
fun put(key: String, value: Any, saveTime: Int = 0) {
val deadline =
if (saveTime == 0) 0 else System.currentTimeMillis() + saveTime * 1000
when (value) {
is QueryTTF -> queryTTFMap[key] = Pair(deadline, value)
is ByteArray -> ACache.get(appCtx).put(key, value, saveTime)
else -> {
val cache = Cache(key, value.toString(), deadline)
appDb.cacheDao.insert(cache)
}
}
}
fun putString2File(key: String, value: String, saveTime: Int = 0) {
ACache.get(appCtx).put(key, value, saveTime)
}
fun get(key: String): String? {
return appDb.cacheDao.get(key, System.currentTimeMillis())
}
fun getInt(key: String): Int? {
return get(key)?.toIntOrNull()
}
fun getLong(key: String): Long? {
return get(key)?.toLongOrNull()
}
fun getDouble(key: String): Double? {
return get(key)?.toDoubleOrNull()
}
fun getFloat(key: String): Float? {
return get(key)?.toFloatOrNull()
}
fun getByteArray(key: String): ByteArray? {
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()) {
return cache.second
}
return null
}
fun delete(key: String) {
appDb.cacheDao.delete(key)
ACache.get(appCtx).remove(key)
}
}