fixes in locks of caches repos

This commit is contained in:
InsanusMokrassar 2023-08-29 15:06:55 +06:00
parent e0e57f0336
commit 3d113dd31e
4 changed files with 43 additions and 12 deletions

View File

@ -2,6 +2,10 @@
## 0.20.3
* `Repos`:
* `Cache`:
* Fixes in locks of caches
## 0.20.2
* All main repos uses `SmartRWLocker`

View File

@ -28,9 +28,18 @@ open class FullReadCRUDCacheRepo<ObjectType, IdType>(
kvCache.action().onPresented { return it }
}
return parentRepo.actionElse().also {
locker.withWriteLock { kvCache.actualize(it) }
kvCache.actualize(it)
}
}
protected suspend inline fun <T> doOrTakeAndActualizeWithWriteLock(
action: KeyValueRepo<IdType, ObjectType>.() -> Optional<T>,
actionElse: ReadCRUDRepo<ObjectType, IdType>.() -> T,
actualize: KeyValueRepo<IdType, ObjectType>.(T) -> Unit
): T = doOrTakeAndActualize(
action = action,
actionElse = actionElse,
actualize = { locker.withWriteLock { actualize(it) } }
)
protected open suspend fun actualizeAll() {
locker.withWriteLock { kvCache.actualizeAll(parentRepo) }
@ -54,22 +63,22 @@ open class FullReadCRUDCacheRepo<ObjectType, IdType>(
{ if (it != 0L) actualizeAll() }
)
override suspend fun contains(id: IdType): Boolean = doOrTakeAndActualize(
override suspend fun contains(id: IdType): Boolean = doOrTakeAndActualizeWithWriteLock(
{ contains(id).takeIf { it }.optionalOrAbsentIfNull },
{ contains(id) },
{ if (it) parentRepo.getById(id) ?.let { set(id, it) } }
{ if (it) parentRepo.getById(id) ?.let { kvCache.set(id, it) } }
)
override suspend fun getAll(): Map<IdType, ObjectType> = doOrTakeAndActualize(
override suspend fun getAll(): Map<IdType, ObjectType> = doOrTakeAndActualizeWithWriteLock(
{ getAll().takeIf { it.isNotEmpty() }.optionalOrAbsentIfNull },
{ getAll() },
{ kvCache.actualizeAll(clear = true) { it } }
)
override suspend fun getById(id: IdType): ObjectType? = doOrTakeAndActualize(
override suspend fun getById(id: IdType): ObjectType? = doOrTakeAndActualizeWithWriteLock(
{ get(id) ?.optional ?: Optional.absent() },
{ getById(id) },
{ it ?.let { set(idGetter(it), it) } }
{ it ?.let { kvCache.set(idGetter(it), it) } }
)
override suspend fun invalidate() {

View File

@ -28,9 +28,18 @@ open class FullReadKeyValueCacheRepo<Key,Value>(
kvCache.action().onPresented { return it }
}
return parentRepo.actionElse().also {
locker.withWriteLock { kvCache.actualize(it) }
kvCache.actualize(it)
}
}
protected suspend inline fun <T> doOrTakeAndActualizeWithWriteLock(
action: KeyValueRepo<Key, Value>.() -> Optional<T>,
actionElse: ReadKeyValueRepo<Key, Value>.() -> T,
actualize: KeyValueRepo<Key, Value>.(T) -> Unit
): T = doOrTakeAndActualize(
action = action,
actionElse = actionElse,
actualize = { locker.withWriteLock { actualize(it) } }
)
protected open suspend fun actualizeAll() {
locker.withWriteLock {
kvCache.clear()
@ -38,10 +47,10 @@ open class FullReadKeyValueCacheRepo<Key,Value>(
}
}
override suspend fun get(k: Key): Value? = doOrTakeAndActualize(
override suspend fun get(k: Key): Value? = doOrTakeAndActualizeWithWriteLock(
{ get(k) ?.optional ?: Optional.absent() },
{ get(k) },
{ set(k, it ?: return@doOrTakeAndActualize) }
{ kvCache.set(k, it ?: return@doOrTakeAndActualizeWithWriteLock) }
)
override suspend fun values(pagination: Pagination, reversed: Boolean): PaginationResult<Value> = doOrTakeAndActualize(
@ -56,13 +65,13 @@ open class FullReadKeyValueCacheRepo<Key,Value>(
{ if (it != 0L) actualizeAll() }
)
override suspend fun contains(key: Key): Boolean = doOrTakeAndActualize(
override suspend fun contains(key: Key): Boolean = doOrTakeAndActualizeWithWriteLock(
{ contains(key).takeIf { it }.optionalOrAbsentIfNull },
{ contains(key) },
{ if (it) parentRepo.get(key) ?.also { kvCache.set(key, it) } }
)
override suspend fun getAll(): Map<Key, Value> = doOrTakeAndActualize(
override suspend fun getAll(): Map<Key, Value> = doOrTakeAndActualizeWithWriteLock(
{ getAll().takeIf { it.isNotEmpty() }.optionalOrAbsentIfNull },
{ getAll() },
{ kvCache.actualizeAll(clear = true) { it } }

View File

@ -27,9 +27,18 @@ open class FullReadKeyValuesCacheRepo<Key,Value>(
kvCache.action().onPresented { return it }
}
return parentRepo.actionElse().also {
locker.withWriteLock { kvCache.actualize(it) }
kvCache.actualize(it)
}
}
protected suspend inline fun <T> doOrTakeAndActualizeWithWriteLock(
action: KeyValueRepo<Key, List<Value>>.() -> Optional<T>,
actionElse: ReadKeyValuesRepo<Key, Value>.() -> T,
actualize: KeyValueRepo<Key, List<Value>>.(T) -> Unit
): T = doOrTakeAndActualize(
action = action,
actionElse = actionElse,
actualize = { locker.withWriteLock { actualize(it) } }
)
protected open suspend fun actualizeKey(k: Key) {
locker.withWriteLock {