Skip to content

Commit

Permalink
Use ConcurrentHashMap for JVM target.
Browse files Browse the repository at this point in the history
  • Loading branch information
ychescale9 committed Jul 6, 2023
1 parent 933de3f commit d824daa
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 7 deletions.
3 changes: 2 additions & 1 deletion cache4k/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ kotlin {
implementation(libs.atomicfu)
}
}
val jvmMain by getting
val nonJvmMain by getting {
dependencies {
dependsOn(commonMain)
Expand All @@ -30,7 +31,7 @@ kotlin {
}
val jvmLincheck by getting {
dependencies {
dependsOn(commonMain)
dependsOn(jvmMain)
implementation(kotlin("test-junit5"))
implementation(libs.lincheck)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.github.reactivecircus.cache4k

internal expect class ConcurrentMutableMap<Key : Any, Value : Any>() {
val size: Int
val values: Collection<Value>
operator fun get(key: Key): Value?
fun put(key: Key, value: Value): Value?
fun remove(key: Key): Value?
fun clear()
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.reactivecircus.cache4k

import co.touchlab.stately.collections.IsoMutableMap
import kotlinx.atomicfu.locks.reentrantLock
import kotlinx.atomicfu.locks.withLock
import kotlinx.coroutines.sync.Mutex
Expand All @@ -11,7 +10,7 @@ import kotlinx.coroutines.sync.withLock
*/
internal class KeyedSynchronizer<Key : Any> {

private val keyBasedMutexes = IsoMutableMap<Key, MutexEntry>()
private val keyBasedMutexes = ConcurrentMutableMap<Key, MutexEntry>()

private val mapLock = reentrantLock()

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

return mutexEntry.mutex
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.reactivecircus.cache4k

import co.touchlab.stately.collections.IsoMutableMap
import co.touchlab.stately.collections.IsoMutableSet
import kotlinx.atomicfu.AtomicRef
import kotlinx.atomicfu.atomic
Expand Down Expand Up @@ -38,7 +37,7 @@ internal class RealCache<Key : Any, Value : Any>(
private val eventListener: CacheEventListener<Key, Value>?,
) : Cache<Key, Value> {

private val cacheEntries = IsoMutableMap<Key, CacheEntry<Key, Value>>()
private val cacheEntries = ConcurrentMutableMap<Key, CacheEntry<Key, Value>>()

/**
* Whether to perform size based evictions.
Expand Down Expand Up @@ -138,7 +137,7 @@ internal class RealCache<Key : Any, Value : Any>(
writeTimeMark = atomic(nowTimeMark),
)
recordWrite(newEntry)
cacheEntries[key] = newEntry
cacheEntries.put(key, newEntry)
}
onEvent(
oldValue?.let {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.github.reactivecircus.cache4k

import java.util.concurrent.ConcurrentHashMap

internal actual class ConcurrentMutableMap<Key : Any, Value : Any> {
private val map = ConcurrentHashMap<Key, Value>()

actual val size: Int
get() = map.size

actual val values: Collection<Value>
get() = map.values

actual operator fun get(key: Key): Value? {
return map[key]
}

actual fun put(key: Key, value: Value): Value? {
return map.put(key, value)
}

actual fun remove(key: Key): Value? {
return map.remove(key)
}

actual fun clear() {
map.clear()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.github.reactivecircus.cache4k

import co.touchlab.stately.collections.IsoMutableMap

internal actual class ConcurrentMutableMap<Key : Any, Value : Any> {
private val map = IsoMutableMap<Key, Value>()

actual val size: Int
get() = map.size

actual val values: Collection<Value>
get() = map.values

actual operator fun get(key: Key): Value? {
return map[key]
}

actual fun put(key: Key, value: Value): Value? {
return map.put(key, value)
}

actual fun remove(key: Key): Value? {
return map.remove(key)
}

actual fun clear() {
map.clear()
}
}

0 comments on commit d824daa

Please sign in to comment.