-
Notifications
You must be signed in to change notification settings - Fork 15
Kotlin
Tony Shen edited this page Jul 23, 2020
·
16 revisions
由于 RxCache 是单例,我们可以通过by lazy
对 RxCache 进行配置并初始化。之后就可以在整个程序中使用 RxCache。
val rxCache:RxCache by lazy {
RxCache.config { // 初始化 RxCache, 并配置二级缓存
RxCache.Builder().memory {
FIFOMemoryImpl()
}.persistence {
val cacheDirectory = File(diskCache) // rxCache 持久层存放地址
if (!cacheDirectory.exists()) {
cacheDirectory.mkdir()
}
OkioImpl(cacheDirectory)
}
}
RxCache.getRxCache()
}
借助 Kotlin 的特性,可以规避范型擦除
RxCache.config(RxCache.Builder())
val rxCache = RxCache.getRxCache()
val list = ArrayList<User>()
val u1 = User()
u1.name = "tony1"
u1.password = "123456"
list.add(u1)
val u2 = User()
u2.name = "tony2"
u2.password = "123456"
list.add(u2)
rxCache.save<List<User>>("test", list)
val observable = rxCache.load2Observable<List<User>>("test")
observable.subscribe {
val recordDataList = it.data
if (Preconditions.isNotBlank(recordDataList)) {
val user = recordDataList[0]
println(user.name)
println(user.password)
}
}
支持使用 Kotlin 的风格保存缓存
需要额外使用 rxcache-extension 包
RxCache.config(RxCache.Builder())
val rxCache = RxCache.getRxCache()
rxCache.saveFunc("test1") {
val u = User()
u.name = "tony"
u.password = "123456"
u
}
rxCache.get<User>("test1")?.let {
val u = it.data
println(u.name)
println(u.password)
}
rxCache.saveFunc("test2",5, TimeUnit.SECONDS) {
val u = User()
u.name = "tony"
u.password = "123456"
u
}
try {
Thread.sleep(2300)
} catch (e: InterruptedException) {
e.printStackTrace()
}
println("ttl=" + rxCache.ttl("test2", User::class.java))
}
需要额外使用 rxcache-extension-coroutines 包
RxCache 支持使用 getDeferred() 返回 Deferred<T> 对象,以及使用 getFlow() 返回 Flow<T> 对象。
fun main() = runBlocking {
RxCache.config(RxCache.Builder())
val rxCache = RxCache.getRxCache()
rxCache.saveFunc("test1") {
val u = User()
u.name = "tony"
u.password = "123456"
u
}
rxCache.saveFunc("test2") {
"hello world"
}
rxCache.saveFunc("test3") {
0
}
val deferred1 = rxCache.getDeferred<User>("test1",User::class.java)
val deferred2 = rxCache.getDeferred<String>("test2",String::class.java)
println(deferred1.await().name + "," + deferred2.await())
rxCache.getFlow<Int>("test3",String::class.java)
.map {
var sum = it
for (i in 1..100) {
sum += i
}
sum
}
.flowOn(IO)
.collect{
println(it)
}
}
RxCache 支持使用 getResult() 返回 Result<T> 对象,Result 可以点击查看
需要额外使用 rxcache-extension-result 包
RxCache.config(RxCache.Builder())
val rxCache = RxCache.getRxCache()
rxCache.saveFunc("test1") {
val u = User()
u.name = "tony"
u.password = "123456"
u
}
rxCache.getResult<User>("test1").get()?.takeIf { it is User }?.let {
it as User
print(it.name)
println(it.password)
}
rxCache.saveFunc("test2") {
"hello world"
}
rxCache.getResult<String>("test2").get()?.takeIf { it is String }?.let {
println(it)
}
val result = rxCache.getResult<String>("test2").map {
it + ", hello kotlin"
}.get()
println(result)
- General
- Memory
- Persistence
-
Disk
-
Serialization
- Gson
- Fastjson
- Moshi
- Kryo
- Hessian
- FST
- Protobuf
-
Encryption
- AES 128
- DES
-
Serialization
-
Disk
- Cache Statistics
- Spring