diff --git a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/CRUDCacheRepo.kt b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/CRUDCacheRepo.kt index 413e116cd92..264e727ed1c 100644 --- a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/CRUDCacheRepo.kt +++ b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/CRUDCacheRepo.kt @@ -10,12 +10,14 @@ open class ReadCRUDCacheRepo( protected open val parentRepo: ReadCRUDRepo, protected open val kvCache: KVCache, protected open val idGetter: (ObjectType) -> IdType -) : ReadCRUDRepo by parentRepo, CacheRepo { +) : ReadCRUDRepo by parentRepo, CommonCacheRepo { override suspend fun getById(id: IdType): ObjectType? = kvCache.get(id) ?: (parentRepo.getById(id) ?.also { kvCache.set(id, it) }) override suspend fun contains(id: IdType): Boolean = kvCache.contains(id) || parentRepo.contains(id) + + override suspend fun invalidate() = kvCache.clear() } fun ReadCRUDRepo.cached( @@ -28,7 +30,7 @@ open class WriteCRUDCacheRepo( protected open val kvCache: KVCache, protected open val scope: CoroutineScope = CoroutineScope(Dispatchers.Default), protected open val idGetter: (ObjectType) -> IdType -) : WriteCRUDRepo, CacheRepo { +) : WriteCRUDRepo, CommonCacheRepo { override val newObjectsFlow: Flow by parentRepo::newObjectsFlow override val updatedObjectsFlow: Flow by parentRepo::updatedObjectsFlow override val deletedObjectsIdsFlow: Flow by parentRepo::deletedObjectsIdsFlow @@ -72,6 +74,8 @@ open class WriteCRUDCacheRepo( return created } + + override suspend fun invalidate() = kvCache.clear() } fun WriteCRUDRepo.caching( diff --git a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/CacheRepo.kt b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/CacheRepo.kt index 73dbbcc11a9..55abb179fd6 100644 --- a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/CacheRepo.kt +++ b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/CacheRepo.kt @@ -1,3 +1,5 @@ package dev.inmo.micro_utils.repos.cache -interface CacheRepo +interface CacheRepo { + suspend fun invalidate() +} diff --git a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/CommonCacheRepo.kt b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/CommonCacheRepo.kt new file mode 100644 index 00000000000..4fcfbc3dd89 --- /dev/null +++ b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/CommonCacheRepo.kt @@ -0,0 +1,7 @@ +package dev.inmo.micro_utils.repos.cache + +/** + * Any inheritor of this should work with next logic: try to take data from some [dev.inmo.micro_utils.repos.cache.cache.KVCache] and, + * if not exists, take from origin and save to the cache for future reuse + */ +interface CommonCacheRepo : CacheRepo diff --git a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/FallbackCacheRepo.kt b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/FallbackCacheRepo.kt new file mode 100644 index 00000000000..0be4cc9eb3e --- /dev/null +++ b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/FallbackCacheRepo.kt @@ -0,0 +1,8 @@ +package dev.inmo.micro_utils.repos.cache + +/** + * Any inheritor of this should work with next logic: try to take data from original repo and, + * if not exists, try to take from the cache some. In case if original repo have returned result, it should be saved to the internal + * [dev.inmo.micro_utils.repos.cache.cache.KVCache] + */ +interface FallbackCacheRepo : CacheRepo diff --git a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/KeyValueCacheRepo.kt b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/KeyValueCacheRepo.kt index fb4a46e194e..8b9c36196d1 100644 --- a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/KeyValueCacheRepo.kt +++ b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/KeyValueCacheRepo.kt @@ -10,7 +10,7 @@ import kotlinx.coroutines.flow.* open class ReadKeyValueCacheRepo( protected open val parentRepo: ReadKeyValueRepo, protected open val kvCache: KVCache, -) : ReadKeyValueRepo by parentRepo, CacheRepo { +) : ReadKeyValueRepo by parentRepo, CommonCacheRepo { override suspend fun get(k: Key): Value? = kvCache.get(k) ?: parentRepo.get(k) ?.also { kvCache.set(k, it) } override suspend fun contains(key: Key): Boolean = kvCache.contains(key) || parentRepo.contains(key) @@ -23,6 +23,8 @@ open class ReadKeyValueCacheRepo( ) } } + + override suspend fun invalidate() = kvCache.clear() } fun ReadKeyValueRepo.cached( @@ -33,9 +35,11 @@ open class KeyValueCacheRepo( parentRepo: KeyValueRepo, kvCache: KVCache, scope: CoroutineScope = CoroutineScope(Dispatchers.Default) -) : ReadKeyValueCacheRepo(parentRepo, kvCache), KeyValueRepo, WriteKeyValueRepo by parentRepo, CacheRepo { +) : ReadKeyValueCacheRepo(parentRepo, kvCache), KeyValueRepo, WriteKeyValueRepo by parentRepo, CommonCacheRepo { protected val onNewJob = parentRepo.onNewValue.onEach { kvCache.set(it.first, it.second) }.launchIn(scope) protected val onRemoveJob = parentRepo.onValueRemoved.onEach { kvCache.unset(it) }.launchIn(scope) + + override suspend fun invalidate() = kvCache.clear() } fun KeyValueRepo.cached( 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 c251b852746..d4db6d676ed 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 @@ -11,7 +11,7 @@ import kotlinx.coroutines.flow.* open class ReadKeyValuesCacheRepo( protected open val parentRepo: ReadKeyValuesRepo, protected open val kvCache: KVCache> -) : ReadKeyValuesRepo by parentRepo, CacheRepo { +) : ReadKeyValuesRepo by parentRepo, CommonCacheRepo { override suspend fun get(k: Key, pagination: Pagination, reversed: Boolean): PaginationResult { return getAll(k, reversed).paginate( pagination @@ -30,6 +30,8 @@ open class ReadKeyValuesCacheRepo( } }) override suspend fun contains(k: Key): Boolean = kvCache.contains(k) || parentRepo.contains(k) + + override suspend fun invalidate() = kvCache.clear() } fun ReadKeyValuesRepo.cached( @@ -40,7 +42,7 @@ open class KeyValuesCacheRepo( parentRepo: KeyValuesRepo, kvCache: KVCache>, scope: CoroutineScope = CoroutineScope(Dispatchers.Default) -) : ReadKeyValuesCacheRepo(parentRepo, kvCache), KeyValuesRepo, WriteKeyValuesRepo by parentRepo, CacheRepo { +) : ReadKeyValuesCacheRepo(parentRepo, kvCache), KeyValuesRepo, WriteKeyValuesRepo by parentRepo, CommonCacheRepo { protected val onNewJob = parentRepo.onNewValue.onEach { (k, v) -> kvCache.set( k, @@ -56,6 +58,8 @@ open class KeyValuesCacheRepo( protected val onDataClearedJob = parentRepo.onDataCleared.onEach { kvCache.unset(it) }.launchIn(scope) + + override suspend fun invalidate() = kvCache.clear() } fun KeyValuesRepo.cached( 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 242b07e12ed..1864180ebab 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 @@ -69,6 +69,10 @@ open class FullReadCRUDCacheRepo( { getById(id) }, { it ?.let { set(idGetter(it), it) } } ) + + override suspend fun invalidate() { + actualizeAll() + } } fun ReadCRUDRepo.cached( @@ -92,7 +96,11 @@ open class FullCRUDCacheRepo( scope, idGetter ), - CRUDRepo + CRUDRepo { + override suspend fun invalidate() { + actualizeAll() + } +} fun CRUDRepo.cached( kvCache: FullKVCache, 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 802cddb2925..a730b01a5d3 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 @@ -68,6 +68,10 @@ open class FullReadKeyValueCacheRepo( { parentRepo.keys(v, pagination, reversed) }, { if (it.results.isNotEmpty()) actualizeAll() } ) + + override suspend fun invalidate() { + actualizeAll() + } } fun ReadKeyValueRepo.cached( @@ -81,6 +85,10 @@ open class FullWriteKeyValueCacheRepo( ) : WriteKeyValueRepo by parentRepo, FullCacheRepo { protected val onNewJob = parentRepo.onNewValue.onEach { kvCache.set(it.first, it.second) }.launchIn(scope) protected val onRemoveJob = parentRepo.onValueRemoved.onEach { kvCache.unset(it) }.launchIn(scope) + + override suspend fun invalidate() { + kvCache.clear() + } } fun WriteKeyValueRepo.caching( @@ -96,6 +104,10 @@ open class FullKeyValueCacheRepo( KeyValueRepo, ReadKeyValueRepo by FullReadKeyValueCacheRepo(parentRepo, kvCache) { override suspend fun unsetWithValues(toUnset: List) = parentRepo.unsetWithValues(toUnset) + + override suspend fun invalidate() { + super.invalidate() + } } fun KeyValueRepo.cached( 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 bc92579b77a..7934a73d65a 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 @@ -102,6 +102,9 @@ open class FullReadKeyValuesCacheRepo( { if (it.results.isNotEmpty()) actualizeAll() } ) + override suspend fun invalidate() { + actualizeAll() + } } fun ReadKeyValuesRepo.cached( @@ -125,6 +128,10 @@ open class FullWriteKeyValuesCacheRepo( kvCache.get(it.first) ?.minus(it.second) ?: return@onEach ) }.launchIn(scope) + + override suspend fun invalidate() { + kvCache.clear() + } } fun WriteKeyValuesRepo.caching( @@ -146,6 +153,10 @@ open class FullKeyValuesCacheRepo( } } } + + override suspend fun invalidate() { + super.invalidate() + } } fun KeyValuesRepo.caching( 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 893453e9a90..307638088d2 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 @@ -125,7 +125,11 @@ interface KeyValueRepo : ReadKeyValueRepo, WriteKeyValue * By default, will remove all the data of current repo using [doAllWithCurrentPaging], [keys] and [unset] */ suspend fun clear() { - doAllWithCurrentPaging { keys(it).also { unset(it.results) } } + var count: Int + do { + count = count().takeIf { it < Int.MAX_VALUE } ?.toInt() ?: Int.MAX_VALUE + keys(FirstPagePagination(count)).also { unset(it.results) } + } while(count > 0) } } typealias StandardKeyValueRepo = KeyValueRepo