From 6abf5002eb3df105e96da0020fcb602bdb04a9ba Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 5 May 2022 08:51:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/legado/app/data/dao/CacheDao.kt | 3 +++ .../main/java/io/legado/app/help/CacheManager.kt | 16 ++++++++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/dao/CacheDao.kt b/app/src/main/java/io/legado/app/data/dao/CacheDao.kt index 863199b75..7ebd9bbd9 100644 --- a/app/src/main/java/io/legado/app/data/dao/CacheDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/CacheDao.kt @@ -9,6 +9,9 @@ import io.legado.app.data.entities.Cache @Dao interface CacheDao { + @Query("select * from caches where `key` = :key") + fun get(key: String): Cache? + @Query("select value from caches where `key` = :key and (deadline = 0 or deadline > :now)") fun get(key: String, now: Long): String? diff --git a/app/src/main/java/io/legado/app/help/CacheManager.kt b/app/src/main/java/io/legado/app/help/CacheManager.kt index 156b16998..a79bb2720 100644 --- a/app/src/main/java/io/legado/app/help/CacheManager.kt +++ b/app/src/main/java/io/legado/app/help/CacheManager.kt @@ -36,15 +36,23 @@ object CacheManager { } fun get(key: String): String? { - return getFromMemory(key) ?: appDb.cacheDao.get(key, System.currentTimeMillis()) + getFromMemory(key)?.let { + return it + } + val cache = appDb.cacheDao.get(key) + if (cache != null && (cache.deadline == 0L || cache.deadline > System.currentTimeMillis())) { + memoryLruCache.put(key, cache) + return cache.value + } + return null } - //从内存中获取数据 使用lrucache 支持过期功能 + //从内存中获取数据 使用lruCache 支持过期功能 private fun getFromMemory(key: String): String? { val cache = memoryLruCache.get(key) ?: return null - val deadline = cache!!.deadline + val deadline = cache.deadline return if (deadline == 0L || deadline > System.currentTimeMillis()) { - cache!!.value + cache.value } else { memoryLruCache.remove(key) null