改造http缓存控制检查发送回缓存,即使过时并再次进行网络呼叫

时间:2016-09-08 11:01:10

标签: android retrofit okhttp cache-control

我正在使用Retrofit 1.9。+和okhttp:2.2。+。

我想缓存我的回复。我用Okclient拦截器做了如下:

public abstract class RestController {
private static Context mContext;
private static long SIZE_OF_CACHE = 10 * 1024 * 1024; // 10 MB

public static RestAdapter mRestAdapter;

public static void init(final Context context, String baseAPIUrl) {
    mContext = context;

    // Create Cache
    Cache cache = null;
    try {
        cache = new Cache(new File(mContext.getCacheDir(), "http"), SIZE_OF_CACHE);
    } catch (IOException e) {
        Log.e(RestController.class.getSimpleName(), "Could not create Cache!", e);
    }

    // Create OkHttpClient
    OkHttpClient okHttpClient = new OkHttpClient();
    okHttpClient.setCache(cache);
    okHttpClient.setConnectTimeout(30, TimeUnit.SECONDS);
    okHttpClient.setReadTimeout(30, TimeUnit.SECONDS);

    // Add Cache-Control Interceptor
    okHttpClient.networkInterceptors().add(mCacheControlInterceptor);

    // Create Executor
    Executor executor = Executors.newCachedThreadPool();

    mRestAdapter = new RestAdapter.Builder()
            .setEndpoint(baseAPIUrl)
            .setExecutors(executor, executor)
            .setClient(new OkClient(okHttpClient))
            .setLogLevel(RestAdapter.LogLevel.FULL)
            .build();
}

private static final Interceptor mCacheControlInterceptor = new Interceptor() {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();

        // Add Cache Control only for GET methods
        if (request.method().equals("GET")) {
            if (isNetworkAvailable()) {
                // 1 day
                request.newBuilder()
                        .header("Cache-Control", "only-if-cached")
                        .build();
            } else {
                // 4 weeks stale
                request.newBuilder()
                        .header("Cache-Control", "public, max-stale=2419200")
                        .build();
            }
        }

        Response response = chain.proceed(request);

        // Re-write response CC header to force use of cache
        return response.newBuilder()
                .header("Cache-Control", "public, max-age=86400") // 1 day
                .build();
    }
};

public static Boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if (networkInfo == null) {
        return false;
    }
    NetworkInfo.State network = networkInfo.getState();
    return (network == NetworkInfo.State.CONNECTED || network == NetworkInfo.State.CONNECTING);
}

}

现在我需要在回放缓存时实现逻辑。我的缓存需要有两个指标: 1 fresh_upto 2扔掉

然后逻辑就像     if(cache is present and current time <live upto){ send back cache } else if(current time<throwawaytime and current time >live upto){ send back cache make another network call onresponse send the new data back }

如何通过缓存控制实现这一目标。我可以使用max-age或max-stale吗?

1 个答案:

答案 0 :(得分:1)

尝试使用这个进行改造,它在我的项目中工作

@Headers("Cache-Control: max-age=" + (MINUTE)) @GET("your Url") Call<ResponseBody> getCourseStructureFromCache("Your Request paramater and all");