I am trying to cache the http response through OKhttp but unable to find some good example or documentation.
Thanks for any help
this will cache all your response for 2 minutes
OkHttpClient provideOkHttpClient () {
Cache cache = new Cache(new File(context.getCacheDir(), "http-cache"), 10 * 1024 * 1024);
return new OkHttpClient.Builder()
.addNetworkInterceptor(provideCacheInterceptor())
.cache(cache)
.build();
}
Interceptor provideCacheInterceptor () {
return new Interceptor() {
@Override
public Response intercept (Chain chain) throws IOException {
Response response = chain.proceed( chain.request() );
CacheControl cacheControl = new CacheControl.Builder()
.maxAge( 2, TimeUnit.MINUTES )
.build();
return response.newBuilder()
.header("Cache-Control", cacheControl.toString() )
.build();
}
};
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With