TBCBank / RetroCache

Retrofit caching extension library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

RetroCache

Kotlin library that provides caching capabilities for Retrofit requests. With this library, you can annotate your Retrofit service methods to specify caching behavior for responses, enhancing the performance and efficiency of your network calls.

Add library to the project

project level gradle:

repositories {
    maven { url = uri("https://jitpack.io") }
}

module level gradle:

dependencies {
    implementation("com.github.TBCBank:RetroCache:1.0.1")
}

Project will be added to Maven Central in future

Setup

Create RetroCacheManager object

This class uses an LRU cache mechanism. When the maximum memory size is reached, it automatically removes the least accessed objects to accommodate new entries. The cache will automatically clear upon application termination.

    RetroCacheManager.Builder()
            .maxMemorySize(512 * 60 * 1024) // Maximum size of memory in bytes
            .maxObjectSize(512 * 1024) // If object will be more than 512kb, it won't be added to the cache
            .enableLogger(true)
            .build()

In most cases you need to provide [RetroCacheManager] as Singleton

When building Okhttp object, pass RetroCacheInterceptor

    OkHttpClient.Builder()
            .addInterceptor(RetroCacheInterceptor(retroCacheManager))
            .build()

Features

Annotation-based caching for Retrofit service methods

    @Cache
    @GET("fetch")
    suspend fun getData(): Response<Data>

Control cache with tags

    @Cache(tag = "tag1")
    @GET("fetch")
    suspend fun getData(): Response<Data>

Define cache refresh time in milliseconds

    @Cache(cacheTimeMillis = 60_000)
    @GET("fetch")
    suspend fun getData(): Response<Data>

Dynamic control of caching and refreshing

    @Cache
    @GET("fetch")
   suspend fun getData(
        @CacheControl cachePolicy: CachePolicy = CachePolicy.Refresh // will refresh data
    ): Response<Data>

Clearing cache

    retroCacheManager.clearAllByTag("tag1") // will remove all objects with corresponding tag

    retroCacheManager.clearAll() // will remove all objects from the cache

You may use [retroCacheManager.clearAll()] in [onLowMemory()] of application

   override fun onLowMemory() {
       super.onLowMemory()
       retroCacheManager.clearAll()
   }

About

Retrofit caching extension library

License:MIT License


Languages

Language:Kotlin 100.0%