From d543d436bc1230fc9431f276fdecb6465189983b Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 7 Sep 2022 20:01:13 +0600 Subject: [PATCH] Fixes in key values cache --- CHANGELOG.md | 4 ++ .../repos/cache/KeyValuesCacheRepo.kt | 44 +++++++++++++------ 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ef410149617..8aa3ee03430 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## 0.12.9 +* `Repos`: + * `Cache`: + * Fixes in key values cache + ## 0.12.8 * `Versions`: diff --git a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/KeyValuesCacheRepo.kt b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/KeyValuesCacheRepo.kt index 716463b76c9..346454e0572 100644 --- a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/KeyValuesCacheRepo.kt +++ b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/KeyValuesCacheRepo.kt @@ -1,9 +1,7 @@ package dev.inmo.micro_utils.repos.cache -import dev.inmo.micro_utils.pagination.Pagination -import dev.inmo.micro_utils.pagination.PaginationResult -import dev.inmo.micro_utils.pagination.utils.paginate -import dev.inmo.micro_utils.pagination.utils.reverse +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.KVCache import kotlinx.coroutines.CoroutineScope @@ -15,18 +13,24 @@ open class ReadKeyValuesCacheRepo( protected open val kvCache: KVCache> ) : ReadKeyValuesRepo by parentRepo, CacheRepo { override suspend fun get(k: Key, pagination: Pagination, reversed: Boolean): PaginationResult { - return kvCache.get(k) ?.paginate( - pagination.let { if (reversed) it.reverse(count(k)) else it } - ) ?.let { - if (reversed) it.copy(results = it.results.reversed()) else it - } ?: parentRepo.get(k, pagination, reversed) + val count = count(k) + return getAll(k, reversed).createPaginationResult( + pagination, + count + ) } override suspend fun getAll(k: Key, reversed: Boolean): List { return kvCache.get(k) ?.let { if (reversed) it.reversed() else it - } ?: parentRepo.getAll(k, reversed) + } ?: parentRepo.getAll(k, reversed).also { + kvCache.set(k, it) + } } - override suspend fun contains(k: Key, v: Value): Boolean = kvCache.get(k) ?.contains(v) ?: parentRepo.contains(k, v) + override suspend fun contains(k: Key, v: Value): Boolean = kvCache.get(k) ?.contains(v) ?: (parentRepo.contains(k, v).also { + if (it) { + kvCache.unset(k) // clear as invalid + } + }) override suspend fun contains(k: Key): Boolean = kvCache.contains(k) || parentRepo.contains(k) } @@ -39,9 +43,21 @@ open class KeyValuesCacheRepo( kvCache: KVCache>, scope: CoroutineScope = CoroutineScope(Dispatchers.Default) ) : ReadKeyValuesCacheRepo(parentRepo, kvCache), KeyValuesRepo, WriteKeyValuesRepo by parentRepo, CacheRepo { - protected val onNewJob = parentRepo.onNewValue.onEach { kvCache.set(it.first, kvCache.get(it.first) ?.plus(it.second) ?: listOf(it.second)) }.launchIn(scope) - protected val onRemoveJob = parentRepo.onValueRemoved.onEach { kvCache.set(it.first, kvCache.get(it.first) ?.minus(it.second) ?: return@onEach) }.launchIn(scope) - protected val onDataClearedJob = parentRepo.onDataCleared.onEach { kvCache.unset(it) }.launchIn(scope) + protected val onNewJob = parentRepo.onNewValue.onEach { (k, v) -> + kvCache.set( + k, + kvCache.get(k) ?.plus(v) ?: return@onEach + ) + }.launchIn(scope) + protected val onRemoveJob = parentRepo.onValueRemoved.onEach { (k, v) -> + kvCache.set( + k, + kvCache.get(k) ?.minus(v) ?: return@onEach + ) + }.launchIn(scope) + protected val onDataClearedJob = parentRepo.onDataCleared.onEach { + kvCache.unset(it) + }.launchIn(scope) } fun KeyValuesRepo.cached(