Skip to content

Commit

Permalink
Corrections test
Browse files Browse the repository at this point in the history
  • Loading branch information
diefferson committed Feb 11, 2019
1 parent 1bfb5f3 commit 7954152
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 113 deletions.
12 changes: 9 additions & 3 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.1'
version='0.2.2'

android {
compileSdkVersion 28
Expand Down
93 changes: 91 additions & 2 deletions library/src/main/java/io/coroutines/cache/core/CoroutinesCache.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,94 @@
package io.coroutines.cache.core

import kotlinx.coroutines.Deferred
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

interface CoroutinesCache
open class CoroutinesCache(private var context: Context, val test: Boolean = false):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 if(test){
async{source().await()}
}else{
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
}
}
}

This file was deleted.

This file was deleted.

0 comments on commit 7954152

Please sign in to comment.