Skip to content

Commit 86aa406

Browse files
authored
Use ConcurrentHashMap for JVM target (#39)
* Use `ConcurrentHashMap` for JVM target. * Fix `nonJvm` and `jvmLincheck` sourcesets.
1 parent d31c054 commit 86aa406

File tree

8 files changed

+61
-20
lines changed

8 files changed

+61
-20
lines changed

build-logic/gradle.properties

-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,3 @@ org.gradle.configuration-cache=true
55
org.gradle.configuration-cache.problems=warn
66

77
kotlin.code.style=official
8-
kotlin.experimental.tryK2=true

build-logic/src/main/kotlin/io/github/reactivecircus/cache4k/buildlogic/convention/ConventionPlugin.kt

+9-12
Original file line numberDiff line numberDiff line change
@@ -182,23 +182,20 @@ private fun KotlinMultiplatformExtension.configureTargets(project: Project) {
182182
mingwX64()
183183
applyDefaultHierarchyTemplate()
184184

185+
@Suppress("UnusedPrivateProperty")
185186
sourceSets {
186-
val jvmAndIos by creating {
187+
val nonJvmMain by creating {
187188
dependsOn(commonMain.get())
188189
}
189-
iosMain.get().dependsOn(jvmAndIos)
190-
jvmMain.get().dependsOn(jvmAndIos)
191-
192-
val nonJvm by creating {
193-
dependsOn(commonMain.get())
190+
val jvmLincheck by getting {
191+
dependsOn(jvmMain.get())
194192
}
195-
jsMain.get().dependsOn(nonJvm)
196-
@Suppress("UnusedPrivateProperty")
193+
jsMain.get().dependsOn(nonJvmMain)
197194
val wasmJsMain by getting {
198-
dependsOn(nonJvm)
195+
dependsOn(nonJvmMain)
199196
}
200-
appleMain.get().dependsOn(nonJvm)
201-
linuxMain.get().dependsOn(nonJvm)
202-
mingwMain.get().dependsOn(nonJvm)
197+
appleMain.get().dependsOn(nonJvmMain)
198+
linuxMain.get().dependsOn(nonJvmMain)
199+
mingwMain.get().dependsOn(nonJvmMain)
203200
}
204201
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package io.github.reactivecircus.cache4k
2+
3+
internal expect class ConcurrentMutableMap<Key : Any, Value : Any>() {
4+
val size: Int
5+
val values: Collection<Value>
6+
operator fun get(key: Key): Value?
7+
fun put(key: Key, value: Value): Value?
8+
fun remove(key: Key): Value?
9+
fun clear()
10+
}

cache4k/src/commonMain/kotlin/io/github/reactivecircus/cache4k/KeyedSynchronizer.kt

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.github.reactivecircus.cache4k
22

3-
import co.touchlab.stately.collections.IsoMutableMap
43
import kotlinx.atomicfu.locks.reentrantLock
54
import kotlinx.atomicfu.locks.withLock
65
import kotlinx.coroutines.sync.Mutex
@@ -11,7 +10,7 @@ import kotlinx.coroutines.sync.withLock
1110
*/
1211
internal class KeyedSynchronizer<Key : Any> {
1312

14-
private val keyBasedMutexes = IsoMutableMap<Key, MutexEntry>()
13+
private val keyBasedMutexes = ConcurrentMutableMap<Key, MutexEntry>()
1514

1615
private val mapLock = reentrantLock()
1716

@@ -40,7 +39,7 @@ internal class KeyedSynchronizer<Key : Any> {
4039
mutexEntry.counter++
4140
// save the lock entry to the map if it has just been created
4241
if (keyBasedMutexes[key] == null) {
43-
keyBasedMutexes[key] = mutexEntry
42+
keyBasedMutexes.put(key, mutexEntry)
4443
}
4544

4645
return mutexEntry.mutex

cache4k/src/commonMain/kotlin/io/github/reactivecircus/cache4k/RealCache.kt

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.github.reactivecircus.cache4k
22

3-
import co.touchlab.stately.collections.IsoMutableMap
43
import co.touchlab.stately.collections.IsoMutableSet
54
import kotlinx.atomicfu.AtomicRef
65
import kotlinx.atomicfu.atomic
@@ -38,7 +37,7 @@ internal class RealCache<Key : Any, Value : Any>(
3837
private val eventListener: CacheEventListener<Key, Value>?,
3938
) : Cache<Key, Value> {
4039

41-
private val cacheEntries = IsoMutableMap<Key, CacheEntry<Key, Value>>()
40+
private val cacheEntries = ConcurrentMutableMap<Key, CacheEntry<Key, Value>>()
4241

4342
/**
4443
* Whether to perform size based evictions.
@@ -138,7 +137,7 @@ internal class RealCache<Key : Any, Value : Any>(
138137
writeTimeMark = atomic(nowTimeMark),
139138
)
140139
recordWrite(newEntry)
141-
cacheEntries[key] = newEntry
140+
cacheEntries.put(key, newEntry)
142141
}
143142
onEvent(
144143
oldValue?.let {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.github.reactivecircus.cache4k
2+
3+
import java.util.concurrent.ConcurrentHashMap
4+
5+
internal actual class ConcurrentMutableMap<Key : Any, Value : Any> {
6+
private val map = ConcurrentHashMap<Key, Value>()
7+
8+
actual val size: Int get() = map.size
9+
10+
actual val values: Collection<Value> get() = map.values
11+
12+
actual operator fun get(key: Key): Value? = map[key]
13+
14+
actual fun put(key: Key, value: Value): Value? = map.put(key, value)
15+
16+
actual fun remove(key: Key): Value? = map.remove(key)
17+
18+
actual fun clear() = map.clear()
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.github.reactivecircus.cache4k
2+
3+
import co.touchlab.stately.collections.IsoMutableMap
4+
5+
internal actual class ConcurrentMutableMap<Key : Any, Value : Any> {
6+
private val map = IsoMutableMap<Key, Value>()
7+
8+
actual val size: Int get() = map.size
9+
10+
actual val values: Collection<Value> get() = map.values
11+
12+
actual operator fun get(key: Key): Value? = map[key]
13+
14+
actual fun put(key: Key, value: Value): Value? = map.put(key, value)
15+
16+
actual fun remove(key: Key): Value? = map.remove(key)
17+
18+
actual fun clear() = map.clear()
19+
}

gradle.properties

-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,3 @@ org.gradle.configureondemand=true
1818
org.gradle.caching=true
1919

2020
kotlin.code.style=official
21-
kotlin.experimental.tryK2=false

0 commit comments

Comments
 (0)