MicroUtils/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/cache/SimpleFullKVCache.kt

29 lines
815 B
Kotlin
Raw Normal View History

2022-06-29 17:53:49 +00:00
package dev.inmo.micro_utils.repos.cache.cache
import dev.inmo.micro_utils.repos.KeyValueRepo
import dev.inmo.micro_utils.repos.MapKeyValueRepo
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
2022-06-29 18:22:07 +00:00
open class SimpleFullKVCache<K, V>(
2022-06-29 17:53:49 +00:00
private val kvParent: KeyValueRepo<K, V> = MapKeyValueRepo<K, V>()
2022-06-29 18:22:07 +00:00
) : FullKVCache<K, V>, KeyValueRepo<K, V> by kvParent {
2022-06-29 17:53:49 +00:00
protected val syncMutex = Mutex()
override suspend fun set(toSet: Map<K, V>) {
syncMutex.withLock {
kvParent.set(toSet)
}
}
override suspend fun unset(toUnset: List<K>) {
syncMutex.withLock {
kvParent.unset(toUnset)
}
}
}
2022-06-29 20:44:44 +00:00
inline fun <K, V> FullKVCache(
kvParent: KeyValueRepo<K, V> = MapKeyValueRepo<K, V>()
) = SimpleFullKVCache<K, V>(kvParent)