[老实李] Retrofit2+OKHttp3实现缓存

首先需要注意的是:使用OKHttp特有的方法只能对GET请求进行缓存。(我自己就在这个坑爬了很久,因为我们公司的所有接口全部是POST请求)。如果想要对POST请求进行缓存的话还需要进一步的处理。这一点从OKHttp的源码中也可以找到答案。(Cache类中的put方法,当请求方法不是GET的时候直接返回一个null)

private CacheRequest put(Response response) throws IOException {
  String requestMethod = response.request().method();

  if (HttpMethod.invalidatesCache(response.request().method())) {
    try {
      remove(response.request());
    } catch (IOException ignored) {
      // The cache cannot be written.
    }
    return null;
  }
  if (!requestMethod.equals("GET")) {
    // Don't cache non-GET responses. We're technically allowed to cache
    // HEAD requests and some POST requests, but the complexity of doing
    // so is high and the benefit is low.
    return null;
  }

  if (OkHeaders.hasVaryAll(response)) {
    return null;
  }

  Entry entry = new Entry(response);
  DiskLruCache.Editor editor = null;
  try {
    editor = cache.edit(urlToKey(response.request()));
    if (editor == null) {
      return null;
    }
    entry.writeTo(editor);
    return new CacheRequestImpl(editor);
  } catch (IOException e) {
    abortQuietly(editor);
    return null;
  }
}

好,下面我们就分别来看在GET和POST方法中缓存的实现。

一、GET缓存

1、首先定义两个Interceptor,分别作为有网络时的拦截器和无网络时的拦截器。关于拦截器的自定义,网上到处都是,我这里就写了两个最简单的。

public class NoNetInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {

    Request request = chain.request();
    boolean connected = HttpNetUtil.isNetConnected(ETApplication.getInstance());
    if (!connected) {
        request = request.newBuilder()
                .cacheControl(CacheControl.FORCE_CACHE)
                .build();
        Log.e("etNet", "have not network and to read local cache");
        Response response = chain.proceed(request);
        return response.newBuilder()
                .header("Cache-Control", "public, only-if-cached, max-stale=3600")
                .removeHeader("Pragma")
                .build();
    }
    return chain.proceed(request);

}

}

public class NetInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
boolean connected = HttpNetUtil.isNetConnected(ETApplication.getInstance());
if(connected){
//if have network will cache 90s
Log.e("etNet","online cache 90s");
Response response = chain.proceed(request);
int maxTime = 90;
return response.newBuilder()
.removeHeader("Pragma")
.header("Cache-Control", "public, max-age=" + maxTime)
.build();
}
//if have not network will immediate return
return chain.proceed(request);
}

}

2、在Retrofit中使用

public class RetrofitUtil {
    private static Retrofit retrofit;
    //over time
    private static final long DUFAULT_TIME_OUT = 5;
    /**
    * @return retrofit instance
    */
    public static Retrofit getRetrofitInstance() {
        if (retrofit == null) {
            File file = new File(ETApplication.getInstance().getApplicationContext().getCacheDir(), "rxCache");
            //cache size 10M
            int cacheSize = 10 * 1024 * 1024;
            Cache cache = new Cache(file, cacheSize);
            OkHttpClient client = new OkHttpClient.Builder()
                    .cache(cache)
                    .addInterceptor(new NoNetInterceptor())
                    .addNetworkInterceptor(new NetInterceptor())
                    .connectTimeout(DUFAULT_TIME_OUT, TimeUnit.SECONDS) //连接超时
                    .writeTimeout(DUFAULT_TIME_OUT, TimeUnit.SECONDS)  //写入超时
                    .readTimeout(DUFAULT_TIME_OUT, TimeUnit.SECONDS)  //读取超时
                    .build();
            retrofit = new Retrofit.Builder()
                    .client(client)
                    .baseUrl(Urls.severURL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();

        }
        return retrofit;
    }

    /**
    * @return interface instance
    *
    */
    public static APIRetrofit getAPIRetrofitInstance() {
        return getRetrofitInstance().create(APIRetrofit.class);
    }

}

二、POST请求缓存

我的做法是用GreenDao来进行缓存。
拦截器参考:http://blog.csdn.net/u010429311/article/details/59116706
GreenDao参考:http://www.jianshu.com/p/4986100eff90

public class RewriteCacheControlInterceptor implements Interceptor {

    private EtCacheDao mEtCacheDao;

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        String url = request.toString();
        Log.e("OkHttp","url:"+url);
        Buffer buffer = new Buffer();
        request.body().writeTo(buffer);
        String params = buffer.readString(Charset.forName("UTF-8"));
        Log.e("OkHttp","params:"+params);
        mEtCacheDao = ETApplication.getInstance().getDaoSession().getEtCacheDao();
        Response response;
        if (HttpNetUtil.isNetConnected(ETApplication.getInstance())) {
            int maxAge = 60 * 60*24;
            Response originalResponse = chain.proceed(request);
            MediaType type = originalResponse.body().contentType();
            byte[] bs = originalResponse.body().bytes();
            response = originalResponse.newBuilder()
                    .removeHeader("Pragma")
                    .removeHeader("Cache-Control")
                    .header("Cache-Control", "public, max-age=" + maxAge)
                    .body(ResponseBody.create(type, bs))
                    .build();
            Log.e("OKHttp","response:"+new String(bs,"UTF-8"));
            EtCache etCache = new EtCache(url+params, new String(bs, "UTF-8"));
            List list = mEtCacheDao.queryBuilder()
                    .where(EtCacheDao.Properties.UrlAndParams.eq(url+params))
                    .list();
            if (list==null||list.size()==0) {
                mEtCacheDao.insert(etCache);
            }

        } else {
            String b =  "";
            List list = mEtCacheDao.queryBuilder()
                    .where(EtCacheDao.Properties.UrlAndParams.eq(url+params))
                    .list();
            if (list != null && list.size() > 0) {
                b = list.get(0).getResponse();
            }

            Log.e("OkHttp", "request:" + url);
            Log.e("OkHttp", "request method:" + request.method());
            Log.e("OkHttp", "response body:" + b);
            int maxStale = 60 * 60 * 24 * 28;
            response = new Response.Builder()
                    .removeHeader("Pragma")
                    .header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
                    .body(ResponseBody.create(MediaType.parse("application/json"), b.getBytes()))
                    .request(request)
                    .protocol(Protocol.HTTP_1_1)
                    .code(200)
                    .build();
        }
        return response;
    }
}

你可能感兴趣的:([老实李] Retrofit2+OKHttp3实现缓存)