CacheX - A feasible caching library for Android.

Overview

Release

CacheX

A feasible caching library for Android.

Features

  • Real-time update
  • Lightweight
  • Secure
  • Thread Safe

How does it work?

Caching is just a simple key-value pair data saving procedure. CacheX follows the same approach. CacheX uses SharedPreference as storage for caching data. Since we really can't just save the original data because of security issues. CacheX uses AES encryption & decryption behind the scene when you are caching data or fetching data from the cache. Also, you can observer cached data in real-time.

Documentation

Installation


Step 1. Add the JitPack repository to your build file

allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}

Step 2. Add the dependency

dependencies {
    implementation 'com.github.rommansabbir:CacheX:Tag'
}

Version available

Latest Releases
2.1.0

What's new in this version?

  • Implementation of UseCase which interact between the app layer & library layer
  • CacheX works asynchronously
  • Performance Improved

What is UseCase & what its implementation?

  • Simply UseCase in an interactor between two layer to communicate with each others (If you know you about Android Clean Architecture, you must be familiar with UseCase. It is one of the core component of ACA)
  • UseCase take input from the respective thread and execute the whole operatin under a Background Thread & return the data on Main Thread

What about the implementation or the work flow?

  • Initialize CacheX properly
  • Get an instance of CacheX (Instance is singleton)
  • Call proper method to cahce data or get data from cache
  • Method return a respective use case
  • Provide the proper param to the use case & use case will return either expected data or an exception

Simple, huh? (Feel free to give any suggestion, I would love to get suggestion for further improvement)


Usages

Instantiate CacheX components in your app application class

This step is important!!

Define an encryption key to instantiate CacheX components. This key will be used to encrypt or decrypt data using AES algorithm.

Initialize CacheX component by calling CacheXCore.init() pass contex, encryption key & app name for CacheX session.

Don't forget to call at CacheXCore.init() from application layer otherwise, CacheX will not work properly and will throw an exception.

    private val encryptionKey = "!x@4#w$%f^g&h*8(j)9b032ubfu8238!"

    override fun onCreate() {
        super.onCreate()
        CacheXCore.init(this, encryptionKey, getString(R.string.app_name))
    }

How to access?

Call CacheXCore.getInstance() to get reference of CacheX and you can access all public methods.


What's the purpose of the UseCase?

Available public methods

  • fun cacheSingle(): SingleCacheUseCase - To cache single data which return an instance of SingleCacheUseCase

  • fun getCacheSingle(): GetSingleCacheUseCase - To get single data from cache which return an instance of GetSingleCacheUseCase

  • fun cacheList(): ListCacheUseCase - To cache list of data which return an instance of ListCacheUseCase

  • fun getCacheList(): GetListCacheUseCase - To get list of cached data from cache which return an instance of GetListCacheUseCase

  • fun registerListener(callback: CacheXCallback, key: String) - To register listener respective to a Key & notify the listener through the callback on data changes

  • fun registerListener(callback: CacheXCallback, key: ArrayList) - - To register list of listeners respective to a Key List & notify the listener through the callback on data changes

  • fun unregisterListener(key: String) - Unregister any listener according to the respective key

  • fun unregisterListener(key: ArrayList) - Unregister list of listeners according to the respective key list

  • fun clearListeners() - Clear all listeners

  • fun clearCacheByKey(key: String) - Clear any cache by it's key

  • fun clearAllCache() - Clear all cache


How to use the callback to get notified on data changes?

  • Implement the callback to Activity or Fragment or ViewModel.
class MainViewModel : ViewModel(), CacheXCallback {
    override fun onChanges(key: String) {
        when (key) {
            MainActivity.SINGLE_KEY -> {
                getSingleFromCache(key)
            }
            MainActivity.LIST_KEY -> {
                getListFromCache(key)
            }
        }
    }

    private fun getListFromCache(key: String) {...}

    private fun cacheLSingle(key: String, data: String) {...}
}
  • Register a listener or list of listener from your Activity or Fragment onResume() method. In this example, I have implemented the callback on my respective view model.
    override fun onResume() {
        super.onResume()
        CacheXCore.getInstance().registerListener(viewModel, arrayListOf(LIST_KEY, SINGLE_KEY))
        //or 
        CacheXCore.getInstance().registerListener(viewModel, LIST_KEY)
    }
  • To unregister a listener or list of listeners from your Activity or Fragment onStop() method.
    override fun onStop() {
        CacheXCore.getInstance().unregisterListener(arrayListOf(LIST_KEY, SINGLE_KEY))
        //or
        CacheXCore.getInstance().unregisterListener(LIST_KEY)
        super.onStop()
    }

