mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-17 13:53:49 +00:00
add full cache repos
This commit is contained in:
parent
540d5cce7c
commit
0d0c16e16d
@ -41,6 +41,13 @@ data class Optional<T> internal constructor(
|
|||||||
inline val <T> T.optional
|
inline val <T> T.optional
|
||||||
get() = Optional.presented(this)
|
get() = Optional.presented(this)
|
||||||
|
|
||||||
|
inline val <T : Any> T?.optionalOrAbsentIfNull
|
||||||
|
get() = if (this == null) {
|
||||||
|
Optional.absent<T>()
|
||||||
|
} else {
|
||||||
|
Optional.presented(this)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Will call [block] when data presented ([Optional.dataPresented] == true)
|
* Will call [block] when data presented ([Optional.dataPresented] == true)
|
||||||
*/
|
*/
|
||||||
|
@ -4,13 +4,12 @@ import dev.inmo.micro_utils.repos.*
|
|||||||
import dev.inmo.micro_utils.repos.cache.cache.KVCache
|
import dev.inmo.micro_utils.repos.cache.cache.KVCache
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.flow.launchIn
|
import kotlinx.coroutines.flow.*
|
||||||
import kotlinx.coroutines.flow.onEach
|
|
||||||
|
|
||||||
open class ReadCRUDCacheRepo<ObjectType, IdType>(
|
open class ReadCRUDCacheRepo<ObjectType, IdType>(
|
||||||
protected val parentRepo: ReadCRUDRepo<ObjectType, IdType>,
|
protected open val parentRepo: ReadCRUDRepo<ObjectType, IdType>,
|
||||||
protected val kvCache: KVCache<IdType, ObjectType>,
|
protected open val kvCache: KVCache<IdType, ObjectType>,
|
||||||
protected val idGetter: (ObjectType) -> IdType
|
protected open val idGetter: (ObjectType) -> IdType
|
||||||
) : ReadCRUDRepo<ObjectType, IdType> by parentRepo {
|
) : ReadCRUDRepo<ObjectType, IdType> by parentRepo {
|
||||||
override suspend fun getById(id: IdType): ObjectType? = kvCache.get(id) ?: (parentRepo.getById(id) ?.also {
|
override suspend fun getById(id: IdType): ObjectType? = kvCache.get(id) ?: (parentRepo.getById(id) ?.also {
|
||||||
kvCache.set(id, it)
|
kvCache.set(id, it)
|
||||||
@ -19,8 +18,52 @@ open class ReadCRUDCacheRepo<ObjectType, IdType>(
|
|||||||
override suspend fun contains(id: IdType): Boolean = kvCache.contains(id) || parentRepo.contains(id)
|
override suspend fun contains(id: IdType): Boolean = kvCache.contains(id) || parentRepo.contains(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
open class WriteCRUDCacheRepo<ObjectType, IdType, InputValueType>(
|
||||||
|
protected open val parentRepo: CRUDRepo<ObjectType, IdType, InputValueType>,
|
||||||
|
protected open val kvCache: KVCache<IdType, ObjectType>,
|
||||||
|
protected open val scope: CoroutineScope = CoroutineScope(Dispatchers.Default),
|
||||||
|
protected open val idGetter: (ObjectType) -> IdType
|
||||||
|
) : WriteCRUDRepo<ObjectType, IdType, InputValueType> {
|
||||||
|
override val newObjectsFlow: Flow<ObjectType> by parentRepo::newObjectsFlow
|
||||||
|
override val updatedObjectsFlow: Flow<ObjectType> by parentRepo::updatedObjectsFlow
|
||||||
|
override val deletedObjectsIdsFlow: Flow<IdType> by parentRepo::deletedObjectsIdsFlow
|
||||||
|
|
||||||
|
val deletedObjectsFlowJob = parentRepo.deletedObjectsIdsFlow.onEach {
|
||||||
|
kvCache.unset(it)
|
||||||
|
}.launchIn(scope)
|
||||||
|
|
||||||
|
override suspend fun deleteById(ids: List<IdType>) = parentRepo.deleteById(ids)
|
||||||
|
|
||||||
|
override suspend fun update(values: List<UpdatedValuePair<IdType, InputValueType>>): List<ObjectType> {
|
||||||
|
val updated = parentRepo.update(values)
|
||||||
|
|
||||||
|
kvCache.unset(values.map { it.id })
|
||||||
|
kvCache.set(updated.associateBy { idGetter(it) })
|
||||||
|
|
||||||
|
return updated
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun update(id: IdType, value: InputValueType): ObjectType? {
|
||||||
|
return parentRepo.update(id, value) ?.also {
|
||||||
|
kvCache.unset(id)
|
||||||
|
kvCache.set(idGetter(it), it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun create(values: List<InputValueType>): List<ObjectType> {
|
||||||
|
val created = parentRepo.create(values)
|
||||||
|
|
||||||
|
kvCache.set(
|
||||||
|
created.associateBy { idGetter(it) }
|
||||||
|
)
|
||||||
|
|
||||||
|
return created
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
open class CRUDCacheRepo<ObjectType, IdType, InputValueType>(
|
open class CRUDCacheRepo<ObjectType, IdType, InputValueType>(
|
||||||
parentRepo: CRUDRepo<ObjectType, IdType, InputValueType>,
|
override val parentRepo: CRUDRepo<ObjectType, IdType, InputValueType>,
|
||||||
kvCache: KVCache<IdType, ObjectType>,
|
kvCache: KVCache<IdType, ObjectType>,
|
||||||
scope: CoroutineScope = CoroutineScope(Dispatchers.Default),
|
scope: CoroutineScope = CoroutineScope(Dispatchers.Default),
|
||||||
idGetter: (ObjectType) -> IdType
|
idGetter: (ObjectType) -> IdType
|
||||||
@ -28,8 +71,11 @@ open class CRUDCacheRepo<ObjectType, IdType, InputValueType>(
|
|||||||
parentRepo,
|
parentRepo,
|
||||||
kvCache,
|
kvCache,
|
||||||
idGetter
|
idGetter
|
||||||
), CRUDRepo<ObjectType, IdType, InputValueType>, WriteCRUDRepo<ObjectType, IdType, InputValueType> by parentRepo {
|
),
|
||||||
protected val onNewJob = parentRepo.newObjectsFlow.onEach { kvCache.set(idGetter(it), it) }.launchIn(scope)
|
WriteCRUDRepo<ObjectType, IdType, InputValueType> by WriteCRUDCacheRepo(
|
||||||
protected val onUpdatedJob = parentRepo.updatedObjectsFlow.onEach { kvCache.set(idGetter(it), it) }.launchIn(scope)
|
parentRepo,
|
||||||
protected val onRemoveJob = parentRepo.deletedObjectsIdsFlow.onEach { kvCache.unset(it) }.launchIn(scope)
|
kvCache,
|
||||||
}
|
scope,
|
||||||
|
idGetter
|
||||||
|
),
|
||||||
|
CRUDRepo<ObjectType, IdType, InputValueType>
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package dev.inmo.micro_utils.repos.cache
|
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.repos.*
|
import dev.inmo.micro_utils.repos.*
|
||||||
import dev.inmo.micro_utils.repos.cache.cache.KVCache
|
import dev.inmo.micro_utils.repos.cache.cache.KVCache
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
@ -7,8 +9,8 @@ import kotlinx.coroutines.Dispatchers
|
|||||||
import kotlinx.coroutines.flow.*
|
import kotlinx.coroutines.flow.*
|
||||||
|
|
||||||
open class ReadKeyValueCacheRepo<Key,Value>(
|
open class ReadKeyValueCacheRepo<Key,Value>(
|
||||||
protected val parentRepo: ReadKeyValueRepo<Key, Value>,
|
protected open val parentRepo: ReadKeyValueRepo<Key, Value>,
|
||||||
protected val kvCache: KVCache<Key, Value>,
|
protected open val kvCache: KVCache<Key, Value>,
|
||||||
) : ReadKeyValueRepo<Key,Value> by parentRepo {
|
) : ReadKeyValueRepo<Key,Value> by parentRepo {
|
||||||
override suspend fun get(k: Key): Value? = kvCache.get(k) ?: parentRepo.get(k) ?.also { kvCache.set(k, it) }
|
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)
|
override suspend fun contains(key: Key): Boolean = kvCache.contains(key) || parentRepo.contains(key)
|
||||||
|
@ -11,8 +11,8 @@ import kotlinx.coroutines.Dispatchers
|
|||||||
import kotlinx.coroutines.flow.*
|
import kotlinx.coroutines.flow.*
|
||||||
|
|
||||||
open class ReadKeyValuesCacheRepo<Key,Value>(
|
open class ReadKeyValuesCacheRepo<Key,Value>(
|
||||||
protected val parentRepo: ReadKeyValuesRepo<Key, Value>,
|
protected open val parentRepo: ReadKeyValuesRepo<Key, Value>,
|
||||||
protected val kvCache: KVCache<Key, List<Value>>
|
protected open val kvCache: KVCache<Key, List<Value>>
|
||||||
) : ReadKeyValuesRepo<Key,Value> by parentRepo {
|
) : ReadKeyValuesRepo<Key,Value> by parentRepo {
|
||||||
override suspend fun get(k: Key, pagination: Pagination, reversed: Boolean): PaginationResult<Value> {
|
override suspend fun get(k: Key, pagination: Pagination, reversed: Boolean): PaginationResult<Value> {
|
||||||
return kvCache.get(k) ?.paginate(
|
return kvCache.get(k) ?.paginate(
|
||||||
|
@ -0,0 +1,24 @@
|
|||||||
|
package dev.inmo.micro_utils.repos.cache.cache
|
||||||
|
|
||||||
|
import dev.inmo.micro_utils.repos.KeyValueRepo
|
||||||
|
import dev.inmo.micro_utils.repos.MapKeyValueRepo
|
||||||
|
import kotlinx.coroutines.sync.Mutex
|
||||||
|
import kotlinx.coroutines.sync.withLock
|
||||||
|
|
||||||
|
open class SimpleUnlimitedKVCache<K, V>(
|
||||||
|
private val kvParent: KeyValueRepo<K, V> = MapKeyValueRepo<K, V>()
|
||||||
|
) : UnlimitedKVCache<K, V>, KeyValueRepo<K, V> by kvParent {
|
||||||
|
protected val syncMutex = Mutex()
|
||||||
|
|
||||||
|
override suspend fun set(toSet: Map<K, V>) {
|
||||||
|
syncMutex.withLock {
|
||||||
|
kvParent.set(toSet)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun unset(toUnset: List<K>) {
|
||||||
|
syncMutex.withLock {
|
||||||
|
kvParent.unset(toUnset)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,24 +1,6 @@
|
|||||||
package dev.inmo.micro_utils.repos.cache.cache
|
package dev.inmo.micro_utils.repos.cache.cache
|
||||||
|
|
||||||
import dev.inmo.micro_utils.repos.KeyValueRepo
|
/**
|
||||||
import dev.inmo.micro_utils.repos.MapKeyValueRepo
|
* This interface declares that current type of [KVCache] will contains all the data all the time of its life
|
||||||
import kotlinx.coroutines.sync.Mutex
|
*/
|
||||||
import kotlinx.coroutines.sync.withLock
|
interface UnlimitedKVCache<K, V> : KVCache<K, V>
|
||||||
|
|
||||||
open class UnlimitedKVCache<K, V>(
|
|
||||||
private val kvParent: KeyValueRepo<K, V> = MapKeyValueRepo<K, V>()
|
|
||||||
) : KVCache<K, V>, KeyValueRepo<K, V> by kvParent {
|
|
||||||
protected val syncMutex = Mutex()
|
|
||||||
|
|
||||||
override suspend fun set(toSet: Map<K, V>) {
|
|
||||||
syncMutex.withLock {
|
|
||||||
kvParent.set(toSet)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun unset(toUnset: List<K>) {
|
|
||||||
syncMutex.withLock {
|
|
||||||
kvParent.unset(toUnset)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -1,31 +1,73 @@
|
|||||||
package dev.inmo.micro_utils.repos.cache.full
|
package dev.inmo.micro_utils.repos.cache.full
|
||||||
|
|
||||||
|
import dev.inmo.micro_utils.common.*
|
||||||
import dev.inmo.micro_utils.pagination.Pagination
|
import dev.inmo.micro_utils.pagination.Pagination
|
||||||
import dev.inmo.micro_utils.pagination.PaginationResult
|
import dev.inmo.micro_utils.pagination.PaginationResult
|
||||||
|
import dev.inmo.micro_utils.pagination.utils.doForAllWithNextPaging
|
||||||
import dev.inmo.micro_utils.repos.*
|
import dev.inmo.micro_utils.repos.*
|
||||||
import dev.inmo.micro_utils.repos.cache.ReadCRUDCacheRepo
|
import dev.inmo.micro_utils.repos.cache.*
|
||||||
import dev.inmo.micro_utils.repos.cache.cache.KVCache
|
import dev.inmo.micro_utils.repos.cache.cache.KVCache
|
||||||
|
import dev.inmo.micro_utils.repos.cache.cache.UnlimitedKVCache
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.flow.launchIn
|
import kotlinx.coroutines.flow.*
|
||||||
import kotlinx.coroutines.flow.onEach
|
|
||||||
|
|
||||||
|
|
||||||
open class FullReadCRUDCacheRepo<ObjectType, IdType>(
|
open class FullReadCRUDCacheRepo<ObjectType, IdType>(
|
||||||
parentRepo: ReadCRUDRepo<ObjectType, IdType>,
|
protected open val parentRepo: ReadCRUDRepo<ObjectType, IdType>,
|
||||||
kvCache: KVCache<IdType, ObjectType>,
|
protected open val kvCache: UnlimitedKVCache<IdType, ObjectType>,
|
||||||
idGetter: (ObjectType) -> IdType
|
protected open val idGetter: (ObjectType) -> IdType
|
||||||
) : ReadCRUDRepo<ObjectType, IdType>, ReadCRUDCacheRepo<ObjectType, IdType>(parentRepo, kvCache, idGetter) {
|
) : ReadCRUDRepo<ObjectType, IdType> {
|
||||||
override suspend fun getByPagination(pagination: Pagination): PaginationResult<ObjectType> {
|
protected inline fun <T> doOrTakeAndActualize(
|
||||||
return kvCache.values(pagination)
|
action: UnlimitedKVCache<IdType, ObjectType>.() -> Optional<T>,
|
||||||
|
actionElse: ReadCRUDRepo<ObjectType, IdType>.() -> T,
|
||||||
|
actualize: UnlimitedKVCache<IdType, ObjectType>.(T) -> Unit
|
||||||
|
): T {
|
||||||
|
kvCache.action().onPresented {
|
||||||
|
return it
|
||||||
|
}.onAbsent {
|
||||||
|
return parentRepo.actionElse().also {
|
||||||
|
kvCache.actualize(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
error("The result should be returned above")
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun count(): Long = kvCache.count()
|
protected suspend fun actualizeAll() {
|
||||||
|
kvCache.clear()
|
||||||
|
doForAllWithNextPaging {
|
||||||
|
parentRepo.getByPagination(it).also {
|
||||||
|
kvCache.set(it.results.associateBy { idGetter(it) })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun getByPagination(pagination: Pagination): PaginationResult<ObjectType> = doOrTakeAndActualize(
|
||||||
|
{ values(pagination).takeIf { it.results.isNotEmpty() }.optionalOrAbsentIfNull },
|
||||||
|
{ getByPagination(pagination) },
|
||||||
|
{ if (it.results.isNotEmpty()) actualizeAll() }
|
||||||
|
)
|
||||||
|
|
||||||
|
override suspend fun count(): Long = doOrTakeAndActualize(
|
||||||
|
{ count().takeIf { it != 0L }.optionalOrAbsentIfNull },
|
||||||
|
{ count() },
|
||||||
|
{ if (it != 0L) actualizeAll() }
|
||||||
|
)
|
||||||
|
|
||||||
|
override suspend fun contains(id: IdType): Boolean = doOrTakeAndActualize(
|
||||||
|
{ contains(id).takeIf { it }.optionalOrAbsentIfNull },
|
||||||
|
{ contains(id) },
|
||||||
|
{ if (it) parentRepo.getById(id) ?.let { set(id, it) } }
|
||||||
|
)
|
||||||
|
|
||||||
|
override suspend fun getById(id: IdType): ObjectType? {
|
||||||
|
TODO("Not yet implemented")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
open class FullCRUDCacheRepo<ObjectType, IdType, InputValueType>(
|
open class FullCRUDCacheRepo<ObjectType, IdType, InputValueType>(
|
||||||
parentRepo: CRUDRepo<ObjectType, IdType, InputValueType>,
|
override val parentRepo: CRUDRepo<ObjectType, IdType, InputValueType>,
|
||||||
kvCache: KVCache<IdType, ObjectType>,
|
kvCache: UnlimitedKVCache<IdType, ObjectType>,
|
||||||
scope: CoroutineScope = CoroutineScope(Dispatchers.Default),
|
scope: CoroutineScope = CoroutineScope(Dispatchers.Default),
|
||||||
idGetter: (ObjectType) -> IdType
|
idGetter: (ObjectType) -> IdType
|
||||||
) : FullReadCRUDCacheRepo<ObjectType, IdType>(
|
) : FullReadCRUDCacheRepo<ObjectType, IdType>(
|
||||||
@ -33,9 +75,10 @@ open class FullCRUDCacheRepo<ObjectType, IdType, InputValueType>(
|
|||||||
kvCache,
|
kvCache,
|
||||||
idGetter
|
idGetter
|
||||||
),
|
),
|
||||||
CRUDRepo<ObjectType, IdType, InputValueType>,
|
WriteCRUDRepo<ObjectType, IdType, InputValueType> by WriteCRUDCacheRepo(
|
||||||
WriteCRUDRepo<ObjectType, IdType, InputValueType> by parentRepo {
|
parentRepo,
|
||||||
protected val onNewJob = parentRepo.newObjectsFlow.onEach { kvCache.set(idGetter(it), it) }.launchIn(scope)
|
kvCache,
|
||||||
protected val onUpdatedJob = parentRepo.updatedObjectsFlow.onEach { kvCache.set(idGetter(it), it) }.launchIn(scope)
|
scope,
|
||||||
protected val onRemoveJob = parentRepo.deletedObjectsIdsFlow.onEach { kvCache.unset(it) }.launchIn(scope)
|
idGetter
|
||||||
}
|
),
|
||||||
|
CRUDRepo<ObjectType, IdType, InputValueType>
|
||||||
|
90
repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/full/FullKeyValueCacheRepo.kt
vendored
Normal file
90
repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/full/FullKeyValueCacheRepo.kt
vendored
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
package dev.inmo.micro_utils.repos.cache.full
|
||||||
|
|
||||||
|
import dev.inmo.micro_utils.common.*
|
||||||
|
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.UnlimitedKVCache
|
||||||
|
import dev.inmo.micro_utils.repos.pagination.getAll
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.flow.*
|
||||||
|
|
||||||
|
open class FullReadKeyValueCacheRepo<Key,Value>(
|
||||||
|
protected open val parentRepo: ReadKeyValueRepo<Key, Value>,
|
||||||
|
protected open val kvCache: UnlimitedKVCache<Key, Value>,
|
||||||
|
) : ReadKeyValueRepo<Key, Value> {
|
||||||
|
protected inline fun <T> doOrTakeAndActualize(
|
||||||
|
action: UnlimitedKVCache<Key, Value>.() -> Optional<T>,
|
||||||
|
actionElse: ReadKeyValueRepo<Key, Value>.() -> T,
|
||||||
|
actualize: UnlimitedKVCache<Key, Value>.(T) -> Unit
|
||||||
|
): T {
|
||||||
|
kvCache.action().onPresented {
|
||||||
|
return it
|
||||||
|
}.onAbsent {
|
||||||
|
return parentRepo.actionElse().also {
|
||||||
|
kvCache.actualize(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
error("The result should be returned above")
|
||||||
|
}
|
||||||
|
protected suspend fun actualizeAll() {
|
||||||
|
kvCache.clear()
|
||||||
|
kvCache.set(parentRepo.getAll { keys(it) }.toMap())
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun get(k: Key): Value? = doOrTakeAndActualize(
|
||||||
|
{ get(k) ?.optional ?: Optional.absent() },
|
||||||
|
{ get(k) },
|
||||||
|
{ set(k, it ?: return@doOrTakeAndActualize) }
|
||||||
|
)
|
||||||
|
|
||||||
|
override suspend fun values(pagination: Pagination, reversed: Boolean): PaginationResult<Value> = doOrTakeAndActualize(
|
||||||
|
{ values(pagination, reversed).takeIf { it.results.isNotEmpty() }.optionalOrAbsentIfNull },
|
||||||
|
{ values(pagination, reversed) },
|
||||||
|
{ if (it.results.isNotEmpty()) actualizeAll() }
|
||||||
|
)
|
||||||
|
|
||||||
|
override suspend fun count(): Long = doOrTakeAndActualize(
|
||||||
|
{ count().takeIf { it != 0L }.optionalOrAbsentIfNull },
|
||||||
|
{ count() },
|
||||||
|
{ if (it != 0L) actualizeAll() }
|
||||||
|
)
|
||||||
|
|
||||||
|
override suspend fun contains(key: Key): Boolean = doOrTakeAndActualize(
|
||||||
|
{ contains(key).takeIf { it }.optionalOrAbsentIfNull },
|
||||||
|
{ contains(key) },
|
||||||
|
{ if (it) parentRepo.get(key) ?.also { kvCache.set(key, it) } }
|
||||||
|
)
|
||||||
|
|
||||||
|
override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult<Key> = doOrTakeAndActualize(
|
||||||
|
{ keys(pagination, reversed).takeIf { it.results.isNotEmpty() }.optionalOrAbsentIfNull },
|
||||||
|
{ keys(pagination, reversed) },
|
||||||
|
{ if (it.results.isNotEmpty()) actualizeAll() }
|
||||||
|
)
|
||||||
|
|
||||||
|
override suspend fun keys(v: Value, pagination: Pagination, reversed: Boolean): PaginationResult<Key> = doOrTakeAndActualize(
|
||||||
|
{ keys(v, pagination, reversed).takeIf { it.results.isNotEmpty() }.optionalOrAbsentIfNull },
|
||||||
|
{ parentRepo.keys(v, pagination, reversed) },
|
||||||
|
{ if (it.results.isNotEmpty()) actualizeAll() }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
open class FullWriteKeyValueCacheRepo<Key,Value>(
|
||||||
|
protected open val parentRepo: WriteKeyValueRepo<Key, Value>,
|
||||||
|
protected open val kvCache: UnlimitedKVCache<Key, Value>,
|
||||||
|
scope: CoroutineScope = CoroutineScope(Dispatchers.Default)
|
||||||
|
) : WriteKeyValueRepo<Key, Value> by parentRepo {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
open class FullKeyValueCacheRepo<Key,Value>(
|
||||||
|
parentRepo: KeyValueRepo<Key, Value>,
|
||||||
|
kvCache: UnlimitedKVCache<Key, Value>,
|
||||||
|
scope: CoroutineScope = CoroutineScope(Dispatchers.Default)
|
||||||
|
) : FullWriteKeyValueCacheRepo<Key,Value>(parentRepo, kvCache, scope),
|
||||||
|
KeyValueRepo<Key,Value>,
|
||||||
|
ReadKeyValueRepo<Key, Value> by FullReadKeyValueCacheRepo(parentRepo, kvCache) {
|
||||||
|
override suspend fun unsetWithValues(toUnset: List<Value>) = parentRepo.unsetWithValues(toUnset)
|
||||||
|
}
|
125
repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/full/FullKeyValuesCacheRepo.kt
vendored
Normal file
125
repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/full/FullKeyValuesCacheRepo.kt
vendored
Normal file
@ -0,0 +1,125 @@
|
|||||||
|
package dev.inmo.micro_utils.repos.cache.full
|
||||||
|
|
||||||
|
import dev.inmo.micro_utils.common.*
|
||||||
|
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.UnlimitedKVCache
|
||||||
|
import dev.inmo.micro_utils.repos.pagination.getAll
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.flow.*
|
||||||
|
|
||||||
|
open class FullReadKeyValuesCacheRepo<Key,Value>(
|
||||||
|
protected open val parentRepo: ReadKeyValuesRepo<Key, Value>,
|
||||||
|
protected open val kvCache: UnlimitedKVCache<Key, List<Value>>,
|
||||||
|
) : ReadKeyValuesRepo<Key, Value> {
|
||||||
|
protected inline fun <T> doOrTakeAndActualize(
|
||||||
|
action: UnlimitedKVCache<Key, List<Value>>.() -> Optional<T>,
|
||||||
|
actionElse: ReadKeyValuesRepo<Key, Value>.() -> T,
|
||||||
|
actualize: UnlimitedKVCache<Key, List<Value>>.(T) -> Unit
|
||||||
|
): T {
|
||||||
|
kvCache.action().onPresented {
|
||||||
|
return it
|
||||||
|
}.onAbsent {
|
||||||
|
return parentRepo.actionElse().also {
|
||||||
|
kvCache.actualize(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
error("The result should be returned above")
|
||||||
|
}
|
||||||
|
|
||||||
|
protected suspend fun actualizeKey(k: Key) {
|
||||||
|
kvCache.set(k, parentRepo.getAll(k))
|
||||||
|
}
|
||||||
|
|
||||||
|
protected suspend fun actualizeAll() {
|
||||||
|
doAllWithCurrentPaging { kvCache.keys(it).also { kvCache.unset(it.results) } }
|
||||||
|
kvCache.set(parentRepo.getAll())
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun get(k: Key, pagination: Pagination, reversed: Boolean): PaginationResult<Value> {
|
||||||
|
return doOrTakeAndActualize(
|
||||||
|
{
|
||||||
|
get(k) ?.paginate(
|
||||||
|
pagination.let { if (reversed) it.reverse(count(k)) else it }
|
||||||
|
) ?.let {
|
||||||
|
if (reversed) it.copy(results = it.results.reversed()) else it
|
||||||
|
}.optionalOrAbsentIfNull
|
||||||
|
},
|
||||||
|
{ get(k, pagination, reversed) },
|
||||||
|
{ actualizeKey(k) }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult<Key> {
|
||||||
|
return doOrTakeAndActualize(
|
||||||
|
{
|
||||||
|
kvCache.keys(pagination, reversed).takeIf { it.results.isNotEmpty() }.optionalOrAbsentIfNull
|
||||||
|
},
|
||||||
|
{ parentRepo.keys(pagination, reversed) },
|
||||||
|
{ actualizeAll() }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun count(): Long = doOrTakeAndActualize(
|
||||||
|
{ count().takeIf { it != 0L }.optionalOrAbsentIfNull },
|
||||||
|
{ count() },
|
||||||
|
{ if (it != 0L) actualizeAll() }
|
||||||
|
)
|
||||||
|
|
||||||
|
override suspend fun count(k: Key): Long = doOrTakeAndActualize(
|
||||||
|
{ count().takeIf { it != 0L }.optionalOrAbsentIfNull },
|
||||||
|
{ count() },
|
||||||
|
{ if (it != 0L) actualizeKey(k) }
|
||||||
|
)
|
||||||
|
|
||||||
|
override suspend fun contains(k: Key, v: Value): Boolean = doOrTakeAndActualize(
|
||||||
|
{ get(k) ?.contains(v).takeIf { it == true }.optionalOrAbsentIfNull },
|
||||||
|
{ contains(k, v) },
|
||||||
|
{ if (it) actualizeKey(k) }
|
||||||
|
)
|
||||||
|
|
||||||
|
override suspend fun contains(k: Key): Boolean = doOrTakeAndActualize(
|
||||||
|
{ contains(k).takeIf { it }.optionalOrAbsentIfNull },
|
||||||
|
{ contains(k) },
|
||||||
|
{ if (it) actualizeKey(k) }
|
||||||
|
)
|
||||||
|
|
||||||
|
override suspend fun keys(
|
||||||
|
v: Value,
|
||||||
|
pagination: Pagination,
|
||||||
|
reversed: Boolean
|
||||||
|
): PaginationResult<Key> = doOrTakeAndActualize(
|
||||||
|
{
|
||||||
|
val keys = getAllWithNextPaging { keys(it) }.filter { get(it) ?.contains(v) == true }.optionallyReverse(reversed)
|
||||||
|
if (keys.isNotEmpty()) {
|
||||||
|
keys.paginate(pagination.optionallyReverse(keys.size, reversed)).takeIf { it.results.isNotEmpty() }.optionalOrAbsentIfNull
|
||||||
|
} else {
|
||||||
|
Optional.absent()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{ parentRepo.keys(v, pagination, reversed) },
|
||||||
|
{ if (it.results.isNotEmpty()) actualizeAll() }
|
||||||
|
)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
open class FullWriteKeyValuesCacheRepo<Key,Value>(
|
||||||
|
protected open val parentRepo: WriteKeyValueRepo<Key, Value>,
|
||||||
|
protected open val kvCache: UnlimitedKVCache<Key, Value>,
|
||||||
|
scope: CoroutineScope = CoroutineScope(Dispatchers.Default)
|
||||||
|
) : WriteKeyValueRepo<Key, Value> by parentRepo {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
open class FullKeyValuesCacheRepo<Key,Value>(
|
||||||
|
parentRepo: KeyValueRepo<Key, Value>,
|
||||||
|
kvCache: UnlimitedKVCache<Key, Value>,
|
||||||
|
scope: CoroutineScope = CoroutineScope(Dispatchers.Default)
|
||||||
|
) : FullWriteKeyValueCacheRepo<Key,Value>(parentRepo, kvCache, scope),
|
||||||
|
KeyValueRepo<Key,Value>,
|
||||||
|
ReadKeyValueRepo<Key, Value> by FullReadKeyValueCacheRepo(parentRepo, kvCache) {
|
||||||
|
override suspend fun unsetWithValues(toUnset: List<Value>) = parentRepo.unsetWithValues(toUnset)
|
||||||
|
}
|
@ -48,6 +48,10 @@ interface KeyValueRepo<Key, Value> : ReadKeyValueRepo<Key, Value>, WriteKeyValue
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
suspend fun clear() {
|
||||||
|
doAllWithCurrentPaging { keys(it).also { unset(it.results) } }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
typealias StandardKeyValueRepo<Key,Value> = KeyValueRepo<Key, Value>
|
typealias StandardKeyValueRepo<Key,Value> = KeyValueRepo<Key, Value>
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user