-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2c88f74
commit 1bfb5f3
Showing
4 changed files
with
110 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 2 additions & 86 deletions
88
library/src/main/java/io/coroutines/cache/core/CoroutinesCache.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,5 @@ | ||
package io.coroutines.cache.core | ||
|
||
import android.content.Context | ||
import com.google.gson.Gson | ||
import com.google.gson.reflect.TypeToken | ||
import io.coroutines.cache.dao.Cache | ||
import io.coroutines.cache.dao.RealmDatabase | ||
import kotlinx.coroutines.* | ||
import java.util.* | ||
import kotlin.coroutines.CoroutineContext | ||
import kotlinx.coroutines.Deferred | ||
|
||
open class CoroutinesCache(private var context: Context): CoroutineScope{ | ||
|
||
private val executionJob: Job by lazy { Job() } | ||
|
||
override val coroutineContext: CoroutineContext by lazy { | ||
Dispatchers.Default + executionJob | ||
} | ||
|
||
@PublishedApi internal val database:RealmDatabase by lazy { | ||
RealmDatabase(context).apply { | ||
initDatabase() | ||
} | ||
} | ||
|
||
inline fun <reified T:Any> asyncCache(noinline source: suspend ()->Deferred<T>, key: String, cachePolicy: CachePolicy): Deferred<T> { | ||
return when(cachePolicy){ | ||
is CachePolicy.LifeCache-> cacheLifeResolver( source, key, cachePolicy) | ||
is CachePolicy.EvictProvider -> cacheProviderResolver(source, key, cachePolicy) | ||
} | ||
} | ||
|
||
@PublishedApi internal inline fun <reified T : Any> cacheLifeResolver(noinline source: suspend () -> Deferred<T>, key: String, cachePolicy: CachePolicy.LifeCache): Deferred<T> { | ||
getFromCacheValidateTime<T>(key, cachePolicy)?.let { | ||
return async { it } | ||
} ?: run { | ||
return getFromSource(source, key) | ||
} | ||
} | ||
|
||
@PublishedApi internal inline fun <reified T : Any> cacheProviderResolver(noinline source: suspend () -> Deferred<T>, key: String, cachePolicy: CachePolicy.EvictProvider): Deferred<T> { | ||
return if (cachePolicy.fromSource) { | ||
getFromSource(source, key) | ||
} else { | ||
getFromCache<T>(key)?.let { | ||
return async { it } | ||
} ?: run { | ||
return getFromSource(source, key) | ||
} | ||
} | ||
} | ||
|
||
@PublishedApi internal fun <T> getFromSource(source: suspend ()->Deferred<T>, key: String):Deferred<T>{ | ||
return async { | ||
val result = source().await() | ||
database.getDatabase().apply { | ||
beginTransaction() | ||
copyToRealmOrUpdate(Cache(key, Gson().toJson(result), Calendar.getInstance().time)) | ||
commitTransaction() | ||
close() | ||
} | ||
result | ||
} | ||
} | ||
|
||
@PublishedApi internal inline fun <reified T:Any> getFromCache(key: String): T? { | ||
val resultDb = database.getDatabase().where(Cache::class.java).equalTo("id", key).findFirst()?.data | ||
return if (resultDb != null) { | ||
val listType = object : TypeToken<T>() {}.type | ||
Gson().fromJson(resultDb, listType) as T | ||
} else { | ||
return null | ||
} | ||
} | ||
|
||
@PublishedApi internal inline fun <reified T:Any> getFromCacheValidateTime(key: String, lifeCache: CachePolicy.LifeCache): T? { | ||
val resultDb = database.getDatabase().where(Cache::class.java).equalTo("id", key).findFirst() | ||
return if (resultDb != null) { | ||
if(resultDb.date.isValidCache(lifeCache.duration, lifeCache.timeUnit)){ | ||
val listType = object : TypeToken<T>() {}.type | ||
Gson().fromJson(resultDb.data, listType) as T | ||
}else{ | ||
null | ||
} | ||
} else { | ||
null | ||
} | ||
} | ||
} | ||
interface CoroutinesCache |
89 changes: 89 additions & 0 deletions
89
library/src/main/java/io/coroutines/cache/core/CoroutinesCacheImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package io.coroutines.cache.core | ||
|
||
import android.content.Context | ||
import com.google.gson.Gson | ||
import com.google.gson.reflect.TypeToken | ||
import io.coroutines.cache.dao.Cache | ||
import io.coroutines.cache.dao.RealmDatabase | ||
import kotlinx.coroutines.* | ||
import java.util.* | ||
import kotlin.coroutines.CoroutineContext | ||
|
||
open class CoroutinesCacheImpl(private var context: Context): CoroutinesCache,CoroutineScope{ | ||
|
||
private val executionJob: Job by lazy { Job() } | ||
|
||
override val coroutineContext: CoroutineContext by lazy { | ||
Dispatchers.Default + executionJob | ||
} | ||
|
||
@PublishedApi internal val database:RealmDatabase by lazy { | ||
RealmDatabase(context).apply { | ||
initDatabase() | ||
} | ||
} | ||
|
||
inline fun <reified T:Any> asyncCache(noinline source: suspend ()->Deferred<T>, key: String, cachePolicy: CachePolicy): Deferred<T> { | ||
return when(cachePolicy){ | ||
is CachePolicy.LifeCache-> cacheLifeResolver( source, key, cachePolicy) | ||
is CachePolicy.EvictProvider -> cacheProviderResolver(source, key, cachePolicy) | ||
} | ||
} | ||
|
||
@PublishedApi internal inline fun <reified T : Any> cacheLifeResolver(noinline source: suspend () -> Deferred<T>, key: String, cachePolicy: CachePolicy.LifeCache): Deferred<T> { | ||
getFromCacheValidateTime<T>(key, cachePolicy)?.let { | ||
return async { it } | ||
} ?: run { | ||
return getFromSource(source, key) | ||
} | ||
} | ||
|
||
@PublishedApi internal inline fun <reified T : Any> cacheProviderResolver(noinline source: suspend () -> Deferred<T>, key: String, cachePolicy: CachePolicy.EvictProvider): Deferred<T> { | ||
return if (cachePolicy.fromSource) { | ||
getFromSource(source, key) | ||
} else { | ||
getFromCache<T>(key)?.let { | ||
return async { it } | ||
} ?: run { | ||
return getFromSource(source, key) | ||
} | ||
} | ||
} | ||
|
||
@PublishedApi internal fun <T> getFromSource(source: suspend ()->Deferred<T>, key: String):Deferred<T>{ | ||
return async { | ||
val result = source().await() | ||
database.getDatabase().apply { | ||
beginTransaction() | ||
copyToRealmOrUpdate(Cache(key, Gson().toJson(result), Calendar.getInstance().time)) | ||
commitTransaction() | ||
close() | ||
} | ||
result | ||
} | ||
} | ||
|
||
@PublishedApi internal inline fun <reified T:Any> getFromCache(key: String): T? { | ||
val resultDb = database.getDatabase().where(Cache::class.java).equalTo("id", key).findFirst()?.data | ||
return if (resultDb != null) { | ||
val listType = object : TypeToken<T>() {}.type | ||
Gson().fromJson(resultDb, listType) as T | ||
} else { | ||
return null | ||
} | ||
} | ||
|
||
@PublishedApi internal inline fun <reified T:Any> getFromCacheValidateTime(key: String, lifeCache: CachePolicy.LifeCache): T? { | ||
val resultDb = database.getDatabase().where(Cache::class.java).equalTo("id", key).findFirst() | ||
return if (resultDb != null) { | ||
if(resultDb.date.isValidCache(lifeCache.duration, lifeCache.timeUnit)){ | ||
val listType = object : TypeToken<T>() {}.type | ||
Gson().fromJson(resultDb.data, listType) as T | ||
}else{ | ||
null | ||
} | ||
} else { | ||
null | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
library/src/main/java/io/coroutines/cache/core/CoroutinesCacheTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package io.coroutines.cache.core | ||
|
||
import android.content.Context | ||
import kotlinx.coroutines.* | ||
import kotlin.coroutines.CoroutineContext | ||
|
||
open class CoroutinesCacheTest(private var context: Context): CoroutinesCache,CoroutineScope{ | ||
|
||
private val executionJob: Job by lazy { Job() } | ||
|
||
override val coroutineContext: CoroutineContext by lazy { | ||
Dispatchers.Unconfined + executionJob | ||
} | ||
|
||
inline fun <reified T:Any> asyncCache(noinline source: suspend ()->Deferred<T>, key: String, cachePolicy: CachePolicy): Deferred<T> { | ||
return async {source().await()} | ||
} | ||
} |