Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
gedoor committed Jan 10, 2023
1 parent e5730f8 commit 1867487
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/src/main/assets/updateLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
**2023/01/10**

* 其他一些有分组的地方也支持独立排序
* 优化搜索,优化缓存,尝试解决部分书源占用内存过大导致OOM
* 修复中文AndroidZipFile不能读取非ASCII字符文件名 by ag2s20150909
* 其它一些优化

Expand Down
13 changes: 12 additions & 1 deletion app/src/main/java/io/legado/app/help/CacheManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,23 @@ 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 io.legado.app.utils.memorySize

@Suppress("unused")
object CacheManager {

private val queryTTFMap = hashMapOf<String, Pair<Long, QueryTTF>>()
private val memoryLruCache = object : LruCache<String, String>(100) {}

/**
* 最多只缓存50M的数据,防止OOM
*/
private val memoryLruCache = object : LruCache<String, String>(1024 * 1024 * 50) {

override fun sizeOf(key: String, value: String): Int {
return value.memorySize()
}

}

/**
* saveTime 单位为秒
Expand Down
9 changes: 8 additions & 1 deletion app/src/main/java/io/legado/app/utils/StringExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import android.icu.text.Collator
import android.icu.util.ULocale
import android.net.Uri
import android.text.Editable
import cn.hutool.core.lang.Validator
import io.legado.app.constant.AppPattern.dataUriRegex
import java.io.File
import java.util.*
Expand Down Expand Up @@ -90,6 +89,14 @@ fun String.cnCompare(other: String): Int {
}
}

/**
* 字符串所占内存大小
*/
fun String?.memorySize(): Int {
this ?: return 0
return 40 + 2 * length
}

/**
* 将字符串拆分为单个字符,包含emoji
*/
Expand Down

0 comments on commit 1867487

Please sign in to comment.