Need more information regarding the solid implementation? - Check out the sample application.

Contact me

Portfolio | LinkedIn | Twitter | Facebook

License


Apache Version 2.0

Copyright (C) 2021 Romman Sabbir

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

		http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
You might also like...
Android library to easily serialize and cache your objects to disk using key/value pairs.

Deprecated This project is no longer maintained. No new issues or pull requests will be accepted. You can still use the source or fork the project to

Form Validator Library for Android

Android-Validator Form Validator Library for Android [](https://flattr.com/submit/auto?user_id=throrin19&url=https://github.com/throrin19/Android-Vali

Very easy to use wrapper library for Android SharePreferences

Treasure English document Treasure是一个Android平台上基于SharePreferences的偏好存储库,只需要定义接口,无需编写实现,默认支持Serializable和Parcelable。运行时0反射,不仅使用方便而且性能和原生写法几乎无差别。 使用方法 1

Error handling library for Android and Java

ErrorHandler Error handling library for Android and Java Encapsulate error handling logic into objects that adhere to configurable defaults. Then pass

Small Android library to help you incorporate MVP, Passive View and Presentation Model patterns in your app
Small Android library to help you incorporate MVP, Passive View and Presentation Model patterns in your app

DroidMVP About DroidMVP is a small Android library to help you incorporate the MVP pattern along with Passive View and Presentation Model (yes, those

A simple Android utils library to write any type of data into cache files and read them later.

CacheUtilsLibrary This is a simple Android utils library to write any type of data into cache files and then read them later, using Gson to serialize

Android library that regroup bunch of dateTime utilities

DateTimeUtils This library is a package of functions that let you manipulate objects and or java date string. it combine the most common functions use

Slinger - deep linking library for Android
Slinger - deep linking library for Android

Slinger - deep linking library for Android Slinger is a small Android library for handling custom Uri which uses regular expression to catch and route

A simple and easy to use stopwatch and timer library for android

TimeIt Now with Timer support! A simple and easy to use stopwatch and timer library for android Introduction A stopwatch can be a very important widge

Releases(2.1.0)
Owner
Romman Sabbir
Software Engineer (Android) | Backend Developer | Tech Lover | Music Producer
Romman Sabbir
KmmCaching - An application that illustrates fetching data from remote data source and caching it in local storage

An application that illustrates fetching data from remote data source and caching it in local storage for both IOS and Android platforms using Kotlin Multiplatform Mobile and SqlDelight.

Felix Kariuki 5 Oct 6, 2022
Android library which makes it easy to handle the different obstacles while calling an API (Web Service) in Android App.

API Calling Flow API Calling Flow is a Android library which can help you to simplify handling different conditions while calling an API (Web Service)

Rohit Surwase 19 Nov 9, 2021
Android Utilities Library build in kotlin Provide user 100 of pre defined method to create advanced native android app.

Android Utilities Library build in kotlin Provide user 100 of pre defined method to create advanced native android app.

Shahid Iqbal 4 Nov 29, 2022
A robust native library loader for Android.

ReLinker A robust native library loader for Android. More information can be found in our blog post Min SDK: 9 JavaDoc Overview The Android PackageMan

Keepsafe 2.9k Dec 27, 2022
Joda-Time library with Android specialization

joda-time-android This library is a version of Joda-Time built with Android in mind. Why Joda-Time? Android has built-in date and time handling - why

Daniel Lew 2.6k Dec 9, 2022
UPnP/DLNA library for Java and Android

Cling EOL: This project is no longer actively maintained, code may be outdated. If you are interested in maintaining and developing this project, comm

4th Line 1.6k Jan 4, 2023
:iphone: [Android Library] Get device information in a super easy way.

EasyDeviceInfo Android library to get device information in a super easy way. The library is built for simplicity and approachability. It not only eli

Nishant Srivastava 1.7k Dec 22, 2022
Android library for viewing, editing and sharing in app databases.

DbInspector DbInspector provides a simple way to view the contents of the in-app database for debugging purposes. There is no need to pull the databas

Infinum 924 Jan 4, 2023
Android Market In-app Billing Library

Update In-app Billing v2 API is deprecated and will be shut down in January 2015. This library was developed for v2 a long time ago. If your app is st

Robot Media 533 Nov 25, 2022
An Android library allowing images to exhibit a parallax effect that reacts to the device's tilt

Motion An Android library allowing images to exhibit a parallax effect. By replacing static pictures and backgrounds with a fluid images that reacts t

Nathan VanBenschoten 781 Nov 11, 2022