fix of full caches initial actualization and realize MapKeyValuesRepo methods getAll

This commit is contained in:
InsanusMokrassar 2024-08-02 23:36:08 +06:00
parent 8713fb04c5
commit 36093b9741
6 changed files with 66 additions and 9 deletions

View File

@ -98,7 +98,7 @@ open class FullCRUDCacheRepo<ObjectType, IdType, InputValueType>(
kvCache: KeyValueRepo<IdType, ObjectType>, kvCache: KeyValueRepo<IdType, ObjectType>,
scope: CoroutineScope = CoroutineScope(Dispatchers.Default), scope: CoroutineScope = CoroutineScope(Dispatchers.Default),
skipStartInvalidate: Boolean = false, skipStartInvalidate: Boolean = false,
locker: SmartRWLocker = SmartRWLocker(), locker: SmartRWLocker = SmartRWLocker(writeIsLocked = !skipStartInvalidate),
idGetter: (ObjectType) -> IdType idGetter: (ObjectType) -> IdType
) : FullReadCRUDCacheRepo<ObjectType, IdType>( ) : FullReadCRUDCacheRepo<ObjectType, IdType>(
parentRepo, parentRepo,
@ -116,10 +116,23 @@ open class FullCRUDCacheRepo<ObjectType, IdType, InputValueType>(
CRUDRepo<ObjectType, IdType, InputValueType> { CRUDRepo<ObjectType, IdType, InputValueType> {
init { init {
if (!skipStartInvalidate) { if (!skipStartInvalidate) {
scope.launchSafelyWithoutExceptions { invalidate() } scope.launchSafelyWithoutExceptions {
if (locker.writeMutex.isLocked) {
initialInvalidate()
} else {
invalidate()
}
}
} }
} }
protected open suspend fun initialInvalidate() {
try {
kvCache.actualizeAll(parentRepo, locker = null)
} finally {
locker.unlockWrite()
}
}
override suspend fun invalidate() { override suspend fun invalidate() {
actualizeAll() actualizeAll()
} }

View File

@ -130,7 +130,7 @@ open class FullKeyValueCacheRepo<Key,Value>(
kvCache: KeyValueRepo<Key, Value>, kvCache: KeyValueRepo<Key, Value>,
scope: CoroutineScope = CoroutineScope(Dispatchers.Default), scope: CoroutineScope = CoroutineScope(Dispatchers.Default),
skipStartInvalidate: Boolean = false, skipStartInvalidate: Boolean = false,
locker: SmartRWLocker = SmartRWLocker() locker: SmartRWLocker = SmartRWLocker(writeIsLocked = !skipStartInvalidate),
) : FullWriteKeyValueCacheRepo<Key,Value>(parentRepo, kvCache, scope), ) : FullWriteKeyValueCacheRepo<Key,Value>(parentRepo, kvCache, scope),
KeyValueRepo<Key,Value>, KeyValueRepo<Key,Value>,
ReadKeyValueRepo<Key, Value> by FullReadKeyValueCacheRepo( ReadKeyValueRepo<Key, Value> by FullReadKeyValueCacheRepo(
@ -140,12 +140,25 @@ open class FullKeyValueCacheRepo<Key,Value>(
) { ) {
init { init {
if (!skipStartInvalidate) { if (!skipStartInvalidate) {
scope.launchSafelyWithoutExceptions { invalidate() } scope.launchSafelyWithoutExceptions {
if (locker.writeMutex.isLocked) {
initialInvalidate()
} else {
invalidate()
}
}
} }
} }
override suspend fun unsetWithValues(toUnset: List<Value>) = parentRepo.unsetWithValues(toUnset) override suspend fun unsetWithValues(toUnset: List<Value>) = parentRepo.unsetWithValues(toUnset)
protected open suspend fun initialInvalidate() {
try {
kvCache.actualizeAll(parentRepo, locker = null)
} finally {
locker.unlockWrite()
}
}
override suspend fun invalidate() { override suspend fun invalidate() {
kvCache.actualizeAll(parentRepo, locker) kvCache.actualizeAll(parentRepo, locker)
} }

View File

@ -10,6 +10,7 @@ import dev.inmo.micro_utils.pagination.utils.*
import dev.inmo.micro_utils.repos.* import dev.inmo.micro_utils.repos.*
import dev.inmo.micro_utils.repos.cache.util.ActualizeAllClearMode import dev.inmo.micro_utils.repos.cache.util.ActualizeAllClearMode
import dev.inmo.micro_utils.repos.cache.util.actualizeAll import dev.inmo.micro_utils.repos.cache.util.actualizeAll
import dev.inmo.micro_utils.repos.pagination.maxPagePagination
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.* import kotlinx.coroutines.flow.*
@ -203,14 +204,19 @@ open class FullKeyValuesCacheRepo<Key,Value>(
kvCache: KeyValueRepo<Key, List<Value>>, kvCache: KeyValueRepo<Key, List<Value>>,
scope: CoroutineScope = CoroutineScope(Dispatchers.Default), scope: CoroutineScope = CoroutineScope(Dispatchers.Default),
skipStartInvalidate: Boolean = false, skipStartInvalidate: Boolean = false,
locker: SmartRWLocker = SmartRWLocker(), locker: SmartRWLocker = SmartRWLocker(writeIsLocked = !skipStartInvalidate),
) : //FullWriteKeyValuesCacheRepo<Key, Value>(parentRepo, kvCache, scope, locker), ) : KeyValuesRepo<Key, Value>,
KeyValuesRepo<Key, Value>,
FullReadKeyValuesCacheRepo<Key, Value>(parentRepo, kvCache, locker), FullReadKeyValuesCacheRepo<Key, Value>(parentRepo, kvCache, locker),
WriteKeyValuesRepo<Key, Value> by parentRepo { WriteKeyValuesRepo<Key, Value> by parentRepo {
init { init {
if (!skipStartInvalidate) { if (!skipStartInvalidate) {
scope.launchSafelyWithoutExceptions { invalidate() } scope.launchSafelyWithoutExceptions {
if (locker.writeMutex.isLocked) {
initialInvalidate()
} else {
invalidate()
}
}
} }
} }
@ -222,6 +228,13 @@ open class FullKeyValuesCacheRepo<Key,Value>(
} }
} }
protected open suspend fun initialInvalidate() {
try {
kvCache.actualizeAll(parentRepo, locker = null)
} finally {
locker.unlockWrite()
}
}
override suspend fun invalidate() { override suspend fun invalidate() {
kvCache.actualizeAll(parentRepo, locker = locker) kvCache.actualizeAll(parentRepo, locker = locker)
} }

View File

@ -3,6 +3,7 @@ package full
import com.benasher44.uuid.uuid4 import com.benasher44.uuid.uuid4
import dev.inmo.micro_utils.repos.* import dev.inmo.micro_utils.repos.*
import dev.inmo.micro_utils.repos.cache.full.FullKeyValuesCacheRepo import dev.inmo.micro_utils.repos.cache.full.FullKeyValuesCacheRepo
import dev.inmo.micro_utils.repos.pagination.maxPagePagination
import korlibs.time.days import korlibs.time.days
import korlibs.time.years import korlibs.time.years
import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.runTest

View File

@ -24,7 +24,9 @@ class ReadMapKeyValueRepo<Key, Value>(
) : ReadKeyValueRepo<Key, Value> { ) : ReadKeyValueRepo<Key, Value> {
constructor(map: Map<Key, Value> = emptyMap()) : this(map, SmartRWLocker()) constructor(map: Map<Key, Value> = emptyMap()) : this(map, SmartRWLocker())
override suspend fun get(k: Key): Value? = locker.withReadAcquire { map[k] } override suspend fun get(k: Key): Value? = locker.withReadAcquire {
map[k]
}
override suspend fun values( override suspend fun values(
pagination: Pagination, pagination: Pagination,

View File

@ -4,6 +4,7 @@ import dev.inmo.micro_utils.coroutines.SmartRWLocker
import dev.inmo.micro_utils.coroutines.withReadAcquire import dev.inmo.micro_utils.coroutines.withReadAcquire
import dev.inmo.micro_utils.coroutines.withWriteLock import dev.inmo.micro_utils.coroutines.withWriteLock
import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.pagination.utils.optionallyReverse
import dev.inmo.micro_utils.pagination.utils.paginate import dev.inmo.micro_utils.pagination.utils.paginate
import dev.inmo.micro_utils.pagination.utils.reverse import dev.inmo.micro_utils.pagination.utils.reverse
import kotlinx.coroutines.flow.* import kotlinx.coroutines.flow.*
@ -33,6 +34,20 @@ class MapReadKeyValuesRepo<Key, Value>(
) )
} }
override suspend fun getAll(k: Key, reversed: Boolean): List<Value> {
return locker.withReadAcquire { map[k] ?.optionallyReverse(reversed) ?: return emptyList() }
}
override suspend fun getAll(reverseLists: Boolean): Map<Key, List<Value>> {
return locker.withReadAcquire {
if (reverseLists) {
map.mapValues { it.value.reversed() }
} else {
map.toMap()
}
}
}
override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult<Key> { override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult<Key> {
val keys = locker.withReadAcquire { val keys = locker.withReadAcquire {
map.keys map.keys