actualize all make it possible to disable clear on actualization

This commit is contained in:
2023-02-02 20:07:31 +06:00
parent 168d6acf7c
commit 269c2876f3

View File

@@ -11,35 +11,41 @@ import dev.inmo.micro_utils.repos.pagination.getAll
import dev.inmo.micro_utils.repos.set import dev.inmo.micro_utils.repos.set
suspend inline fun <K, V> KVCache<K, V>.actualizeAll( suspend inline fun <K, V> KVCache<K, V>.actualizeAll(
clear: Boolean = true,
getAll: () -> Map<K, V> getAll: () -> Map<K, V>
) { ) {
set( set(
getAll().also { getAll().also {
clear() if (clear) {
clear()
}
} }
) )
} }
suspend inline fun <K, V> KVCache<K, V>.actualizeAll( suspend inline fun <K, V> KVCache<K, V>.actualizeAll(
repo: ReadKeyValueRepo<K, V> repo: ReadKeyValueRepo<K, V>,
clear: Boolean = true,
) { ) {
actualizeAll { actualizeAll(clear) {
repo.getAll { keys(it) }.toMap() repo.getAll { keys(it) }.toMap()
} }
} }
suspend inline fun <K, V> KVCache<K, List<V>>.actualizeAll( suspend inline fun <K, V> KVCache<K, List<V>>.actualizeAll(
repo: ReadKeyValuesRepo<K, V> repo: ReadKeyValuesRepo<K, V>,
clear: Boolean = true,
) { ) {
actualizeAll { actualizeAll(clear) {
repo.getAll { keys(it) }.toMap() repo.getAll { keys(it) }.toMap()
} }
} }
suspend inline fun <K, V> KVCache<K, V>.actualizeAll( suspend inline fun <K, V> KVCache<K, V>.actualizeAll(
repo: ReadCRUDRepo<V, K> repo: ReadCRUDRepo<V, K>,
clear: Boolean = true,
) { ) {
actualizeAll { actualizeAll(clear) {
repo.getAllByWithNextPaging { repo.getAllByWithNextPaging {
getIdsByPagination(it) getIdsByPagination(it)
}.mapNotNull { it to (repo.getById(it) ?: return@mapNotNull null) }.toMap() }.mapNotNull { it to (repo.getById(it) ?: return@mapNotNull null) }.toMap()