mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2025-09-14 21:09:24 +00:00
getAll
This commit is contained in:
@@ -2,6 +2,7 @@ package dev.inmo.micro_utils.repos.cache
|
||||
|
||||
import dev.inmo.micro_utils.repos.*
|
||||
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
|
||||
import kotlinx.coroutines.flow.*
|
||||
@@ -15,6 +16,12 @@ open class ReadCRUDCacheRepo<ObjectType, IdType>(
|
||||
kvCache.set(id, it)
|
||||
})
|
||||
|
||||
override suspend fun getAll(): Map<IdType, ObjectType> {
|
||||
return kvCache.getAll().takeIf { it.size.toLong() == count() } ?: parentRepo.getAll().also {
|
||||
kvCache.actualizeAll(true) { it }
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun contains(id: IdType): Boolean = kvCache.contains(id) || parentRepo.contains(id)
|
||||
|
||||
override suspend fun invalidate() = kvCache.clear()
|
||||
|
@@ -24,6 +24,12 @@ open class ReadKeyValueCacheRepo<Key,Value>(
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun getAll(): Map<Key, Value> = kvCache.getAll().takeIf {
|
||||
it.size.toLong() == count()
|
||||
} ?: parentRepo.getAll().also {
|
||||
kvCache.set(it)
|
||||
}
|
||||
|
||||
override suspend fun invalidate() = kvCache.clear()
|
||||
}
|
||||
|
||||
|
@@ -52,6 +52,14 @@ open class AutoRecacheReadCRUDRepo<RegisteredObject, Id>(
|
||||
kvCache.contains(id)
|
||||
}
|
||||
|
||||
override suspend fun getAll(): Map<Id, RegisteredObject> = actionWrapper.wrap {
|
||||
originalRepo.getAll()
|
||||
}.onSuccess {
|
||||
kvCache.actualizeAll(clear = true) { it }
|
||||
}.getOrElse {
|
||||
kvCache.getAll()
|
||||
}
|
||||
|
||||
override suspend fun count(): Long = actionWrapper.wrap {
|
||||
originalRepo.count()
|
||||
}.getOrElse {
|
||||
|
@@ -52,6 +52,8 @@ open class AutoRecacheReadKeyValueRepo<Id, RegisteredObject>(
|
||||
kvCache.contains(key)
|
||||
}
|
||||
|
||||
override suspend fun getAll(): Map<Id, RegisteredObject> = kvCache.getAll()
|
||||
|
||||
override suspend fun count(): Long = actionWrapper.wrap {
|
||||
originalRepo.count()
|
||||
}.getOrElse {
|
||||
|
@@ -58,6 +58,12 @@ open class FullReadCRUDCacheRepo<ObjectType, IdType>(
|
||||
{ if (it) parentRepo.getById(id) ?.let { set(id, it) } }
|
||||
)
|
||||
|
||||
override suspend fun getAll(): Map<IdType, ObjectType> = doOrTakeAndActualize(
|
||||
{ getAll().takeIf { it.isNotEmpty() }.optionalOrAbsentIfNull },
|
||||
{ getAll() },
|
||||
{ kvCache.actualizeAll(clear = true) { it } }
|
||||
)
|
||||
|
||||
override suspend fun getById(id: IdType): ObjectType? = doOrTakeAndActualize(
|
||||
{ get(id) ?.optional ?: Optional.absent() },
|
||||
{ getById(id) },
|
||||
|
@@ -9,7 +9,6 @@ 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
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.flow.*
|
||||
|
||||
open class FullReadKeyValueCacheRepo<Key,Value>(
|
||||
@@ -59,6 +58,12 @@ open class FullReadKeyValueCacheRepo<Key,Value>(
|
||||
{ if (it) parentRepo.get(key) ?.also { kvCache.set(key, it) } }
|
||||
)
|
||||
|
||||
override suspend fun getAll(): Map<Key, Value> = doOrTakeAndActualize(
|
||||
{ getAll().takeIf { it.isNotEmpty() }.optionalOrAbsentIfNull },
|
||||
{ getAll() },
|
||||
{ kvCache.actualizeAll(clear = true) { it } }
|
||||
)
|
||||
|
||||
override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult<Key> = doOrTakeAndActualize(
|
||||
{ keys(pagination, reversed).takeIf { it.results.isNotEmpty() }.optionalOrAbsentIfNull },
|
||||
{ keys(pagination, reversed) },
|
||||
|
Reference in New Issue
Block a user