Bucket is a disk cache library for Android. You can use it to cache any object that can be serialized to json.
DiskLruCache by Jake Wharton is used as the underlying cache.
- get
- put
- contains
- remove
- clear
Bucket contains synchronous, async and Rx methods for all operations.
Add using Gradle:
compile 'com.github.simonpercic:bucket:1.0.0'
Create a singleton instance using a builder()
// create a singleton instance using a builder()
int maxSizeBytes = 1024 * 1024;
Bucket bucket = Bucket.builder(context, maxSizeBytes).build();
You can also pass in a custom Gson instance, if you wish to do so:
// create a singleton instance using a builder()
Gson gson = ...
Bucket.builder(context, maxSizeBytes).withGson(gson).build();
// sync
MyObject object = bucket.get("key", MyObject.class);
// async
bucket.getAsync("key", MyObject.class, new BucketGetCallback<MyObject>() {
@Override public void onSuccess(MyObject object) {
}
@Override public void onFailure(Throwable throwable) {
}
});
// Rx
Observable<MyObject> observable = bucket.getRx("key", MyObject.class);
// sync
bucket.put("key", object);
// async
bucket.putAsync("key", object, new BucketCallback() {
@Override public void onSuccess() {
}
@Override public void onFailure(Throwable throwable) {
}
});
// Rx
Observable<Boolean> observable = bucket.putRx("key", object);
// sync
boolean contains = bucket.contains("key");
// async
bucket.containsAsync("key", new BucketGetCallback<Boolean>() {
@Override public void onSuccess(Boolean contains) {
}
@Override public void onFailure(Throwable throwable) {
}
});
// Rx
Observable<Boolean> observable = bucket.containsRx("key");
// sync
bucket.remove("key");
// async
bucket.removeAsync("key", new BucketCallback() {
@Override public void onSuccess() {
}
@Override public void onFailure(Throwable throwable) {
}
});
// Rx
Observable<Boolean> observable = bucket.removeRx("key");
// sync
bucket.clear();
// async
bucket.clearAsync(new BucketCallback() {
@Override public void onSuccess() {
}
@Override public void onFailure(Throwable throwable) {
}
});
// Rx
Observable<Boolean> observable = bucket.clearRx();
Bucket fully supports Generics and Collections by passing a custom Type instance created through Gson:
// generics
public class GenericObject<T> {
T object;
String value;
}
Type genericType = new TypeToken<GenericObject<MyObject>>(){}.getType();
GenericObject<MyObject> object = bucket.get("key", genericType);
// collections
Type collectionType = new TypeToken<List<MyObject>>() {}.getType();
List<MyObject> list = bucket.get("key", collectionType);
Bucket depends on the following awesome open source projects:
- store any kind of object, as long as it is json-serializable
- relies on RxJava and RxAndroid schedulers for threading
- supports generics and collections
- unit and android test coverage
- checkstyle, findbugs, pmd and lint static code analysis checks
Check out the androidTest directory for practical examples.
Open source, distributed under the MIT License. See LICENSE for details.