start add full repos caches

This commit is contained in:
InsanusMokrassar 2022-06-29 19:43:58 +06:00
parent a548b00979
commit 540d5cce7c
2 changed files with 42 additions and 1 deletions

View File

@ -0,0 +1,41 @@
package dev.inmo.micro_utils.repos.cache.full
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.ReadCRUDCacheRepo
import dev.inmo.micro_utils.repos.cache.cache.KVCache
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
open class FullReadCRUDCacheRepo<ObjectType, IdType>(
parentRepo: ReadCRUDRepo<ObjectType, IdType>,
kvCache: KVCache<IdType, ObjectType>,
idGetter: (ObjectType) -> IdType
) : ReadCRUDRepo<ObjectType, IdType>, ReadCRUDCacheRepo<ObjectType, IdType>(parentRepo, kvCache, idGetter) {
override suspend fun getByPagination(pagination: Pagination): PaginationResult<ObjectType> {
return kvCache.values(pagination)
}
override suspend fun count(): Long = kvCache.count()
}
open class FullCRUDCacheRepo<ObjectType, IdType, InputValueType>(
parentRepo: CRUDRepo<ObjectType, IdType, InputValueType>,
kvCache: KVCache<IdType, ObjectType>,
scope: CoroutineScope = CoroutineScope(Dispatchers.Default),
idGetter: (ObjectType) -> IdType
) : FullReadCRUDCacheRepo<ObjectType, IdType>(
parentRepo,
kvCache,
idGetter
),
CRUDRepo<ObjectType, IdType, InputValueType>,
WriteCRUDRepo<ObjectType, IdType, InputValueType> by parentRepo {
protected val onNewJob = parentRepo.newObjectsFlow.onEach { kvCache.set(idGetter(it), it) }.launchIn(scope)
protected val onUpdatedJob = parentRepo.updatedObjectsFlow.onEach { kvCache.set(idGetter(it), it) }.launchIn(scope)
protected val onRemoveJob = parentRepo.deletedObjectsIdsFlow.onEach { kvCache.unset(it) }.launchIn(scope)
}