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

Commit 09d6cbb

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

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.mapk.kmapper
22

33
import com.mapk.core.EnumMapper
4+
import java.util.concurrent.ConcurrentHashMap
5+
import java.util.concurrent.ConcurrentMap
46
import kotlin.reflect.KClass
57
import kotlin.reflect.KFunction
68
import kotlin.reflect.KParameter
@@ -13,7 +15,7 @@ internal class ParameterForMap<T : Any> private constructor(val param: KParamete
1315
// リストの長さが小さいと期待されるためこの形で実装しているが、理想的にはmap的なものが使いたい
1416
private val converters: Set<Pair<KClass<*>, KFunction<T>>> = clazz.getConverters()
1517

16-
private val convertCache: MutableMap<KClass<*>, (Any) -> Any?> = HashMap()
18+
private val convertCache: ConcurrentMap<KClass<*>, (Any) -> Any?> = ConcurrentHashMap()
1719

1820
fun <U : Any> mapObject(value: U): Any? {
1921
val valueClazz: KClass<*> = value::class
@@ -38,7 +40,7 @@ internal class ParameterForMap<T : Any> private constructor(val param: KParamete
3840
clazz == String::class -> { { it.toString() } }
3941
else -> throw IllegalArgumentException("Can not convert $valueClazz to $clazz")
4042
}
41-
convertCache[valueClazz] = lambda
43+
convertCache.putIfAbsent(valueClazz, lambda)
4244
return lambda(value)
4345
}
4446

0 commit comments

Comments
 (0)