我正在使用来自网络电话的Retrofit库.非常棒.但我缺少缓存支持.我无法在HTTP层上使用缓存(通过缓存头).目前,我正在使用ObjectCache实现自定义缓存,但它太复杂了.它应该是令人敬畏的扩展当前Retrofit与@Cache(Expire.ONE_DAY)anotation.
我目前的代码如下:
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) { …Run Code Online (Sandbox Code Playgroud)