parent
04149d2bf7
commit
dc58cc7672
@ -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) |
||||
|
||||
} |
@ -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 |
||||
) |
@ -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() |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue