11package io.github.kdroidfilter.composemediaplayer
22
33import io.github.kdroidfilter.composemediaplayer.util.TaggedLogger
4+ import kotlin.concurrent.AtomicInt
45import platform.Foundation.NSURLCache
56
67private val cacheLogger = TaggedLogger (" iOSVideoCache" )
@@ -18,13 +19,12 @@ private val cacheLogger = TaggedLogger("iOSVideoCache")
1819 * that allow caching.
1920 */
2021internal object IosVideoCache {
21- private var configured = false
22+ private val configuredFlag = AtomicInt ( 0 )
2223 private var previousMemoryCapacity: ULong = 0u
2324 private var previousDiskCapacity: ULong = 0u
2425
25- @Synchronized
2626 fun configure (maxCacheSizeBytes : Long ) {
27- if (configured ) return
27+ if (! configuredFlag.compareAndSet( 0 , 1 ) ) return
2828
2929 val sharedCache = NSURLCache .sharedURLCache
3030 previousMemoryCapacity = sharedCache.memoryCapacity
@@ -34,26 +34,22 @@ internal object IosVideoCache {
3434 sharedCache.memoryCapacity = maxOf(sharedCache.memoryCapacity, (10L * 1024 * 1024 ).toULong())
3535 sharedCache.diskCapacity = maxOf(sharedCache.diskCapacity, maxCacheSizeBytes.toULong())
3636
37- configured = true
3837 cacheLogger.d {
3938 " NSURLCache configured: disk=${sharedCache.diskCapacity} bytes, memory=${sharedCache.memoryCapacity} bytes"
4039 }
4140 }
4241
43- @Synchronized
4442 fun clear () {
4543 NSURLCache .sharedURLCache.removeAllCachedResponses()
4644 cacheLogger.d { " NSURLCache cleared" }
4745 }
4846
49- @Synchronized
5047 fun release () {
51- if (! configured ) return
48+ if (! configuredFlag.compareAndSet( 1 , 0 ) ) return
5249
5350 val sharedCache = NSURLCache .sharedURLCache
5451 sharedCache.memoryCapacity = previousMemoryCapacity
5552 sharedCache.diskCapacity = previousDiskCapacity
56- configured = false
5753 cacheLogger.d { " NSURLCache restored to previous configuration" }
5854 }
5955}
0 commit comments