Giter VIP home page Giter VIP logo

coroutinescache's Introduction

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

coroutinescache's People

Contributors

diefferson avatar dnnyg33 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.