This library provide an easy way for configure retrofit with a 2 layer cache (RAM and Disk). To see more details about the cache used visit DualCache
This allow you to improve the data usage of your apps.
repositories {
maven { url 'https://jitpack.io' }
}
android {
packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
}
}
dependencies {
compile 'com.github.iagocanalejas:retrocache:
# Usage
1. In your Service Api Interface change all `Call<T>` return types for `CachedCall<T>`
```java
public interface ApiService {
@GET("/")
Cached<MyObject> getResource();
}
Build your cache. Library include some shortcuts for this tasks but you can always build a full configured cache using DualCache. Just remember cache key type must be String
and entry type must be byte[]
Basic cache using only RAM.
DualCache<String, byte[]> mCache = RetroCache.getRamCache(APP_VERSION);
Basic cache using both, Disk and Ram, layers.
DualCache<String, byte[]> mCache = RetroCache.getDualCache(context, APP_VERSION);
Basic cache using both, Disk and Ram, layers and setting a life time for entries.
DualCache<String, byte[]> mCache = RetroCache.getVolatileCache(context, APP_VERSION);
You can also get a non configured Builder.
Builder<String, byte[]> builder = RetroCache.getBuilder(APP_VERSION);
For see default values in this caches take a look at RetroCache
Important
Add the cache to your Retrofit service with one of the following methods.
retrofitBuilder.addCallAdapterFactory(CachedCallAdapterFactory.create(cache));
retrofitBuilder.addCallAdapterFactory(CachedCallAdapterFactory.create(context, APP_VERSION));
retrofitBuilder.addCallAdapterFactory(CachedCallAdapterFactory.createWithExecutor(cache, executor));
Use it as normal retrofit. Just remember to use Cached
. All retrofit methods are included, and you can also use methods explained in Included
section.
In addition to normal retrofit usage you can also call refresh(callback)
to avoid looking in the cache or remove()
to invalidate a cached call.
```java
Cached<MyObject> call = ...
call.refresh(new Callback<MyObject>() {
...
});
call.remove();
```
RxJava2 adapter is still in beta. You can use it as normal retrofit rxjava2 adapter just add your adapter like:
```java
retrofitBuilder.addCallAdapterFactory(RxJava2CachedCallAdapterFactory.create(cache));
retrofitBuilder.addCallAdapterFactory(RxJava2CachedCallAdapterFactory.create(context, APP_VERSION));
retrofitBuilder.addCallAdapterFactory(RxJava2CachedCallAdapterFactory.createAsync(cache));
retrofitBuilder.addCallAdapterFactory(RxJava2CachedCallAdapterFactory.createAsync(context, APP_VERSION));
retrofitBuilder.addCallAdapterFactory(RxJava2CachedCallAdapterFactory.createWithScheduler(cache, scheduler));
retrofitBuilder.addCallAdapterFactory(RxJava2CachedCallAdapterFactory.createWithScheduler(context, APP_VERSION, scheduler));
```
Don't forget to include dependency:
dependencies {
compile 'com.github.iagocanalejas:retrocache-rxjava2:<VERSION>'
}
I welcome and encourage all pull requests. Here are some basic rules to follow to ensure timely addition of your request:
gradlew check
for styling, and gradlew test
for unit tests.develop
branch. Any other branch (unless specified by the maintainers) will get rejected.IagoCanalejas (@iagocanalejas)
Copyright 2016 IagoCanalejas.
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.