This commit is contained in:
Horis
2025-05-17 17:08:58 +08:00
parent e4e97a2ce2
commit 74fa8a2e5f
8 changed files with 92 additions and 52 deletions
@@ -3,7 +3,6 @@ package io.legado.app.api.controller
import android.text.TextUtils import android.text.TextUtils
import io.legado.app.api.ReturnData import io.legado.app.api.ReturnData
import io.legado.app.constant.SourceType
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.help.source.SourceHelp import io.legado.app.help.source.SourceHelp
@@ -71,9 +70,7 @@ object BookSourceController {
fun deleteSources(postData: String?): ReturnData { fun deleteSources(postData: String?): ReturnData {
kotlin.runCatching { kotlin.runCatching {
GSON.fromJsonArray<BookSource>(postData).getOrThrow().let { GSON.fromJsonArray<BookSource>(postData).getOrThrow().let {
it.forEach { source -> SourceHelp.deleteBookSources(it)
SourceHelp.deleteBookSource(source.bookSourceUrl)
}
} }
}.onFailure { }.onFailure {
return ReturnData().setErrorMsg(it.localizedMessage ?: "数据格式错误") return ReturnData().setErrorMsg(it.localizedMessage ?: "数据格式错误")
@@ -5,6 +5,7 @@ import android.text.TextUtils
import io.legado.app.api.ReturnData import io.legado.app.api.ReturnData
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.data.entities.RssSource 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.GSON
import io.legado.app.utils.fromJsonArray import io.legado.app.utils.fromJsonArray
import io.legado.app.utils.fromJsonObject import io.legado.app.utils.fromJsonObject
@@ -69,9 +70,7 @@ object RssSourceController {
GSON.fromJsonArray<RssSource>(postData).onFailure { GSON.fromJsonArray<RssSource>(postData).onFailure {
return ReturnData().setErrorMsg("格式不对") return ReturnData().setErrorMsg("格式不对")
}.onSuccess { }.onSuccess {
it.forEach { source -> SourceHelp.deleteRssSources(it)
appDb.rssSourceDao.delete(source)
}
} }
return ReturnData().setData("已执行"/*okSources*/) return ReturnData().setData("已执行"/*okSources*/)
} }
@@ -8,23 +8,48 @@ import io.legado.app.model.analyzeRule.QueryTTF
import io.legado.app.utils.ACache import io.legado.app.utils.ACache
import io.legado.app.utils.memorySize import io.legado.app.utils.memorySize
private val queryTTFMap = LruCache<String, QueryTTF>(4)
/**
* 最多只缓存50M的数据,防止OOM
*/
private val memoryLruCache = object : LruCache<String, Any>(1024 * 1024 * 50) {
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 @Keep
@Suppress("unused") @Suppress("unused")
object CacheManager { object CacheManager {
private val queryTTFMap = hashMapOf<String, Pair<Long, QueryTTF>>()
/**
* 最多只缓存50M的数据,防止OOM
*/
private val memoryLruCache = object : LruCache<String, Any>(1024 * 1024 * 50) {
override fun sizeOf(key: String, value: Any): Int {
return value.toString().memorySize()
}
}
/** /**
* saveTime 单位为秒 * saveTime 单位为秒
*/ */
@@ -33,7 +58,6 @@ object CacheManager {
val deadline = val deadline =
if (saveTime == 0) 0 else System.currentTimeMillis() + saveTime * 1000 if (saveTime == 0) 0 else System.currentTimeMillis() + saveTime * 1000
when (value) { when (value) {
is QueryTTF -> queryTTFMap[key] = Pair(deadline, value)
is ByteArray -> ACache.get().put(key, value, saveTime) is ByteArray -> ACache.get().put(key, value, saveTime)
else -> { else -> {
val cache = Cache(key, value.toString(), deadline) val cache = Cache(key, value.toString(), deadline)
@@ -49,7 +73,7 @@ object CacheManager {
//从内存中获取数据 使用lruCache //从内存中获取数据 使用lruCache
fun getFromMemory(key: String): Any? { fun getFromMemory(key: String): Any? {
return memoryLruCache.get(key) return memoryLruCache[key]
} }
fun deleteMemory(key: String) { fun deleteMemory(key: String) {
@@ -88,16 +112,6 @@ object CacheManager {
return ACache.get().getAsBinary(key) 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) { fun putFile(key: String, value: String, saveTime: Int = 0) {
ACache.get().put(key, value, saveTime) ACache.get().put(key, value, saveTime)
} }
@@ -819,7 +819,7 @@ interface JsExtensions : JsEncodeUtils {
if (useCache) { if (useCache) {
key = MessageDigest.getInstance("SHA-256").digest(data.toByteArray()) key = MessageDigest.getInstance("SHA-256").digest(data.toByteArray())
.toHexString() .toHexString()
qTTF = CacheManager.getQueryTTF(key) qTTF = AppCacheManager.getQueryTTF(key)
if (qTTF != null) return qTTF if (qTTF != null) return qTTF
} }
val font: ByteArray? = when { val font: ByteArray? = when {
@@ -838,7 +838,7 @@ interface JsExtensions : JsEncodeUtils {
is ByteArray -> { is ByteArray -> {
if (useCache) { if (useCache) {
key = MessageDigest.getInstance("SHA-256").digest(data).toHexString() key = MessageDigest.getInstance("SHA-256").digest(data).toHexString()
qTTF = CacheManager.getQueryTTF(key) qTTF = AppCacheManager.getQueryTTF(key)
if (qTTF != null) return qTTF if (qTTF != null) return qTTF
} }
qTTF = QueryTTF(data) qTTF = QueryTTF(data)
@@ -846,7 +846,7 @@ interface JsExtensions : JsEncodeUtils {
else -> return null else -> return null
} }
if (key != null) CacheManager.put(key, qTTF) if (key != null) AppCacheManager.put(key, qTTF)
return qTTF return qTTF
} catch (e: Exception) { } catch (e: Exception) {
AppLog.put("[queryTTF] 获取字体处理类出错", e) AppLog.put("[queryTTF] 获取字体处理类出错", e)
@@ -4,7 +4,9 @@ import io.legado.app.constant.SourceType
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.data.entities.BaseSource import io.legado.app.data.entities.BaseSource
import io.legado.app.data.entities.BookSource 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.data.entities.RssSource
import io.legado.app.help.AppCacheManager
import io.legado.app.help.config.SourceConfig import io.legado.app.help.config.SourceConfig
import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.coroutine.Coroutine
import io.legado.app.model.AudioPlay 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.bookSourceDao.delete(key)
appDb.cacheDao.deleteSourceVariables(key) appDb.cacheDao.deleteSourceVariables(key)
SourceConfig.removeSource(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.rssSourceDao.delete(key)
appDb.rssArticleDao.delete(key)
appDb.cacheDao.deleteSourceVariables(key) appDb.cacheDao.deleteSourceVariables(key)
} }
fun deleteRssSource(key: String) {
deleteRssSourceInternal(key)
AppCacheManager.clearSourceVariables()
}
fun enableSource(key: String, @SourceType.Type type: Int, enable: Boolean) { fun enableSource(key: String, @SourceType.Type type: Int, enable: Boolean) {
when (type) { when (type) {
SourceType.book -> appDb.bookSourceDao.enable(key, enable) SourceType.book -> appDb.bookSourceDao.enable(key, enable)
@@ -4,12 +4,10 @@ import android.app.Application
import android.text.TextUtils import android.text.TextUtils
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.constant.SourceType
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.data.entities.BookSourcePart import io.legado.app.data.entities.BookSourcePart
import io.legado.app.data.entities.toBookSource 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.help.source.SourceHelp
import io.legado.app.utils.FileUtils import io.legado.app.utils.FileUtils
import io.legado.app.utils.GSON import io.legado.app.utils.GSON
@@ -52,9 +50,7 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application)
fun del(sources: List<BookSourcePart>) { fun del(sources: List<BookSourcePart>) {
execute { execute {
sources.forEach { SourceHelp.deleteBookSourceParts(sources)
SourceHelp.deleteBookSource(it.bookSourceUrl)
}
} }
} }
@@ -1,10 +1,11 @@
package io.legado.app.ui.main.rss package io.legado.app.ui.main.rss
import android.app.Application import android.app.Application
import com.script.rhino.runScriptWithContext
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.data.entities.RssSource 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 import io.legado.app.utils.toastOnUi
class RssViewModel(application: Application) : BaseViewModel(application) { class RssViewModel(application: Application) : BaseViewModel(application) {
@@ -33,10 +34,7 @@ class RssViewModel(application: Application) : BaseViewModel(application) {
fun del(vararg rssSource: RssSource) { fun del(vararg rssSource: RssSource) {
execute { execute {
appDb.rssSourceDao.delete(*rssSource) SourceHelp.deleteRssSources(rssSource.toList())
rssSource.forEach {
appDb.rssArticleDao.delete(it.sourceUrl)
}
} }
} }
@@ -6,7 +6,12 @@ import io.legado.app.base.BaseViewModel
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.data.entities.RssSource import io.legado.app.data.entities.RssSource
import io.legado.app.help.DefaultData 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 import java.io.File
/** /**
@@ -39,10 +44,7 @@ class RssSourceViewModel(application: Application) : BaseViewModel(application)
fun del(vararg rssSource: RssSource) { fun del(vararg rssSource: RssSource) {
execute { execute {
appDb.rssSourceDao.delete(*rssSource) SourceHelp.deleteRssSources(rssSource.toList())
rssSource.forEach {
appDb.rssArticleDao.delete(it.sourceUrl)
}
} }
} }