Created
December 12, 2019 09:33
-
-
Save lzh77/0fa9b9ad248ca785e851d160c2ac958c to your computer and use it in GitHub Desktop.
okhttp的缓存拦截器实现 CacheInterceptor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package nyc.friendlyrobot.demo.util; | |
import android.support.annotation.NonNull; | |
import java.io.IOException; | |
import javax.inject.Inject; | |
import okhttp3.CacheControl; | |
import okhttp3.Interceptor; | |
import okhttp3.Request; | |
import okhttp3.Response; | |
/** | |
* Created by 206847 on 1/16/16. | |
*/ | |
public class CacheInterceptor implements Interceptor { | |
private final NetworkStatus networkStatus; | |
@Inject | |
public CacheInterceptor(NetworkStatus networkStatus) { | |
this.networkStatus = networkStatus; | |
} | |
@Override | |
public Response intercept(Chain chain) throws IOException { | |
Request originalRequest = chain.request(); | |
Request.Builder request = originalRequest.newBuilder(); | |
if (originalRequest.header("fresh") != null) { | |
request.cacheControl(CacheControl.FORCE_NETWORK); | |
} | |
Response response = chain.proceed(request.build()); | |
return response.newBuilder() | |
.removeHeader("Pragma") | |
.removeHeader("Cache-Control") | |
.header("Cache-Control", cacheControl()) | |
.build(); | |
} | |
@NonNull | |
private String cacheControl() { | |
String cacheHeaderValue; | |
if (networkStatus.isOnGoodConnection()) { | |
cacheHeaderValue = "public, max-age=2419200"; | |
} else { | |
cacheHeaderValue = "public, only-if-cached, max-stale=2419200"; | |
} | |
return cacheHeaderValue; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment