Android Caching: CacheX

Romman Sabbir
3 min readFeb 22, 2020

--

CacheX

What is Cache?

A cache is nothing more than a key-value store with a getter and setter.

When to use

When developing an online based Android application, as a developer we have to deal with some circumstances, one of them is Network connection or stable internet connection.

Assume a situation where you are requesting to your server side to provide some data & something went wrong, or request time out, or maximum server connection has been established, then what you are gonna do? Just show an error message and leave the screen blank? No. That’s a bad user experience, instead of doing this use Cached data from Cache Store. When your request to server side is completed & you get data from server side, first save the data to cache store for further reuses.

Assume that your device doesn’t have an internet connection right now, but you want to get some information from the app. What are you going to do?. Show error message and leave screen blank or exit app?, No. Instead fetch data from cache store.

How it works?

CacheX is a feasible opensource library for Android. Caching is just a simple key value pair data saving procedure. CacheX follow the same approach. CacheX use SharedPreference as storage for caching data. Since, we really can’t just save the original data because of security issue. CacheX uses AES encryption & decryption behind the scene when you are caching data or fetching data from cache.

What features CacheX offering:

  1. Data Security: When caching any sensitive data without any kind of encryption break the data security rules, such as user authentication token. CacheX encrypt data with AES encryption before storing cache, same for fetching, before transformation data into your desire data type, CacheX decrypt the data first for data transformations.
  2. Cache Generic Type Data: CacheX take generic type data as it input (Such as Data Model / POJO, String, Numbers and others), process the data and transform into a single string. You can provide single items or list of items to cache.
  3. Get Generic Data from Cache: CacheX provide features to fetch data from cache storage with the generic data type you passed to CacheX. Before accessing data/list of data, you need to cast to specific class since CacheX provide generic class.
  4. Performance/Threading: CacheX respect user experiences, CacheX use RxJava behind the scene for caching data or fetching data from cache storage. All processes are executed in a background thread and notified on Main or UI thread when process is completed successfully or with an error.
  5. Storage: CahceX use SharedPreference behind the scene to store cached data. SharedPreference is a light-weight storage type provided by Android Framework.

GitHub Link for CacheX Library (Sample app included):

Let me know for any kind corrections or suggestions. If you face any issues, open a issue in GitHub. Thanks.

--

--

Romman Sabbir
Romman Sabbir

Written by Romman Sabbir

Senior Android Engineer / Electronic Music Producer

No responses yet