Skip to content

diefferson/CoroutinesCache

Repository files navigation

Android Arsenal

CoroutinesCache

Kotlin Coroutines is simple and your Cache Handler must be too, thats why I created this library, a powerfull caching library for Kotlin Android

Every Android application is a client application, which means it does not make sense to create and maintain a database just for caching data.

Plus, the fact that you have some sort of legendary database for persisting your data does not solves by itself the real challenge: to be able to configure your caching needs in a flexible and simple way.

Inspired by Retrofit api and RxCache, CoroutinesCache is a reactive caching library for Android which turns your caching needs into simple functions.

When supplying an deferred (these is the actualy supported Couroutine type) which contains the data provided by an expensive task -probably an http connection, CoroutinesCache determines if it is needed to execute request to it or instead fetch the data previously cached. This decision is made based on the CachePolicy.

  myCache.asyncCache(source = {restApi.getUser()},key =  "userKey", cachePolicy=CachePolicy.LifeCache(15, TimeUnit.MINUTES))

Setup

Add the JitPack repository in your build.gradle (top level module):

allprojects {
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

And add next dependencies in the build.gradle of the module:

dependencies {
    implementation "com.github.diefferson:CoroutinesCache:0.3.0"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.1"
}

Usage

Using Kotlin Coroutine Adapter create your retrofit interface:

interface RestApi {

    @GET("user/{id}")
    suspend fun getUser(@Path("id")idUser:String): User

    @GET("user")
    suspend fun getUsers(): List<User>
}

CoroutinesCache exposes asyncCache() method to help use cache in a row.

Build an instance of CouroutinesCache and call asyncCache() method

Finally, instantiate the CouroutinesCache .

  val myCache = CoroutinesCache(context,lifecycleOwner = this)
  
  //CachePolicy.EvictProvider to defines to local cache ou data source 
  val users:List<User> = myCache.asyncCache({restApi.getUsers()} , "usersKey", CachePolicy.EvictProvider(true))
    
   //CachePolicy.TimeCache to defines a time to expire cache
  val user:User = myCache.asyncCache({restApi.getUser(id)} , "userKey"+id, CachePolicy.LifeCache(15, TimeUnit.MINUTES))
  
     //CachePolicy.LifecycleCache - Pass a LifecycleOwner in constructor, cache is deleted when lifecycle is destroyed
  val user:User = myCache.asyncCache({restApi.getUser(id)} , "userKey"+id, CachePolicy.LifecycleCache)
  

Author

Diefferson Santos

About

Powerfull caching library for Kotlin Android

Resources

License

Stars

Watchers

Forks

Packages

No packages published