Caching made easy on android with Kotlin (Part 3)

Anurag
2 min readJan 30, 2020

--

We have covered creating a cache layer manually and caching method outputs using annotation in the previous posts. Today we will see how we can turn a class into a cache using the “@Freeze” annotation offered by the ColdStorage library.

Why do I need to convert a regular class into a cache?

When we have multiple methods on which we want to add the caching logic, it can get tiresome annotating all of them with “@Refrigerate”.So we will simply put all the methods into a single class and annotate it with “@Freeze” to generate a cache layer for us.

Creating a cache layer with just one annotation sounds magical, doesn’t it? Let us see how the implementation will look like. 😃

Prerequisites

  • Add the following to the root build.gradle
allprojects {
repositories {
google()
jcenter()
maven { url 'https://jitpack.io' }
}
}
  • Add the “Kotlin-kapt” Gradle plugin to the app build.gradle
apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'
apply plugin: "kotlin-kapt"
  • Specify the following dependencies for cold storage library
implementation "com.github.crypticminds.ColdStorage:coldstoragecache:3.0.0"
kapt "com.github.crypticminds.ColdStorage:coldstoragecompiler:3.0.0"
implementation "com.github.crypticminds.ColdStorage:coldstorageannotation:3.0.0"
  • Create an application class and initialize the class.
  • Register the application in the manifest file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my packagename">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:name=".application.Application">

........ other details like activity
</application>

The Class of Interest

Now we will create a class and annotate it with “@Freeze”.

That is all you need to do to create the cache layer. Now we will simply use the generated class “MyBeautifulCacheLayer” to call the methods.

You can also specify when the cache will expire using the timeToLive parameter of Freeze.

//The cached value will expire after 2 seconds.
@Freeze(timeToLive = 2000)

Application logic

In this activity, we are calling the remote endpoint and displaying the output in the textView. The generated method will accept all of the parameters like the original method with one additional parameter, the implementation of OnOperationSuccessfulCallback<MethodOutput>. This is a callback that will be used to pass the data from the background thread ( since the cache handles all operations asynchronously) to the main thread.

After applying the annotation, you can run your application so that the class is generated and your IDE indexes it and then you will be able to use the generated class like any other regular one.

You can find the entire source code in GitHub repo. ▶️

That’s it. You effectively created a caching layer in your application using just one annotation. 👏

Check out the ColdStorage repository here ▶️

--

--