Skip to content
This repository was archived by the owner on Jan 20, 2023. It is now read-only.

Commit 18c39c6

Browse files
committed
ConcurrentMapにして並列実行された時にキャッシュが壊れないよう修正
1 parent 409d102 commit 18c39c6

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/main/kotlin/com/mapk/kmapper/KMapper.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import com.mapk.core.getAliasOrName
88
import com.mapk.core.isUseDefaultArgument
99
import com.mapk.core.toKConstructor
1010
import java.lang.reflect.Method
11+
import java.util.concurrent.ConcurrentHashMap
12+
import java.util.concurrent.ConcurrentMap
1113
import kotlin.reflect.KClass
1214
import kotlin.reflect.KFunction
1315
import kotlin.reflect.KParameter
@@ -31,7 +33,7 @@ class KMapper<T : Any> private constructor(
3133
.filter { it.kind != KParameter.Kind.INSTANCE && !it.isUseDefaultArgument() }
3234
.associate { (parameterNameConverter(it.getAliasOrName()!!)) to ParameterForMap.newInstance(it) }
3335

34-
private val getCache: MutableMap<KClass<*>, List<(Any, ArgumentBucket) -> Unit>> = HashMap()
36+
private val getCache: ConcurrentMap<KClass<*>, List<(Any, ArgumentBucket) -> Unit>> = ConcurrentHashMap()
3537

3638
private fun bindArguments(argumentBucket: ArgumentBucket, src: Any) {
3739
val clazz = src::class
@@ -74,7 +76,7 @@ class KMapper<T : Any> private constructor(
7476
// キャッシュの整合性を保つため、ここでは終了判定を行わない
7577
}
7678
}
77-
getCache[clazz] = tempCacheArrayList
79+
getCache.putIfAbsent(clazz, tempCacheArrayList)
7880
}
7981

8082
private fun bindArguments(argumentBucket: ArgumentBucket, src: Map<*, *>) {

0 commit comments

Comments
 (0)