|
|
@ -11,7 +11,7 @@ import splitties.init.appCtx |
|
|
|
object CacheManager { |
|
|
|
object CacheManager { |
|
|
|
|
|
|
|
|
|
|
|
private val queryTTFMap = hashMapOf<String, Pair<Long, QueryTTF>>() |
|
|
|
private val queryTTFMap = hashMapOf<String, Pair<Long, QueryTTF>>() |
|
|
|
private val memoryLruCache = object : LruCache<String, Cache>(100) {} |
|
|
|
private val memoryLruCache = object : LruCache<String, String>(100) {} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* saveTime 单位为秒 |
|
|
|
* saveTime 单位为秒 |
|
|
@ -25,15 +25,23 @@ object CacheManager { |
|
|
|
is ByteArray -> ACache.get(appCtx).put(key, value, saveTime) |
|
|
|
is ByteArray -> ACache.get(appCtx).put(key, value, saveTime) |
|
|
|
else -> { |
|
|
|
else -> { |
|
|
|
val cache = Cache(key, value.toString(), deadline) |
|
|
|
val cache = Cache(key, value.toString(), deadline) |
|
|
|
memoryLruCache.put(key, cache) |
|
|
|
putMemory(key, value.toString()) |
|
|
|
appDb.cacheDao.insert(cache) |
|
|
|
appDb.cacheDao.insert(cache) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun putMemory(key: String, value: Any) { |
|
|
|
fun putMemory(key: String, value: String) { |
|
|
|
val cache = Cache(key, value.toString(), 0) |
|
|
|
memoryLruCache.put(key, value) |
|
|
|
memoryLruCache.put(key, cache) |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//从内存中获取数据 使用lruCache |
|
|
|
|
|
|
|
fun getFromMemory(key: String): String? { |
|
|
|
|
|
|
|
return memoryLruCache.get(key) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun deleteMemory(key: String) { |
|
|
|
|
|
|
|
memoryLruCache.remove(key) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun get(key: String): String? { |
|
|
|
fun get(key: String): String? { |
|
|
@ -42,24 +50,12 @@ object CacheManager { |
|
|
|
} |
|
|
|
} |
|
|
|
val cache = appDb.cacheDao.get(key) |
|
|
|
val cache = appDb.cacheDao.get(key) |
|
|
|
if (cache != null && (cache.deadline == 0L || cache.deadline > System.currentTimeMillis())) { |
|
|
|
if (cache != null && (cache.deadline == 0L || cache.deadline > System.currentTimeMillis())) { |
|
|
|
memoryLruCache.put(key, cache) |
|
|
|
putMemory(key, cache.value ?: "") |
|
|
|
return cache.value |
|
|
|
return cache.value |
|
|
|
} |
|
|
|
} |
|
|
|
return null |
|
|
|
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 |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun getInt(key: String): Int? { |
|
|
|
fun getInt(key: String): Int? { |
|
|
|
return get(key)?.toIntOrNull() |
|
|
|
return get(key)?.toIntOrNull() |
|
|
|
} |
|
|
|
} |
|
|
@ -100,7 +96,7 @@ object CacheManager { |
|
|
|
|
|
|
|
|
|
|
|
fun delete(key: String) { |
|
|
|
fun delete(key: String) { |
|
|
|
appDb.cacheDao.delete(key) |
|
|
|
appDb.cacheDao.delete(key) |
|
|
|
memoryLruCache.remove(key) |
|
|
|
deleteMemory(key) |
|
|
|
ACache.get(appCtx).remove(key) |
|
|
|
ACache.get(appCtx).remove(key) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |