|
|
@@ -8,23 +8,48 @@ import io.legado.app.model.analyzeRule.QueryTTF
|
|
|
import io.legado.app.utils.ACache
|
|
|
import io.legado.app.utils.memorySize
|
|
|
|
|
|
-@Keep
|
|
|
-@Suppress("unused")
|
|
|
-object CacheManager {
|
|
|
+private val queryTTFMap = LruCache<String, QueryTTF>(4)
|
|
|
|
|
|
- private val queryTTFMap = hashMapOf<String, Pair<Long, QueryTTF>>()
|
|
|
+/**
|
|
|
+ * 最多只缓存50M的数据,防止OOM
|
|
|
+ */
|
|
|
+private val memoryLruCache = object : LruCache<String, Any>(1024 * 1024 * 50) {
|
|
|
|
|
|
- /**
|
|
|
- * 最多只缓存50M的数据,防止OOM
|
|
|
- */
|
|
|
- private val memoryLruCache = object : LruCache<String, Any>(1024 * 1024 * 50) {
|
|
|
+ override fun sizeOf(key: String, value: Any): Int {
|
|
|
+ return value.toString().memorySize()
|
|
|
+ }
|
|
|
|
|
|
- override fun sizeOf(key: String, value: Any): Int {
|
|
|
- return value.toString().memorySize()
|
|
|
- }
|
|
|
+}
|
|
|
+
|
|
|
+object AppCacheManager {
|
|
|
+
|
|
|
+ fun put(key: String, queryTTF: QueryTTF) {
|
|
|
+ queryTTFMap.put(key, queryTTF)
|
|
|
+ }
|
|
|
+
|
|
|
+ fun getQueryTTF(key: String): QueryTTF? {
|
|
|
+ return queryTTFMap[key]
|
|
|
+ }
|
|
|
|
|
|
+ fun clearSourceVariables() {
|
|
|
+ memoryLruCache.snapshot().keys.forEach {
|
|
|
+ if (it.startsWith("v_")
|
|
|
+ || it.startsWith("userInfo_")
|
|
|
+ || it.startsWith("loginHeader_")
|
|
|
+ || it.startsWith("sourceVariable_")
|
|
|
+ ) {
|
|
|
+ memoryLruCache.remove(it)
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+@Keep
|
|
|
+@Suppress("unused")
|
|
|
+object CacheManager {
|
|
|
+
|
|
|
/**
|
|
|
* saveTime 单位为秒
|
|
|
*/
|
|
|
@@ -33,7 +58,6 @@ object CacheManager {
|
|
|
val deadline =
|
|
|
if (saveTime == 0) 0 else System.currentTimeMillis() + saveTime * 1000
|
|
|
when (value) {
|
|
|
- is QueryTTF -> queryTTFMap[key] = Pair(deadline, value)
|
|
|
is ByteArray -> ACache.get().put(key, value, saveTime)
|
|
|
else -> {
|
|
|
val cache = Cache(key, value.toString(), deadline)
|
|
|
@@ -49,7 +73,7 @@ object CacheManager {
|
|
|
|
|
|
//从内存中获取数据 使用lruCache
|
|
|
fun getFromMemory(key: String): Any? {
|
|
|
- return memoryLruCache.get(key)
|
|
|
+ return memoryLruCache[key]
|
|
|
}
|
|
|
|
|
|
fun deleteMemory(key: String) {
|
|
|
@@ -88,16 +112,6 @@ object CacheManager {
|
|
|
return ACache.get().getAsBinary(key)
|
|
|
}
|
|
|
|
|
|
- fun getQueryTTF(key: String): QueryTTF? {
|
|
|
- val cache = queryTTFMap[key] ?: return null
|
|
|
- if (cache.first == 0L || cache.first > System.currentTimeMillis()) {
|
|
|
- return cache.second
|
|
|
- } else {
|
|
|
- queryTTFMap.remove(key)
|
|
|
- }
|
|
|
- return null
|
|
|
- }
|
|
|
-
|
|
|
fun putFile(key: String, value: String, saveTime: Int = 0) {
|
|
|
ACache.get().put(key, value, saveTime)
|
|
|
}
|