From 8bee354f049cf2b223526dcef7e958081b20d8a8 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 30 Jun 2022 13:59:32 +0600 Subject: [PATCH] improvements of cache repos --- CHANGELOG.md | 5 +++++ .../dev/inmo/micro_utils/repos/cache/CRUDCacheRepo.kt | 4 ++-- .../kotlin/dev/inmo/micro_utils/repos/cache/CacheRepo.kt | 3 +++ .../dev/inmo/micro_utils/repos/cache/KeyValueCacheRepo.kt | 4 ++-- .../inmo/micro_utils/repos/cache/KeyValuesCacheRepo.kt | 4 ++-- .../micro_utils/repos/cache/full/FullCRUDCacheRepo.kt | 4 ++-- .../inmo/micro_utils/repos/cache/full/FullCacheRepo.kt | 5 +++++ .../micro_utils/repos/cache/full/FullKeyValueCacheRepo.kt | 6 +++--- .../repos/cache/full/FullKeyValuesCacheRepo.kt | 8 ++++---- 9 files changed, 28 insertions(+), 15 deletions(-) create mode 100644 repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/CacheRepo.kt create mode 100644 repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/full/FullCacheRepo.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ed0e6b4ce9..543f7913225 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ * `Ktor`: * `Client`: * `BodyPair` has been deprecated +* `Repos`: + * `Cache`: + * New interface `CacheRepo` + * New interface `FullCacheRepo` + * `actualize*` methods inside of full cache repos now open for overriding ## 0.11.10 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 c4facb233f5..28ab069b0ff 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,7 +10,7 @@ open class ReadCRUDCacheRepo( protected open val parentRepo: ReadCRUDRepo, protected open val kvCache: KVCache, protected open val idGetter: (ObjectType) -> IdType -) : ReadCRUDRepo by parentRepo { +) : ReadCRUDRepo by parentRepo, CacheRepo { override suspend fun getById(id: IdType): ObjectType? = kvCache.get(id) ?: (parentRepo.getById(id) ?.also { kvCache.set(id, it) }) @@ -28,7 +28,7 @@ open class WriteCRUDCacheRepo( protected open val kvCache: KVCache, protected open val scope: CoroutineScope = CoroutineScope(Dispatchers.Default), protected open val idGetter: (ObjectType) -> IdType -) : WriteCRUDRepo { +) : WriteCRUDRepo, CacheRepo { override val newObjectsFlow: Flow by parentRepo::newObjectsFlow override val updatedObjectsFlow: Flow by parentRepo::updatedObjectsFlow override val deletedObjectsIdsFlow: Flow by parentRepo::deletedObjectsIdsFlow 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 new file mode 100644 index 00000000000..73dbbcc11a9 --- /dev/null +++ b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/CacheRepo.kt @@ -0,0 +1,3 @@ +package dev.inmo.micro_utils.repos.cache + +interface 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 3e2265e4466..e16eb4aac62 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 @@ -11,7 +11,7 @@ import kotlinx.coroutines.flow.* open class ReadKeyValueCacheRepo( protected open val parentRepo: ReadKeyValueRepo, protected open val kvCache: KVCache, -) : ReadKeyValueRepo by parentRepo { +) : ReadKeyValueRepo by parentRepo, CacheRepo { 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) } @@ -24,7 +24,7 @@ open class KeyValueCacheRepo( parentRepo: KeyValueRepo, kvCache: KVCache, scope: CoroutineScope = CoroutineScope(Dispatchers.Default) -) : ReadKeyValueCacheRepo(parentRepo, kvCache), KeyValueRepo, WriteKeyValueRepo by parentRepo { +) : ReadKeyValueCacheRepo(parentRepo, kvCache), KeyValueRepo, WriteKeyValueRepo by parentRepo, CacheRepo { 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) } 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 afe2d202d8c..716463b76c9 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 @@ -13,7 +13,7 @@ import kotlinx.coroutines.flow.* open class ReadKeyValuesCacheRepo( protected open val parentRepo: ReadKeyValuesRepo, protected open val kvCache: KVCache> -) : ReadKeyValuesRepo by parentRepo { +) : 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 } @@ -38,7 +38,7 @@ open class KeyValuesCacheRepo( parentRepo: KeyValuesRepo, kvCache: KVCache>, scope: CoroutineScope = CoroutineScope(Dispatchers.Default) -) : ReadKeyValuesCacheRepo(parentRepo, kvCache), KeyValuesRepo, WriteKeyValuesRepo by parentRepo { +) : 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) 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 f5a76e06b1d..bbd5445e5f3 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 @@ -15,7 +15,7 @@ open class FullReadCRUDCacheRepo( protected open val parentRepo: ReadCRUDRepo, protected open val kvCache: FullKVCache, protected open val idGetter: (ObjectType) -> IdType -) : ReadCRUDRepo { +) : ReadCRUDRepo, FullCacheRepo { protected inline fun doOrTakeAndActualize( action: FullKVCache.() -> Optional, actionElse: ReadCRUDRepo.() -> T, @@ -31,7 +31,7 @@ open class FullReadCRUDCacheRepo( error("The result should be returned above") } - protected suspend fun actualizeAll() { + protected open suspend fun actualizeAll() { kvCache.clear() doForAllWithNextPaging { parentRepo.getByPagination(it).also { diff --git a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/full/FullCacheRepo.kt b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/full/FullCacheRepo.kt new file mode 100644 index 00000000000..e94d3abe99d --- /dev/null +++ b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/full/FullCacheRepo.kt @@ -0,0 +1,5 @@ +package dev.inmo.micro_utils.repos.cache.full + +import dev.inmo.micro_utils.repos.cache.CacheRepo + +interface FullCacheRepo : CacheRepo 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 6e428846c25..802cddb2925 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 @@ -13,7 +13,7 @@ import kotlinx.coroutines.flow.* open class FullReadKeyValueCacheRepo( protected open val parentRepo: ReadKeyValueRepo, protected open val kvCache: FullKVCache, -) : ReadKeyValueRepo { +) : ReadKeyValueRepo, FullCacheRepo { protected inline fun doOrTakeAndActualize( action: FullKVCache.() -> Optional, actionElse: ReadKeyValueRepo.() -> T, @@ -28,7 +28,7 @@ open class FullReadKeyValueCacheRepo( } error("The result should be returned above") } - protected suspend fun actualizeAll() { + protected open suspend fun actualizeAll() { kvCache.clear() kvCache.set(parentRepo.getAll { keys(it) }.toMap()) } @@ -78,7 +78,7 @@ open class FullWriteKeyValueCacheRepo( protected open val parentRepo: WriteKeyValueRepo, protected open val kvCache: FullKVCache, scope: CoroutineScope = CoroutineScope(Dispatchers.Default) -) : WriteKeyValueRepo by parentRepo { +) : 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) } 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 014cde674e4..bc92579b77a 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 @@ -12,7 +12,7 @@ import kotlinx.coroutines.flow.* open class FullReadKeyValuesCacheRepo( protected open val parentRepo: ReadKeyValuesRepo, protected open val kvCache: FullKVCache>, -) : ReadKeyValuesRepo { +) : ReadKeyValuesRepo, FullCacheRepo { protected inline fun doOrTakeAndActualize( action: FullKVCache>.() -> Optional, actionElse: ReadKeyValuesRepo.() -> T, @@ -28,11 +28,11 @@ open class FullReadKeyValuesCacheRepo( error("The result should be returned above") } - protected suspend fun actualizeKey(k: Key) { + protected open suspend fun actualizeKey(k: Key) { kvCache.set(k, parentRepo.getAll(k)) } - protected suspend fun actualizeAll() { + protected open suspend fun actualizeAll() { doAllWithCurrentPaging { kvCache.keys(it).also { kvCache.unset(it.results) } } kvCache.set(parentRepo.getAll()) } @@ -112,7 +112,7 @@ open class FullWriteKeyValuesCacheRepo( protected open val parentRepo: WriteKeyValuesRepo, protected open val kvCache: FullKVCache>, scope: CoroutineScope = CoroutineScope(Dispatchers.Default) -) : WriteKeyValuesRepo by parentRepo { +) : WriteKeyValuesRepo by parentRepo, FullCacheRepo { protected val onNewJob = parentRepo.onNewValue.onEach { kvCache.set( it.first,