js添加cache变量,可以用来存储token之类的变量,可以设置时效

pull/487/head^2
gedoor 4 years ago
parent 04149d2bf7
commit dc58cc7672
  1. 15
      app/src/main/java/io/legado/app/data/AppDatabase.kt
  2. 24
      app/src/main/java/io/legado/app/data/dao/CacheDao.kt
  3. 3
      app/src/main/java/io/legado/app/data/entities/BookSource.kt
  4. 13
      app/src/main/java/io/legado/app/data/entities/Cache.kt
  5. 2
      app/src/main/java/io/legado/app/data/entities/RssSource.kt
  6. 34
      app/src/main/java/io/legado/app/help/CacheManager.kt
  7. 2
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt
  8. 3
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.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`)")
}
}
}
}

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

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

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

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

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

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

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

Loading…
Cancel
Save