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>,
scope: CoroutineScope = CoroutineScope(Dispatchers.Default),
skipStartInvalidate: Boolean = false,
locker: SmartRWLocker = SmartRWLocker(),
locker: SmartRWLocker = SmartRWLocker(writeIsLocked = !skipStartInvalidate),
idGetter: (ObjectType) -> IdType
) : FullReadCRUDCacheRepo<ObjectType, IdType>(
parentRepo,
@ -116,10 +116,23 @@ open class FullCRUDCacheRepo<ObjectType, IdType, InputValueType>(
CRUDRepo<ObjectType, IdType, InputValueType> {
init {
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() {
actualizeAll()
}

View File

@ -130,7 +130,7 @@ open class FullKeyValueCacheRepo<Key,Value>(
kvCache: KeyValueRepo<Key, Value>,
scope: CoroutineScope = CoroutineScope(Dispatchers.Default),
skipStartInvalidate: Boolean = false,
locker: SmartRWLocker = SmartRWLocker()
locker: SmartRWLocker = SmartRWLocker(writeIsLocked = !skipStartInvalidate),
) : FullWriteKeyValueCacheRepo<Key,Value>(parentRepo, kvCache, scope),
KeyValueRepo<Key,Value>,
ReadKeyValueRepo<Key, Value> by FullReadKeyValueCacheRepo(
@ -140,12 +140,25 @@ open class FullKeyValueCacheRepo<Key,Value>(
) {
init {
if (!skipStartInvalidate) {
scope.launchSafelyWithoutExceptions { invalidate() }
scope.launchSafelyWithoutExceptions {
if (locker.writeMutex.isLocked) {
initialInvalidate()
} else {
invalidate()
}
}
}
}
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() {
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.cache.util.ActualizeAllClearMode
import dev.inmo.micro_utils.repos.cache.util.actualizeAll
import dev.inmo.micro_utils.repos.pagination.maxPagePagination
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.*
@ -203,14 +204,19 @@ open class FullKeyValuesCacheRepo<Key,Value>(
kvCache: KeyValueRepo<Key, List<Value>>,
scope: CoroutineScope = CoroutineScope(Dispatchers.Default),
skipStartInvalidate: Boolean = false,
locker: SmartRWLocker = SmartRWLocker(),
) : //FullWriteKeyValuesCacheRepo<Key, Value>(parentRepo, kvCache, scope, locker),
KeyValuesRepo<Key, Value>,
locker: SmartRWLocker = SmartRWLocker(writeIsLocked = !skipStartInvalidate),
) : KeyValuesRepo<Key, Value>,
FullReadKeyValuesCacheRepo<Key, Value>(parentRepo, kvCache, locker),
WriteKeyValuesRepo<Key, Value> by parentRepo {
init {
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() {
kvCache.actualizeAll(parentRepo, locker = locker)
}

View File

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

View File

@ -24,7 +24,9 @@ class ReadMapKeyValueRepo<Key, Value>(
) : ReadKeyValueRepo<Key, Value> {
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(
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.withWriteLock
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.reverse
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> {
val keys = locker.withReadAcquire {
map.keys