Last active
September 7, 2018 06:57
-
-
Save bapspatil/eff374baf8c0aec91c64dd9865a64b65 to your computer and use it in GitHub Desktop.
OkHttpClient (Cache-enabled)
This file contains hidden or 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
| val cacheSize = (5 x 1024 x 1024).toLong() | |
| val myCache = Cache(context.cacheDir, cacheSize) | |
| val okHttpClient = OkHttpClient.Builder() | |
| .cache(myCache) | |
| .addInterceptor { chain -> | |
| var request = chain.request() | |
| request = if (hasNetwork(context)!!) | |
| request.newBuilder().header("Cache-Control", "public, max-age=" + 5).build() | |
| else | |
| request.newBuilder().header("Cache-Control", "public, only-if-cached, max-stale=" + 60 * 60 * 24 * 7).build() | |
| chain.proceed(request) | |
| } | |
| .build() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment