This commit is contained in:
2023-03-10 18:37:48 +06:00
parent afc6aeea15
commit eeebbff70d
30 changed files with 154 additions and 5 deletions

View File

@@ -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()

View File

@@ -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()
}

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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) },

View File

@@ -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) },