WaterfallCache

A reactive Android cache composed of multiple cache levels.

License

License

GroupId

GroupId

com.github.simonpercic
ArtifactId

ArtifactId

waterfallcache
Last Version

Last Version

1.1.0
Release Date

Release Date

Type

Type

aar
Description

Description

WaterfallCache
A reactive Android cache composed of multiple cache levels.
Project URL

Project URL

https://github.com/simonpercic/WaterfallCache
Source Code Management

Source Code Management

https://github.com/simonpercic/WaterfallCache

Download waterfallcache

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.simonpercic/waterfallcache/ -->
<dependency>
    <groupId>com.github.simonpercic</groupId>
    <artifactId>waterfallcache</artifactId>
    <version>1.1.0</version>
    <type>aar</type>
</dependency>
// https://jarcasting.com/artifacts/com.github.simonpercic/waterfallcache/
implementation 'com.github.simonpercic:waterfallcache:1.1.0'
// https://jarcasting.com/artifacts/com.github.simonpercic/waterfallcache/
implementation ("com.github.simonpercic:waterfallcache:1.1.0")
'com.github.simonpercic:waterfallcache:aar:1.1.0'
<dependency org="com.github.simonpercic" name="waterfallcache" rev="1.1.0">
  <artifact name="waterfallcache" type="aar" />
</dependency>
@Grapes(
@Grab(group='com.github.simonpercic', module='waterfallcache', version='1.1.0')
)
libraryDependencies += "com.github.simonpercic" % "waterfallcache" % "1.1.0"
[com.github.simonpercic/waterfallcache "1.1.0"]

Dependencies

compile (3)

Group / Artifact Type Version
io.reactivex : rxandroid jar 1.0.1
io.reactivex : rxjava jar 1.1.0
com.github.simonpercic : bucket jar 1.0.0

Project Modules

There are no modules declared in this project.

WaterfallCache

Multi-level RxJava-powered cache for Android.

Supports Rx and async methods for all operations.

Build Status Download

Caches

Includes the following caches:

You can also implement your own cache and add it to cache levels, as long as it implements the RxCache interface.

How does it work?

Read operations

When getting a value, it will first try to obtain it from the first cache level, if it does not contain it, it will try to obtain it from the next cache level. This continues through all cache levels until the value is obtained.

Upon obtaining the value, it is written to all cache levels that are lower than the level the value was obtained from.

Write operations

Values are written and removed from all cache levels.

Usage

Add using Gradle:

compile 'com.github.simonpercic:waterfallcache:1.1.0'

Use:

// create cache
Cache cache = WaterfallCache.builder()
                .addMemoryCache(1000)
                .addDiskCache(this, 1024 * 1024)
                .build();

Rx

Observable<T> get(String key, Class<T> classOfT);

Observable<Boolean> put(String key, Object object);

Observable<Boolean> contains(String key);

Observable<Boolean> remove(String key);

Observable<Boolean> clear();

or

Async

<T> void getAsync(String key, Type typeOfT, WaterfallGetCallback<T> callback);

void putAsync(String key, Object object, WaterfallCallback callback);

void containsAsync(String key, WaterfallGetCallback<Boolean> callback);

void removeAsync(String key, WaterfallCallback callback);

void clearAsync(WaterfallCallback callback);

Values expiration

The library includes a LazyExpirableCache that can work with any Cache. It enables lazy value expiration based on the insertion time and an expiration time. Since it's lazy, the values are removed only when trying to obtain them.

// create an expirable cache by passing an instance of a Cache
Cache expirableCache = LazyExpirableCache.fromCache(cache, 10, TimeUnit.MINUTES);

Time provider

By default, LazyExpirableCache uses Android's built-in SystemClock.elapsedRealtime() as a time provider in order to determine whether a cache value should expire. You can also provide your own time provider, by passing either:

or

  • an Observable<Long> that emits the current time

RxTime

You can use RxTime as a complementary library that provides the current UTC time from the internet to serve as your time provider. That way the values are ensured to expire correctly, even if you are caching them for a longer period of time (which can span device deep sleep or even device restarts).

Sample application

See the included sample application to see a practical example of usage.

Change Log

See CHANGELOG.md

License

Open source, distributed under the MIT License. See LICENSE for details.

Versions

Version
1.1.0
1.0.2
1.0.1
1.0.0