ReactiveCache


Source link: https://github.com/VictorAlbertos/ReactiveCache

ReactiveCache

The act of caching data with ReactiveCache is just another transformation in the reactive chain. ReactiveCache's API exposes both Single, SingleTransformer and Completable reactive types to gracefully merge the caching actions with the data stream.

Features

  • A dual cache based on both memory and disk layers.
  • Automatic deserialization-serialization for custom Types, List, Map and Array.
  • Pagination
  • A lifetime system to expire data on specific time lapses.
  • Data encryption.
  • Customizable disk cache size limit.
  • Migrations to evict data by Type between releases.
  • A complete set of built-in functions to perform write operations easily using List, such as addFirst, evictLast, addAll and so on.

SetUp

Add to top level gradle.build file

allprojects {

  repositories {

maven {
 url "https://jitpack.io" 
}

  
}
 
}

Add to app module gradle.build file

dependencies {

  compile 'com.github.VictorAlbertos:ReactiveCache:1.1.1-2.x'
  compile 'com.github.VictorAlbertos.Jolyglot:gson:0.0.3'
  compile 'io.reactivex.rxjava2:rxjava:2.0.4' 
}

Usage

ReactiveCache

Create a single instace of ReactiveCache for your entire application. The builder offers some additional configurations.

ReactiveCache reactiveCache = new ReactiveCache.Builder()

.using(application.getFilesDir(), new GsonSpeaker());

evictAll() returns a Completable which evicts the cached data for every provider:

cacheProvider.evictAll()

Provider

Call reactiveCache#provider() to create a Provider to manage cache operations. The builder offers some additional configurations.

Provider<List<Model>> cacheProvider =
reactiveCache.<List<Model>>provider()

.withKey("models");

replace() returns a SingleTransformer which replaces the provider data with the item emitted from the Single source. If the source throws an exception, calling replace() doesn't evict the provider data.

api.getModels()  .compose(cacheProvider.replace())

read() returns an Single which emits the provider data. If there isn't any data available, throws an exception.

cacheProvider.read()

readWithLoader() returns a SingleTransformer which emits the provider data. If there isn't any data available, it subscribes to the Single source to cache and emit its item.

api.getModels()

.compose(cacheProvider.readWithLoader())

evict() returns a Completable which evicts the provider data:

cacheProvider.evict()

ProviderGroup

Call reactiveCache#providerGroup() to create a ProviderGroup to manage cache operations with pagination support. The builder offers some additional configurations.

ProviderGroup<List<Model>> cacheProvider =
reactiveCache.<List<Model>>providerGroup()

.withKey("modelsPaginated");

ProviderGroup exposes the same methods as Provider but requesting a key as an argument. That way the scope of the provider data in every operation is constrained to the data associated with the key.

api.getModels(group)  .compose(cacheProvider.replace(group))  cacheProvider.read(group)  api.getModels(group)

.compose(cacheProvider.readWithLoader())  cacheProvider.evict(group)

evict() is an overloaded method to evict the provider data for the entire collection of groups.

cacheProvider.evict()

Built-in functions for writing operations

When the data is encoded as type List<Model>, you may use ProviderList and ProviderGroupList. Both clases inherit from their base clase ( Provider and ProviderGroup respectively), so - besides exposing all their base funcionality- they offer a supletory api to perform write operations.

Call reactiveCache#providerList() to create a ProviderList.

ProviderList<Model> cacheProvider =
reactiveCache.<Model>providerList()

.withKey("models");

Or call reactiveCache#providerGroupList() to create a ProviderGroupList.

ProviderGroupList<Model> cacheProviderGroup =
reactiveCache.<Model>providerGroupList()

.withKey("modelsPaginated");

Both cacheProvider.entries() and cacheProviderGroup.entries(group) return an ActionsList<Model> instance which allows to easily operate with the cached data thought a whole set of functions.

ActionsList<Model> actions = cacheProvider.entries();
ActionsList<Model> actions = cacheProviderGroup.entries(group);

Every function exposed through actions return a Completable which must be subscribed to in order to consume the action. Follow some examples:

actions.addFirst(new Model())  //Add a new element at 5 position actions.add((position, count) -> position == 5, new Model())  //Evict first element if the cache has already 300 records actions.evictFirst(count -> count > 300)  //Update the element with id 5 actions.update(model -> model.getId() == 5, model -> {

  mock.setActive();

  return mock; 
}
)  //Update all inactive modelds actions.updateIterable(model -> model.isInactive(), model -> {

  model.setActive();

  return mock; 
}
)

This table summarizes the available functions.

Use cases

Next examples illustrate how to use ReactiveCache on the data layer for client Android applications. They follow the well-known repository pattern in order to deal with data coming from a remote repository (server) and a local one (ReactiveCache).

Simple user session.

class UserRepository {

