CoroutinesCache

Additional

Language
N/A
Version
0.2.5 (Jul 5, 2019)
Created
Nov 8, 2018
Updated
Jul 5, 2019
Owner
Diefferson Santos (diefferson)
Contributor
Diefferson Santos (diefferson)
1
Activity
Badge
Generate
Download
Source code
APK file

Blurb

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.2.3"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.1"
}

Usage

Using Kotlin Coroutine Adapter create your retrofit interface:

interface RestApi {

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

    @GET("user")
    fun getUsers(): Deferred<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)).await()
    
   //CachePolicy.TimeCache to defines a time to expire cache
  val user:User = myCache.asyncCache({restApi.getUser(id)} , "userKey"+id, CachePolicy.LifeCache(15, TimeUnit.MINUTES)).await()
  
     //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).await()
  

Author

Diefferson Santos