I'm using Retrofit library from network calls. Pretty awesome. But I'm missing caching support. I can't using cache on HTTP layer (via Cache headers). Currently, I'm implementing custom caching with ObjectCache, but it's so complicated. It just should be awesome extend current Retrofit with @Cache(Expire.ONE_DAY)
anotation.
My current code is like:
public static void getRestaurant(int restaurantId, String token, boolean forceNetwork, final Callback<Restaurant> listener) {
final String key = "getRestaurant-" + restaurantId + "-" + token;
Restaurant restaurant = (Restaurant) getCacheManager().get(key, Restaurant.class, new TypeToken<Restaurant>() {}.getType());
if (restaurant != null && !forceNetwork) {
Log.d(TAG, "Cache hit: " + key);
// Cache
listener.success(restaurant);
} else {
Log.d(TAG, "Network: " + key);
// Retrofit
getNetwork().getRestaurant(restaurantId, token, new retrofit.Callback<Response>() {
@Override
public void success(Response response, retrofit.client.Response response2) {
getCacheManager().put(key, response.result.restaurant, CacheManager.ExpiryTimes.ONE_HOUR.asSeconds(), true);
listener.success(response.result.restaurant);
}
@Override
public void failure(RetrofitError error) {
listener.failure(error.getLocalizedMessage());
}
});
}
}
Now, it's just a lot of boilerplate code for each method.
Or do you know any library like Retrofit with caching support?
Thanks!