  private final Provider<User> cacheProvider;
  private final ApiUser api;

UserRepository(ApiUser api, ReactiveCache reactiveCache) {

 this.api = api;

 this.cacheProvider = reactiveCache.<User>provider()

  .withKey("user");

  
}

Single<User> login(String email) {

 return api.loginUser(email)

  .compose(cacheProvider.replace());

  
}

Single<Boolean> isLogged() {

 return cacheProvider.read()

  .map(user -> true)

  .onErrorReturn(observer -> false);

  
}

Single<User> profile() {

 return cacheProvider.read();

  
}

Completable updateUserName(String name) {

 return cacheProvider.read()

  .map(user -> {

 user.setName(name);

 return user;

  
}
)

  .compose(cacheProvider.replace())

  .toCompletable();

  
}

Completable logout() {

 return api.logout().andThen(cacheProvider.evict());

  
}
 
}

Adding and removing tasks.

class TasksRepository {

  private final ProviderList<Task> cacheProvider;
  private final ApiTasks api;

TasksRepository(ApiTasks api, ReactiveCache reactiveCache) {

 this.api = api;

 this.cacheProvider = reactiveCache.<Task>providerList()

  .withKey("tasks");

  
}

Single<Reply<List<Task>>> tasks(boolean refresh) {

 return refresh ? api.tasks().compose(cacheProvider.replaceAsReply())

  : api.tasks().compose(cacheProvider.readWithLoaderAsReply());

  
}

Completable addTask(String name, String desc) {

 return api.addTask(1, name, desc)

  .andThen(cacheProvider.entries()

.addFirst(new Task(1, name, desc)));

  
}

Completable removeTask(int id) {

 return api.removeTask(id)

  .andThen(cacheProvider.entries()

.evict((position, count, element) -> element.getId() == id));

  
}
 
}

Paginated feed of events.

class EventsRepository {

  private final ProviderGroup<List<Event>> cacheProvider;
  private final ApiEvents apiEvents;

EventsRepository(ApiEvents apiEvents, ReactiveCache reactiveCache) {

 this.apiEvents = apiEvents;

 this.cacheProvider = reactiveCache.<List<Event>>providerGroup()

  .withKey("events");

  
}

Single<Reply<List<Event>>> events(boolean refresh, int page) {

 if (refresh) {

return apiEvents.events(page)

 .compose(cacheProvider.replaceAsReply(page));

 
}

  return apiEvents.events(page)

  .compose(cacheProvider.readWithLoaderAsReply(page));

  
}
 
}

Configuration

ReactiveCache

When building ReactiveCache the next global configurations are available thought the builder:

  • diskCacheSize(int) sets the max memory in megabytes for all the cached data on disk. Default value is 100.

  • encrypt(String) sets the key to be used for encrypting the data on those providers as such configured.

  • useExpiredDataWhenNoLoaderAvailable() if invoked, ReactiveCache dispatches records already expired instead of throwing.

  • migrations(List<MigrationCache>) every MigrationCache expects a version number and a Class[] to check what cached data matches with these classes to evict it from disk. Use MigrationCache for those Type which have added new fields between app releases.

ReactiveCache reactiveCache = new ReactiveCache.Builder()

.diskCacheSize(100)

.encrypt("myStrongKey1234")

.useExpiredDataWhenNoLoaderAvailable()

.migrations(Arrays.asList(

 new MigrationCache(1, new Class[] {
Model.class
}
),

 new MigrationCache(1, new Class[] {
Model1.class
}
)))

.using(application.getFilesDir(), new GsonSpeaker());

Config provider

When building Provider, ProviderList, ProviderGroup or ProviderGroupList the next configuration is available thought the builder:

  • encrypt(boolean) when true, the data cached by this provider is encrypted using the key specified in ReactiveCache#encript(key). Default value is false.

  • expirable(boolean) when false, the data cached by this provider is not eligible to be expired if not enough space remains on disk. Default value is true.

  • lifeCache(long, TimeUnit) sets the amount of time before the data would be expired. By default the data has no life time.

 Provider<Model> cacheModel = reactiveCache.<Model>provider()

  .encrypt(true)

  .expirable(false)

  .lifeCache(60, TimeUnit.MINUTES)

  .withKey("model");

Author

Víctor Albertos

Another author's libraries using RxJava:

  • Mockery: Android and Java library for mocking and testing networking layers with built-in support for Retrofit.
  • RxCache: Reactive caching library for Android and Java. (ReactiveCache uses internally the core from RxCache).
  • RxActivityResult: A reactive-tiny-badass-vindictive library to break with the OnActivityResult implementation as it breaks the observables chain.
  • RxFcm: RxJava extension for Android Firebase Cloud Messaging (aka fcm).
  • RxSocialConnect: OAuth RxJava extension for Android.

Resources

Android Library to help you with your runtime Permissions.

This is a lightweight Image Cropper for Android which also supports no-crop feature.

Recycling mechanism for pager adapter.

Take Android screenshots with Falcons bright eye!

Includes dialogs, toasts and all other extra windows in your screenshot.

Android Recycler View with Parallax Effect on each item, inspired by soundcloud android app.

Simple aspect oriented annotation debugging library for Android development.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes