Skip to content

Instantly share code, notes, and snippets.

@hardikm9850
Last active March 28, 2025 10:53
Show Gist options
  • Select an option

  • Save hardikm9850/64acc76d9cc15ae349863737e4597ba4 to your computer and use it in GitHub Desktop.

Select an option

Save hardikm9850/64acc76d9cc15ae349863737e4597ba4 to your computer and use it in GitHub Desktop.
// Creating cache folder where our response will be cached
val cacheFolder = Cache(File(applicationContext.cacheDir, "http-cache"), 10L * 1024L * 1024L) // 10 MB Cache
// Creating the okhttp builder passing the CacheInterceptor that adds "Cache-Control" in server's header
val okHttpClient = OkHttpClient().newBuilder()
.cache(cacheFolder)
.addNetworkInterceptor(CacheInterceptor()) // Only intercepts network responses (i.e. responses fetched from the server, not the cache)
.build()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment