Repository: expo/expo
File: packages/expo-video/android/src/main/java/expo/modules/video/VideoCache.kt (Kotlin)

Code before:
      SimpleCache(getCacheDir(), cacheEvictor, databaseProvider)
    }
  }

  fun release() {
    instance.release()
  }

  fun setMaxCacheSize(size: Long) {
    assertModificationReleaseConditions()
    instance.release()
    sharedPreferences.edit().putLong(CACHE_SIZE_KEY, size).apply()
    cacheEvictor = LeastRecentlyUsedCacheEvictor(size)
    instance = SimpleCache(getCacheDir(), cacheEvictor, databaseProvider)
  }

  fun getCurrentCacheSize(): Long {
    return getFileSize(getCacheDir())
  }

  // We have to keep the current directory name in the shared preferences, when the cache is released, a new name will be assigned to avoid conflicts.
  private fun getCacheDir(): File {
    // Weird structure, because kotlin marks the result of `getString` as nullable
    val videoCacheDirName = sharedPreferences.getString(VIDEO_CACHE_DIR_KEY, null) ?: run {
      val newCacheDirName = generateCacheDirName()
      // Using commit() instead of apply() to ensure synchronous write and avoid race conditions
      sharedPreferences.edit().putString(VIDEO_CACHE_DIR_KEY, newCacheDirName).commit()
      newCacheDirName
    }
    val cacheParentDir = File(context.cacheDir, VIDEO_CACHE_PARENT_DIR)
    val cacheDir = File(cacheParentDir, videoCacheDirName)

    if (!cacheDir.exists()) {
      cacheDir.mkdirs()
    }
    return cacheDir
  }

  private fun generateCacheDirName(): String {
    return UUID.randomUUID().toString()
  }

  fun clear() {
    assertModificationReleaseConditions()

    // Creates a new cache directory to avoid conflicts while removing the old cache
    val oldCacheDirectory = getCacheDir()
    val oldCache = instance
    val newCacheName = generateCacheDirName()

    sharedPreferences.edit().putString(VIDEO_CACHE_DIR_KEY, newCacheName).apply()

Review comment (suggestion):
The change is ok, I don't like the comment though
```suggestion
```

Code after:
    return getFileSize(getCacheDir())
  }

  // We have to keep the current directory name in the shared preferences, when the cache is released, a new name will be assigned to avoid conflicts.
  private fun getCacheDir(): File {
    // Weird structure, because kotlin marks the result of `getString` as nullable
    val videoCacheDirName = sharedPreferences.getString(VIDEO_CACHE_DIR_KEY, null) ?: run {
      val newCacheDirName = generateCacheDirName()
      sharedPreferences.edit().putString(VIDEO_CACHE_DIR_KEY, newCacheDirName).commit()
      newCacheDirName
    }
    val cacheParentDir = File(context.cacheDir, VIDEO_CACHE_PARENT_DIR)
    val cacheDir = File(cacheParentDir, videoCacheDirName)

    if (!cacheDir.exists()) {
      cacheDir.mkdirs()
    }
    return cacheDir
  }

  private fun generateCacheDirName(): String {
    return UUID.randomUUID().toString()
  }

  fun clear() {
    assertModificationReleaseConditions()

    // Creates a new cache directory to avoid conflicts while removing the old cache
    val oldCacheDirectory = getCacheDir()
    val oldCache = instance
    val newCacheName = generateCacheDirName()

    sharedPreferences.edit().putString(VIDEO_CACHE_DIR_KEY, newCacheName).apply()
    instance = SimpleCache(getCacheDir(), cacheEvictor, databaseProvider)
    oldCache.release()
    oldCacheDirectory.deleteRecursively()
  }

  private fun getFileSize(file: File): Long {
    return file
      .walkTopDown()
      .filter { it.isFile }
      .map { it.length() }
      .sum()
  }

  private fun assertModificationReleaseConditions() {
    if (VideoManager.hasRegisteredPlayers()) {
      throw VideoCacheException("Cannot clear cache while there are active players")
    }