All Projects → anupcowkur → Reservoir

anupcowkur / Reservoir

Licence: mit
Android library to easily serialize and cache your objects to disk using key/value pairs.

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Reservoir

Predis Async
Asynchronous PHP client library for Redis built on top of ReactPHP
Stars: ✭ 354 (-47.48%)
Mutual labels:  async, asynchronous
Kovenant
Kovenant. Promises for Kotlin.
Stars: ✭ 657 (-2.52%)
Mutual labels:  async, rxjava
Rxcache
简单一步,缓存搞定。这是一个专用于 RxJava,解决 Android 中对任何 Observable 发出的结果做缓存处理的框架
Stars: ✭ 377 (-44.07%)
Mutual labels:  rxjava, cache
Apex Recipes
A library of concise, meaningful examples of Apex code for common use cases following best practices.
Stars: ✭ 307 (-54.45%)
Mutual labels:  async, cache
Vertx Guide For Java Devs
Vert.x 3 guide for Java developers
Stars: ✭ 500 (-25.82%)
Mutual labels:  async, rxjava
Async Sema
Semaphore using `async` and `await`
Stars: ✭ 326 (-51.63%)
Mutual labels:  async, asynchronous
React Query
⚛️ Hooks for fetching, caching and updating asynchronous data in React
Stars: ✭ 24,427 (+3524.18%)
Mutual labels:  async, cache
Drone Core
The core crate for Drone, an Embedded Operating System.
Stars: ✭ 263 (-60.98%)
Mutual labels:  async, asynchronous
Elle
The Elle coroutine-based asynchronous C++ development framework.
Stars: ✭ 459 (-31.9%)
Mutual labels:  asynchronous, serialization
Quill
Asynchronous Low Latency C++ Logging Library
Stars: ✭ 422 (-37.39%)
Mutual labels:  async, asynchronous
Php Watcher
Monitor for any changes in your php application and automatically restart it (suitable for async apps).
Stars: ✭ 303 (-55.04%)
Mutual labels:  async, asynchronous
Tornado Celery
Non-blocking Celery client for Tornado
Stars: ✭ 561 (-16.77%)
Mutual labels:  async, asynchronous
Microwebsrv2
The last Micro Web Server for IoTs (MicroPython) or large servers (CPython), that supports WebSockets, routes, template engine and with really optimized architecture (mem allocations, async I/Os). Ready for ESP32, STM32 on Pyboard, Pycom's chipsets (WiPy, LoPy, ...). Robust, efficient and documented!
Stars: ✭ 295 (-56.23%)
Mutual labels:  async, asynchronous
Net
Android上强大的网络请求
Stars: ✭ 344 (-48.96%)
Mutual labels:  rxjava, cache
Creed
Sophisticated and functionally-minded async with advanced features: coroutines, promises, ES2015 iterables, fantasy-land
Stars: ✭ 265 (-60.68%)
Mutual labels:  async, asynchronous
Transmittable Thread Local
📌 TransmittableThreadLocal (TTL), the missing Java™ std lib(simple & 0-dependency) for framework/middleware, provide an enhanced InheritableThreadLocal that transmits values between threads even using thread pooling components.
Stars: ✭ 4,678 (+594.07%)
Mutual labels:  async, asynchronous
nakadi-java
🌀 Client library for the Nakadi Event Broker (examples: http://bit.ly/njc-examples, site: https://dehora.github.io/nakadi-java/)
Stars: ✭ 29 (-95.7%)
Mutual labels:  rxjava, gson
json-serialization-benchmarking
Miscellaneous benchmarks for JSON serialization on JVM/Android
Stars: ✭ 48 (-92.88%)
Mutual labels:  serialization, gson
Nfx
C# Server UNISTACK framework [MOVED]
Stars: ✭ 379 (-43.77%)
Mutual labels:  serialization, cache
Zsh Async
Because your terminal should be able to perform tasks asynchronously without external tools!
Stars: ✭ 528 (-21.66%)
Mutual labels:  async, asynchronous

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 suit your needs.

Reservoir

Reservoir is a simple library for Android that allows you to easily serialize and cache your objects to disk using key/value pairs.

Download

Including in your project

Add the jcenter repository to your gradle build file if it's not already present:

repositories {
    jcenter()
}

Next, add Reservoir as a dependency:

dependencies {
    compile 'com.anupcowkur:reservoir:3.1.0'
}

Usage

Initialize

Reservoir uses the internal cache storage allocated to your app. Before you can do anything, you need to initialize Reservoir with the cache size.

try {
    Reservoir.init(this, 2048); //in bytes
} catch (IOException e) {
        //failure
}

If you want to pass in a custom GSON instance for whatever reason, you can do that too:

try {
    Reservoir.init(this, 2048, myGsonInstance);
} catch (IOException e) {
        //failure
}

The best place to do this initialization would be in your application's onCreate() method.

Since this library depends directly on DiskLruCache, you can refer that project for more info on the maximum size you can allocate etc.

Put stuff

You can put objects into Reservoir synchronously or asynchronously.

Async put will you give you a callback on completion:

//Put a simple object
Reservoir.putAsync("myKey", myObject, new ReservoirPutCallback() {
            @Override
            public void onSuccess() {
                //success
            }

            @Override
            public void onFailure(Exception e) {
                //error
            }
        });


//Put collection
List<String> strings = new ArrayList<String>();
strings.add("one");
strings.add("two");
strings.add("three");
Reservoir.putAsync("myKey", strings, new ReservoirPutCallback() {
            @Override
            public void onSuccess() {
                //success
            }

            @Override
            public void onFailure(Exception e) {
                //error
            }
        });        

synchronous put:

//Put a simple object
try {
    Reservoir.put("myKey", myObject);
} catch (IOException e) {
    //failure;
}

//Put collection
List<String> strings = new ArrayList<String>();
strings.add("one");
strings.add("two");
strings.add("three");
try {
    Reservoir.put("myKey", strings);
} catch (IOException e) {
    //failure;
}

Async put uses the standard AsyncTask provided by the Android framework.

Get Stuff

You can get stuff out of Reservoir synchronously or asynchronously as well.

Async get will give you a callback on completion:

//Get a simple object
Reservoir.getAsync("myKey", MyClass.class, new ReservoirGetCallback<MyClass>() {
            @Override
            public void onSuccess(MyClass myObject) {
                //success
            }

            @Override
            public void onFailure(Exception e) {
                //error
            }
        });

//Get collection
Type resultType = new TypeToken<List<String>>() {}.getType();
Reservoir.getAsync("myKey", resultType, new ReservoirGetCallback<List<String>>() {
            @Override
            public void onSuccess(List<String> strings) {
                //success
            }

            @Override
            public void onFailure(Exception e) {
                //error
            }
        });        

synchronous get:

//Get a simple object
try {
    Reservoir.get("myKey", MyClass.class);
} catch (IOException e) {
        //failure
}

//Get collection
Type resultType = new TypeToken<List<String>>() {}.getType();
try {
    Reservoir.get("myKey", resultType);
} catch (IOException e) {
        //failure
}

Check for existence

If you wish to know whether an object exists for the given key, you can use:

try {
    boolean objectExists = Reservoir.contains("myKey");
} catch (IOException e) {}

Delete Stuff

deleting stuff can also be synchronous or asynchronous.

Async delete will give you a callback on completion:

Reservoir.deleteAsync("myKey", new ReservoirDeleteCallback() {
            @Override
            public void onSuccess(MyClass myObject) {
                //success
            }

            @Override
            public void onFailure(Exception e) {
                //error
            }
        });

synchronous delete:

try {
    Reservoir.delete("myKey");
} catch (IOException e) {
        //failure
}

Clearing the cache

You can clear the entire cache at once if you want.

asynchronous clear:

Reservoir.clearAsync(new ReservoirClearCallback() {
            @Override
            public void onSuccess() {
                try {
                    assertEquals(0, Reservoir.bytesUsed());
                } catch (Exception e) {
                   
                }
            }

            @Override
            public void onFailure(Exception e) {
                
            }
        });

synchronous clear:

try {
    Reservoir.clear();
} catch (IOException e) {
        //failure
}

RxJava

Reservoir is down with RxJava! All the async methods have RxJava variants that return observables. These observables are scheduled on a background thread and observed on the main thread by default (you can change this easily by assigning your own schedulers and observers to the returned observable).

First, you'll need to add RxJava dependency to your app since Reservoir does not come bundled with it:

compile 'io.reactivex:rxandroid:<rxandroid-version>' - tested with v1.2.1
compile 'io.reactivex:rxjava:<rxjava-version>' - tested with v1.1.6

Then you can use the RxJava variants of all the regular Reservoir methods.

put:

//Put a simple object
Reservoir.putUsingObservable("myKey", myObject) returns Observable<Boolean>

//Put collection
List<String> strings = new ArrayList<String>();
strings.add("one");
strings.add("two");
strings.add("three");
Reservoir.putUsingObservable("myKey", strings) returns Observable<Boolean>

get:

//Get a simple object
Reservoir.getUsingObservable("myKey", MyClass.class) returns Observable<MyClass>

//Get collection
//Note : Rx observables return items one at a time. So even if you put in a complete collection, the items in the collection will be returned 
//one by one by the observable.
Type collectionType = new TypeToken<List<String>>() {}.getType();
Reservoir.getUsingObservable("myKey", String.class, collectionType) returns Observable<String>

delete:

Reservoir.deleteUsingObservable("myKey") returns Observable<Boolean>

clear:

Reservoir.clearUsingObservable() returns Observable<Boolean>

If you'd like to see examples of using these observables, check out the tests in the sample application.

FAQs

What kind of objects can I add to Reservoir?

Anything that GSON can serialize.

What happens if my cache size is exceeded?

Older objects will be removed in a LRU (Least Recently Used) order.

Can I use this a SharedPreferences replacement?

NO! This is a cache. You should store stuff in here that is good to have around, but you wouldn't mind if they were to be removed. SharedPreferences are meant to store user preferences which is not something you want to lose.

Sample

Check out the sample application tests for complete examples of API usage.

Contributing

Contributions welcome via Github pull requests.

Credits

Reservoir is just a tiny little convenience wrapper around the following fantastic projects:

License

This project is licensed under the MIT License. Please refer the License.txt file.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].