diff --git a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/full/FullCRUDCacheRepo.kt b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/full/FullCRUDCacheRepo.kt index 1864180ebab..7878d897668 100644 --- a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/full/FullCRUDCacheRepo.kt +++ b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/full/FullCRUDCacheRepo.kt @@ -8,6 +8,7 @@ import dev.inmo.micro_utils.repos.* import dev.inmo.micro_utils.repos.cache.* import dev.inmo.micro_utils.repos.cache.cache.FullKVCache import dev.inmo.micro_utils.repos.cache.cache.KVCache +import dev.inmo.micro_utils.repos.cache.util.actualizeAll import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -32,12 +33,7 @@ open class FullReadCRUDCacheRepo( } protected open suspend fun actualizeAll() { - kvCache.clear() - doForAllWithNextPaging { - parentRepo.getByPagination(it).also { - kvCache.set(it.results.associateBy { idGetter(it) }) - } - } + kvCache.actualizeAll(parentRepo) } override suspend fun getByPagination(pagination: Pagination): PaginationResult = doOrTakeAndActualize( diff --git a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/full/FullKeyValueCacheRepo.kt b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/full/FullKeyValueCacheRepo.kt index a730b01a5d3..635e5754724 100644 --- a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/full/FullKeyValueCacheRepo.kt +++ b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/full/FullKeyValueCacheRepo.kt @@ -5,6 +5,7 @@ import dev.inmo.micro_utils.pagination.Pagination import dev.inmo.micro_utils.pagination.PaginationResult import dev.inmo.micro_utils.repos.* import dev.inmo.micro_utils.repos.cache.cache.FullKVCache +import dev.inmo.micro_utils.repos.cache.util.actualizeAll import dev.inmo.micro_utils.repos.pagination.getAll import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -97,7 +98,7 @@ fun WriteKeyValueRepo.caching( ) = FullWriteKeyValueCacheRepo(this, kvCache, scope) open class FullKeyValueCacheRepo( - parentRepo: KeyValueRepo, + override val parentRepo: KeyValueRepo, kvCache: FullKVCache, scope: CoroutineScope = CoroutineScope(Dispatchers.Default) ) : FullWriteKeyValueCacheRepo(parentRepo, kvCache, scope), @@ -106,7 +107,7 @@ open class FullKeyValueCacheRepo( override suspend fun unsetWithValues(toUnset: List) = parentRepo.unsetWithValues(toUnset) override suspend fun invalidate() { - super.invalidate() + kvCache.actualizeAll(parentRepo) } } diff --git a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/full/FullKeyValuesCacheRepo.kt b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/full/FullKeyValuesCacheRepo.kt index 7934a73d65a..e91ef6dbcb1 100644 --- a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/full/FullKeyValuesCacheRepo.kt +++ b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/full/FullKeyValuesCacheRepo.kt @@ -5,6 +5,7 @@ import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.pagination.utils.* import dev.inmo.micro_utils.repos.* import dev.inmo.micro_utils.repos.cache.cache.FullKVCache +import dev.inmo.micro_utils.repos.cache.util.actualizeAll import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.* @@ -33,8 +34,7 @@ open class FullReadKeyValuesCacheRepo( } protected open suspend fun actualizeAll() { - doAllWithCurrentPaging { kvCache.keys(it).also { kvCache.unset(it.results) } } - kvCache.set(parentRepo.getAll()) + kvCache.actualizeAll(parentRepo) } override suspend fun get(k: Key, pagination: Pagination, reversed: Boolean): PaginationResult { @@ -140,7 +140,7 @@ fun WriteKeyValuesRepo.caching( ) = FullWriteKeyValuesCacheRepo(this, kvCache, scope) open class FullKeyValuesCacheRepo( - parentRepo: KeyValuesRepo, + override val parentRepo: KeyValuesRepo, kvCache: FullKVCache>, scope: CoroutineScope = CoroutineScope(Dispatchers.Default) ) : FullWriteKeyValuesCacheRepo(parentRepo, kvCache, scope), @@ -155,7 +155,7 @@ open class FullKeyValuesCacheRepo( } override suspend fun invalidate() { - super.invalidate() + kvCache.actualizeAll(parentRepo) } } diff --git a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/util/ActualizeAll.kt b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/util/ActualizeAll.kt new file mode 100644 index 00000000000..a657d6b8b45 --- /dev/null +++ b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/util/ActualizeAll.kt @@ -0,0 +1,61 @@ +package dev.inmo.micro_utils.repos.cache.util + +import dev.inmo.micro_utils.pagination.FirstPagePagination +import dev.inmo.micro_utils.pagination.utils.doForAllWithNextPaging +import dev.inmo.micro_utils.repos.ReadCRUDRepo +import dev.inmo.micro_utils.repos.ReadKeyValueRepo +import dev.inmo.micro_utils.repos.ReadKeyValuesRepo +import dev.inmo.micro_utils.repos.cache.cache.KVCache +import dev.inmo.micro_utils.repos.set + +suspend inline fun KVCache.actualizeAll( + getAll: () -> Map +) { + clear() + set(getAll()) +} + +suspend inline fun KVCache.actualizeAll( + repo: ReadKeyValueRepo +) { + clear() + val count = repo.count().takeIf { it < Int.MAX_VALUE } ?.toInt() ?: Int.MAX_VALUE + val initPagination = FirstPagePagination(count) + doForAllWithNextPaging(initPagination) { + keys(it).also { + set( + it.results.mapNotNull { k -> repo.get(k) ?.let { k to it } } + ) + } + } +} + +suspend inline fun KVCache>.actualizeAll( + repo: ReadKeyValuesRepo +) { + clear() + val count = repo.count().takeIf { it < Int.MAX_VALUE } ?.toInt() ?: Int.MAX_VALUE + val initPagination = FirstPagePagination(count) + doForAllWithNextPaging(initPagination) { + keys(it).also { + set( + it.results.associateWith { k -> repo.getAll(k) } + ) + } + } +} + +suspend inline fun KVCache.actualizeAll( + repo: ReadCRUDRepo +) { + clear() + val count = repo.count().takeIf { it < Int.MAX_VALUE } ?.toInt() ?: Int.MAX_VALUE + val initPagination = FirstPagePagination(count) + doForAllWithNextPaging(initPagination) { + keys(it).also { + set( + it.results.mapNotNull { k -> repo.getById(k) ?.let { k to it } } + ) + } + } +} diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/KeyValueRepo.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/KeyValueRepo.kt index 307638088d2..1faa5c4a546 100644 --- a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/KeyValueRepo.kt +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/KeyValueRepo.kt @@ -93,6 +93,10 @@ suspend inline fun WriteKeyValueRepo.set( vararg toSet: Pair ) = set(toSet.toMap()) +suspend inline fun WriteKeyValueRepo.set( + toSet: List> +) = set(toSet.toMap()) + suspend inline fun WriteKeyValueRepo.set( k: Key, v: Value ) = set(k to v)