Horis 1 anno fa
parent
commit
74fa8a2e5f

+ 1 - 4
app/src/main/java/io/legado/app/api/controller/BookSourceController.kt

@@ -3,7 +3,6 @@ package io.legado.app.api.controller
 
 import android.text.TextUtils
 import io.legado.app.api.ReturnData
-import io.legado.app.constant.SourceType
 import io.legado.app.data.appDb
 import io.legado.app.data.entities.BookSource
 import io.legado.app.help.source.SourceHelp
@@ -71,9 +70,7 @@ object BookSourceController {
     fun deleteSources(postData: String?): ReturnData {
         kotlin.runCatching {
             GSON.fromJsonArray<BookSource>(postData).getOrThrow().let {
-                it.forEach { source ->
-                    SourceHelp.deleteBookSource(source.bookSourceUrl)
-                }
+                SourceHelp.deleteBookSources(it)
             }
         }.onFailure {
             return ReturnData().setErrorMsg(it.localizedMessage ?: "数据格式错误")

+ 2 - 3
app/src/main/java/io/legado/app/api/controller/RssSourceController.kt

@@ -5,6 +5,7 @@ import android.text.TextUtils
 import io.legado.app.api.ReturnData
 import io.legado.app.data.appDb
 import io.legado.app.data.entities.RssSource
+import io.legado.app.help.source.SourceHelp
 import io.legado.app.utils.GSON
 import io.legado.app.utils.fromJsonArray
 import io.legado.app.utils.fromJsonObject
@@ -69,9 +70,7 @@ object RssSourceController {
         GSON.fromJsonArray<RssSource>(postData).onFailure {
             return ReturnData().setErrorMsg("格式不对")
         }.onSuccess {
-            it.forEach { source ->
-                appDb.rssSourceDao.delete(source)
-            }
+            SourceHelp.deleteRssSources(it)
         }
         return ReturnData().setData("已执行"/*okSources*/)
     }

+ 37 - 23
app/src/main/java/io/legado/app/help/CacheManager.kt

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

+ 3 - 3
app/src/main/java/io/legado/app/help/JsExtensions.kt

@@ -819,7 +819,7 @@ interface JsExtensions : JsEncodeUtils {
                     if (useCache) {
                         key = MessageDigest.getInstance("SHA-256").digest(data.toByteArray())
                             .toHexString()
-                        qTTF = CacheManager.getQueryTTF(key)
+                        qTTF = AppCacheManager.getQueryTTF(key)
                         if (qTTF != null) return qTTF
                     }
                     val font: ByteArray? = when {
@@ -838,7 +838,7 @@ interface JsExtensions : JsEncodeUtils {
                 is ByteArray -> {
                     if (useCache) {
                         key = MessageDigest.getInstance("SHA-256").digest(data).toHexString()
-                        qTTF = CacheManager.getQueryTTF(key)
+                        qTTF = AppCacheManager.getQueryTTF(key)
                         if (qTTF != null) return qTTF
                     }
                     qTTF = QueryTTF(data)
@@ -846,7 +846,7 @@ interface JsExtensions : JsEncodeUtils {
 
                 else -> return null
             }
-            if (key != null) CacheManager.put(key, qTTF)
+            if (key != null) AppCacheManager.put(key, qTTF)
             return qTTF
         } catch (e: Exception) {
             AppLog.put("[queryTTF] 获取字体处理类出错", e)

+ 36 - 2
app/src/main/java/io/legado/app/help/source/SourceHelp.kt

@@ -4,7 +4,9 @@ import io.legado.app.constant.SourceType
 import io.legado.app.data.appDb
 import io.legado.app.data.entities.BaseSource
 import io.legado.app.data.entities.BookSource
+import io.legado.app.data.entities.BookSourcePart
 import io.legado.app.data.entities.RssSource
+import io.legado.app.help.AppCacheManager
 import io.legado.app.help.config.SourceConfig
 import io.legado.app.help.coroutine.Coroutine
 import io.legado.app.model.AudioPlay
@@ -58,17 +60,49 @@ object SourceHelp {
         }
     }
 
-    fun deleteBookSource(key: String) {
+    fun deleteBookSourceParts(sources: List<BookSourcePart>) {
+        sources.forEach {
+            deleteBookSourceInternal(it.bookSourceUrl)
+        }
+        AppCacheManager.clearSourceVariables()
+    }
+
+    fun deleteBookSources(sources: List<BookSource>) {
+        sources.forEach {
+            deleteBookSourceInternal(it.bookSourceUrl)
+        }
+        AppCacheManager.clearSourceVariables()
+    }
+
+    private fun deleteBookSourceInternal(key: String) {
         appDb.bookSourceDao.delete(key)
         appDb.cacheDao.deleteSourceVariables(key)
         SourceConfig.removeSource(key)
     }
 
-    fun deleteRssSource(key: String) {
+    fun deleteBookSource(key: String) {
+        deleteBookSourceInternal(key)
+        AppCacheManager.clearSourceVariables()
+    }
+
+    fun deleteRssSources(sources: List<RssSource>) {
+        sources.forEach {
+            deleteRssSourceInternal(it.sourceUrl)
+        }
+        AppCacheManager.clearSourceVariables()
+    }
+
+    private fun deleteRssSourceInternal(key: String) {
         appDb.rssSourceDao.delete(key)
+        appDb.rssArticleDao.delete(key)
         appDb.cacheDao.deleteSourceVariables(key)
     }
 
+    fun deleteRssSource(key: String) {
+        deleteRssSourceInternal(key)
+        AppCacheManager.clearSourceVariables()
+    }
+
     fun enableSource(key: String, @SourceType.Type type: Int, enable: Boolean) {
         when (type) {
             SourceType.book -> appDb.bookSourceDao.enable(key, enable)

+ 1 - 5
app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt

@@ -4,12 +4,10 @@ import android.app.Application
 import android.text.TextUtils
 import io.legado.app.R
 import io.legado.app.base.BaseViewModel
-import io.legado.app.constant.SourceType
 import io.legado.app.data.appDb
 import io.legado.app.data.entities.BookSource
 import io.legado.app.data.entities.BookSourcePart
 import io.legado.app.data.entities.toBookSource
-import io.legado.app.help.config.SourceConfig
 import io.legado.app.help.source.SourceHelp
 import io.legado.app.utils.FileUtils
 import io.legado.app.utils.GSON
@@ -52,9 +50,7 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application)
 
     fun del(sources: List<BookSourcePart>) {
         execute {
-            sources.forEach {
-                SourceHelp.deleteBookSource(it.bookSourceUrl)
-            }
+            SourceHelp.deleteBookSourceParts(sources)
         }
     }
 

+ 3 - 5
app/src/main/java/io/legado/app/ui/main/rss/RssViewModel.kt

@@ -1,10 +1,11 @@
 package io.legado.app.ui.main.rss
 
 import android.app.Application
+import com.script.rhino.runScriptWithContext
 import io.legado.app.base.BaseViewModel
 import io.legado.app.data.appDb
 import io.legado.app.data.entities.RssSource
-import com.script.rhino.runScriptWithContext
+import io.legado.app.help.source.SourceHelp
 import io.legado.app.utils.toastOnUi
 
 class RssViewModel(application: Application) : BaseViewModel(application) {
@@ -33,10 +34,7 @@ class RssViewModel(application: Application) : BaseViewModel(application) {
 
     fun del(vararg rssSource: RssSource) {
         execute {
-            appDb.rssSourceDao.delete(*rssSource)
-            rssSource.forEach {
-                appDb.rssArticleDao.delete(it.sourceUrl)
-            }
+            SourceHelp.deleteRssSources(rssSource.toList())
         }
     }
 

+ 7 - 5
app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt

@@ -6,7 +6,12 @@ import io.legado.app.base.BaseViewModel
 import io.legado.app.data.appDb
 import io.legado.app.data.entities.RssSource
 import io.legado.app.help.DefaultData
-import io.legado.app.utils.*
+import io.legado.app.help.source.SourceHelp
+import io.legado.app.utils.FileUtils
+import io.legado.app.utils.GSON
+import io.legado.app.utils.splitNotBlank
+import io.legado.app.utils.stackTraceStr
+import io.legado.app.utils.toastOnUi
 import java.io.File
 
 /**
@@ -39,10 +44,7 @@ class RssSourceViewModel(application: Application) : BaseViewModel(application)
 
     fun del(vararg rssSource: RssSource) {
         execute {
-            appDb.rssSourceDao.delete(*rssSource)
-            rssSource.forEach {
-                appDb.rssArticleDao.delete(it.sourceUrl)
-            }
+            SourceHelp.deleteRssSources(rssSource.toList())
         }
     }