2

Deps:

  • Kotlin 1.0.3
  • Exoplayer r1.5.9
  • Retrofit 2.1.0
  • okhttp 3.4.1

Im trying to setup cache for my video playback project (list of videos of 20 secs per video) and Im wondering if theres any problem if I set the cache for OkHttp

val cacheSize: Long = 1000 * 1024 * 1024 // 1000 MB  <-------- HERE
val cookieManager = CookieManager()

cookieManager.setCookiePolicy(java.net.CookiePolicy.ACCEPT_ORIGINAL_SERVER)

return OkHttpClient.Builder()
  .cache(Cache(File(cacheDir, "responses"), cacheSize))
  .cookieJar(JavaNetCookieJar(cookieManager))
  • May have a Exceptions if I try to use 1000 MB for cache?
  • How can I find the best cache size for my app without having issues?
Daniel Gomez Rico
  • 15,026
  • 20
  • 92
  • 162

1 Answers1

3
  1. Looking at the Cache class, it doesn't do pre-allocation of the files, so your 1000MB will pass without any exception BUT that doesn't mean it will work if the device doesn't even have that kind of empty space around, which means ...

  2. When it runs out of space when saving file, it will purge files according to the LRU. a

  3. No one can answer your 'best' cache size since it depends on your use case. And you said "without having issues"; what issues are you afraid of? Once a video file has cached, will that likely to change? How often will it change? On average how large is the video file? For segmented files (e.g. HLS), do you want to keep use the default LRU?

  4. example: let's say you're doing HLS, each chunk is 500k, so on average, you can save 2000 chunks.

a: DiskLruCache.java from okhttp3 (Note it may change overtime, just search for it if link is broken)

Brian Chu
  • 113
  • 9