Skip to content

Commit

Permalink
Test class
Browse files Browse the repository at this point in the history
  • Loading branch information
diefferson committed Feb 11, 2019
1 parent 2c88f74 commit 1bfb5f3
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 87 deletions.
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ apply plugin: 'realm-android'


group = 'com.github.diefferson'
version='0.2.0'
version='0.2.1'

android {
compileSdkVersion 28
Expand Down
88 changes: 2 additions & 86 deletions library/src/main/java/io/coroutines/cache/core/CoroutinesCache.kt
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
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
}
}
}
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()}
}
}

0 comments on commit 1bfb5f3

Please sign in to comment.