diff --git a/app/src/main/java/io/legado/app/data/AppDatabase.kt b/app/src/main/java/io/legado/app/data/AppDatabase.kt index 706d08094..7888cb7f9 100644 --- a/app/src/main/java/io/legado/app/data/AppDatabase.kt +++ b/app/src/main/java/io/legado/app/data/AppDatabase.kt @@ -17,8 +17,8 @@ import java.util.* entities = [Book::class, BookGroup::class, BookSource::class, BookChapter::class, ReplaceRule::class, SearchBook::class, SearchKeyword::class, Cookie::class, RssSource::class, Bookmark::class, RssArticle::class, RssReadRecord::class, - RssStar::class, TxtTocRule::class, ReadRecord::class, HttpTTS::class], - version = 23, + RssStar::class, TxtTocRule::class, ReadRecord::class, HttpTTS::class, Cache::class], + version = 24, exportSchema = true ) abstract class AppDatabase : RoomDatabase() { @@ -38,6 +38,7 @@ abstract class AppDatabase : RoomDatabase() { abstract fun txtTocRule(): TxtTocRuleDao abstract fun readRecordDao(): ReadRecordDao abstract fun httpTTSDao(): HttpTTSDao + abstract fun cacheDao(): CacheDao companion object { @@ -58,7 +59,8 @@ abstract class AppDatabase : RoomDatabase() { migration_19_20, migration_20_21, migration_21_22, - migration_22_23 + migration_22_23, + migration_23_24 ) .allowMainThreadQueries() .addCallback(dbCallback) @@ -201,6 +203,13 @@ abstract class AppDatabase : RoomDatabase() { database.execSQL("ALTER TABLE chapters ADD baseUrl TEXT NOT NULL DEFAULT ''") } } + + private val migration_23_24 = object : Migration(23, 24) { + override fun migrate(database: SupportSQLiteDatabase) { + database.execSQL("CREATE TABLE IF NOT EXISTS `caches` (`key` TEXT NOT NULL, `value` TEXT, `deadline` INTEGER NOT NULL, PRIMARY KEY(`key`))") + database.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS `index_caches_key` ON `caches` (`key`)") + } + } } } \ No newline at end of file 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 new file mode 100644 index 000000000..863199b75 --- /dev/null +++ b/app/src/main/java/io/legado/app/data/dao/CacheDao.kt @@ -0,0 +1,24 @@ +package io.legado.app.data.dao + +import androidx.room.Dao +import androidx.room.Insert +import androidx.room.OnConflictStrategy +import androidx.room.Query +import io.legado.app.data.entities.Cache + +@Dao +interface CacheDao { + + @Query("select value from caches where `key` = :key and (deadline = 0 or deadline > :now)") + fun get(key: String, now: Long): String? + + @Insert(onConflict = OnConflictStrategy.REPLACE) + fun insert(vararg cache: Cache) + + @Query("delete from caches where `key` = :key") + fun delete(key: String) + + @Query("delete from caches where deadline > 0 and deadline < :now") + fun clearDeadline(now: Long) + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/data/entities/BookSource.kt b/app/src/main/java/io/legado/app/data/entities/BookSource.kt index 8be8b68cb..5d82ec2f5 100644 --- a/app/src/main/java/io/legado/app/data/entities/BookSource.kt +++ b/app/src/main/java/io/legado/app/data/entities/BookSource.kt @@ -8,6 +8,7 @@ import io.legado.app.constant.AppConst import io.legado.app.constant.BookType import io.legado.app.data.entities.rule.* import io.legado.app.help.AppConfig +import io.legado.app.help.CacheManager import io.legado.app.help.JsExtensions import io.legado.app.help.http.CookieStore import io.legado.app.utils.ACache @@ -111,6 +112,7 @@ data class BookSource( bindings["baseUrl"] = bookSourceUrl bindings["java"] = this bindings["cookie"] = CookieStore + bindings["cache"] = CacheManager a = AppConst.SCRIPT_ENGINE.eval( it.substring(4, it.lastIndexOf("<")), bindings @@ -139,6 +141,7 @@ data class BookSource( val bindings = SimpleBindings() bindings["java"] = this bindings["cookie"] = CookieStore + bindings["cache"] = CacheManager return AppConst.SCRIPT_ENGINE.eval(jsStr, bindings) } diff --git a/app/src/main/java/io/legado/app/data/entities/Cache.kt b/app/src/main/java/io/legado/app/data/entities/Cache.kt new file mode 100644 index 000000000..f9687dea7 --- /dev/null +++ b/app/src/main/java/io/legado/app/data/entities/Cache.kt @@ -0,0 +1,13 @@ +package io.legado.app.data.entities + +import androidx.room.Entity +import androidx.room.Index +import androidx.room.PrimaryKey + +@Entity(tableName = "caches", indices = [(Index(value = ["key"], unique = true))]) +data class Cache( + @PrimaryKey + val key: String = "", + var value: String? = null, + var deadline: Long = 0L +) \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/data/entities/RssSource.kt b/app/src/main/java/io/legado/app/data/entities/RssSource.kt index 975e14b3b..b61d8baa3 100644 --- a/app/src/main/java/io/legado/app/data/entities/RssSource.kt +++ b/app/src/main/java/io/legado/app/data/entities/RssSource.kt @@ -6,6 +6,7 @@ import androidx.room.Index import androidx.room.PrimaryKey import io.legado.app.constant.AppConst import io.legado.app.help.AppConfig +import io.legado.app.help.CacheManager import io.legado.app.help.JsExtensions import io.legado.app.help.http.CookieStore import io.legado.app.utils.GSON @@ -78,6 +79,7 @@ data class RssSource( val bindings = SimpleBindings() bindings["java"] = this bindings["cookie"] = CookieStore + bindings["cache"] = CacheManager return AppConst.SCRIPT_ENGINE.eval(jsStr, bindings) } diff --git a/app/src/main/java/io/legado/app/help/CacheManager.kt b/app/src/main/java/io/legado/app/help/CacheManager.kt new file mode 100644 index 000000000..558b71f0c --- /dev/null +++ b/app/src/main/java/io/legado/app/help/CacheManager.kt @@ -0,0 +1,34 @@ +package io.legado.app.help + +import io.legado.app.App +import io.legado.app.data.entities.Cache + +object CacheManager { + + fun put(key: String, value: Any, saveTime: Int = 0) { + val deadline = if (saveTime == 0) 0 else System.currentTimeMillis() + saveTime * 1000 + val cache = Cache(key, value.toString(), deadline) + App.db.cacheDao().insert(cache) + } + + fun get(key: String): String? { + return App.db.cacheDao().get(key, System.currentTimeMillis()) + } + + fun getInt(key: String): Int? { + return get(key)?.toIntOrNull() + } + + fun getLong(key: String): Long? { + return get(key)?.toLongOrNull() + } + + fun getDouble(key: String): Double? { + return get(key)?.toDoubleOrNull() + } + + fun getFloat(key: String): Float? { + return get(key)?.toFloatOrNull() + } + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt index 17a56b0be..50fceacc4 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt @@ -6,6 +6,7 @@ import io.legado.app.constant.AppConst.SCRIPT_ENGINE import io.legado.app.constant.AppPattern.JS_PATTERN import io.legado.app.data.entities.BaseBook import io.legado.app.data.entities.BookChapter +import io.legado.app.help.CacheManager import io.legado.app.help.JsExtensions import io.legado.app.help.http.CookieStore import io.legado.app.utils.* @@ -651,6 +652,7 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions { val bindings = SimpleBindings() bindings["java"] = this bindings["cookie"] = CookieStore + bindings["cache"] = CacheManager bindings["book"] = book bindings["result"] = result bindings["baseUrl"] = baseUrl diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt index 468b78058..338b8386e 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt @@ -12,6 +12,7 @@ import io.legado.app.constant.AppPattern.JS_PATTERN import io.legado.app.data.entities.BaseBook import io.legado.app.data.entities.BookChapter import io.legado.app.help.AppConfig +import io.legado.app.help.CacheManager import io.legado.app.help.JsExtensions import io.legado.app.help.http.* import io.legado.app.help.http.api.HttpGetApi @@ -139,6 +140,7 @@ class AnalyzeUrl( val bindings = SimpleBindings() bindings["java"] = this bindings["cookie"] = CookieStore + bindings["cache"] = CacheManager bindings["baseUrl"] = baseUrl bindings["page"] = page bindings["key"] = key @@ -261,6 +263,7 @@ class AnalyzeUrl( val bindings = SimpleBindings() bindings["java"] = this bindings["cookie"] = CookieStore + bindings["cache"] = CacheManager bindings["page"] = page bindings["key"] = key bindings["speakText"] = speakText