mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-12-23 00:57:15 +00:00
realize invalidate method in fallback cache repos
This commit is contained in:
parent
2645ea29d6
commit
3f6f6ebc2b
@ -16,7 +16,7 @@ import kotlinx.coroutines.launch
|
|||||||
import kotlin.time.Duration.Companion.seconds
|
import kotlin.time.Duration.Companion.seconds
|
||||||
|
|
||||||
open class AutoRecacheReadCRUDRepo<RegisteredObject, Id>(
|
open class AutoRecacheReadCRUDRepo<RegisteredObject, Id>(
|
||||||
protected val originalRepo: ReadCRUDRepo<RegisteredObject, Id>,
|
protected open val originalRepo: ReadCRUDRepo<RegisteredObject, Id>,
|
||||||
protected val scope: CoroutineScope,
|
protected val scope: CoroutineScope,
|
||||||
protected val kvCache: FullKVCache<Id, RegisteredObject> = FullKVCache(),
|
protected val kvCache: FullKVCache<Id, RegisteredObject> = FullKVCache(),
|
||||||
protected val recacheDelay: Long = 60.seconds.inWholeMilliseconds,
|
protected val recacheDelay: Long = 60.seconds.inWholeMilliseconds,
|
||||||
@ -25,9 +25,7 @@ open class AutoRecacheReadCRUDRepo<RegisteredObject, Id>(
|
|||||||
) : ReadCRUDRepo<RegisteredObject, Id>, FallbackCacheRepo {
|
) : ReadCRUDRepo<RegisteredObject, Id>, FallbackCacheRepo {
|
||||||
val autoUpdateJob = scope.launch {
|
val autoUpdateJob = scope.launch {
|
||||||
while (isActive) {
|
while (isActive) {
|
||||||
runCatchingSafely {
|
actualizeAll()
|
||||||
kvCache.actualizeAll(originalRepo)
|
|
||||||
}
|
|
||||||
|
|
||||||
delay(recacheDelay)
|
delay(recacheDelay)
|
||||||
}
|
}
|
||||||
@ -42,6 +40,12 @@ open class AutoRecacheReadCRUDRepo<RegisteredObject, Id>(
|
|||||||
idGetter: (RegisteredObject) -> Id
|
idGetter: (RegisteredObject) -> Id
|
||||||
) : this(originalRepo, scope, kvCache, recacheDelay, ActionWrapper.Timeouted(originalCallTimeoutMillis), idGetter)
|
) : this(originalRepo, scope, kvCache, recacheDelay, ActionWrapper.Timeouted(originalCallTimeoutMillis), idGetter)
|
||||||
|
|
||||||
|
protected suspend fun actualizeAll(): Result<Unit> {
|
||||||
|
return runCatchingSafely {
|
||||||
|
kvCache.actualizeAll(originalRepo)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override suspend fun contains(id: Id): Boolean = actionWrapper.wrap {
|
override suspend fun contains(id: Id): Boolean = actionWrapper.wrap {
|
||||||
originalRepo.contains(id)
|
originalRepo.contains(id)
|
||||||
}.getOrElse {
|
}.getOrElse {
|
||||||
@ -75,4 +79,8 @@ open class AutoRecacheReadCRUDRepo<RegisteredObject, Id>(
|
|||||||
}.getOrNull() ?.also {
|
}.getOrNull() ?.also {
|
||||||
kvCache.set(idGetter(it), it)
|
kvCache.set(idGetter(it), it)
|
||||||
} ?: kvCache.get(id)
|
} ?: kvCache.get(id)
|
||||||
|
|
||||||
|
override suspend fun invalidate() {
|
||||||
|
actualizeAll()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -58,4 +58,8 @@ open class AutoRecacheWriteCRUDRepo<RegisteredObject, Id, InputObject>(
|
|||||||
override suspend fun create(values: List<InputObject>): List<RegisteredObject> = originalRepo.create(values).onEach {
|
override suspend fun create(values: List<InputObject>): List<RegisteredObject> = originalRepo.create(values).onEach {
|
||||||
kvCache.set(idGetter(it), it)
|
kvCache.set(idGetter(it), it)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override suspend fun invalidate() {
|
||||||
|
kvCache.clear()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,9 +25,7 @@ open class AutoRecacheReadKeyValueRepo<Id, RegisteredObject>(
|
|||||||
) : ReadKeyValueRepo<Id, RegisteredObject>, FallbackCacheRepo {
|
) : ReadKeyValueRepo<Id, RegisteredObject>, FallbackCacheRepo {
|
||||||
val autoUpdateJob = scope.launch {
|
val autoUpdateJob = scope.launch {
|
||||||
while (isActive) {
|
while (isActive) {
|
||||||
runCatchingSafely {
|
actualizeAll()
|
||||||
kvCache.actualizeAll(originalRepo)
|
|
||||||
}
|
|
||||||
|
|
||||||
delay(recacheDelay)
|
delay(recacheDelay)
|
||||||
}
|
}
|
||||||
@ -42,6 +40,12 @@ open class AutoRecacheReadKeyValueRepo<Id, RegisteredObject>(
|
|||||||
idGetter: (RegisteredObject) -> Id
|
idGetter: (RegisteredObject) -> Id
|
||||||
) : this(originalRepo, scope, kvCache, recacheDelay, ActionWrapper.Timeouted(originalCallTimeoutMillis), idGetter)
|
) : this(originalRepo, scope, kvCache, recacheDelay, ActionWrapper.Timeouted(originalCallTimeoutMillis), idGetter)
|
||||||
|
|
||||||
|
protected suspend fun actualizeAll(): Result<Unit> {
|
||||||
|
return runCatchingSafely {
|
||||||
|
kvCache.actualizeAll(originalRepo)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override suspend fun contains(key: Id): Boolean = actionWrapper.wrap {
|
override suspend fun contains(key: Id): Boolean = actionWrapper.wrap {
|
||||||
originalRepo.contains(key)
|
originalRepo.contains(key)
|
||||||
}.getOrElse {
|
}.getOrElse {
|
||||||
@ -85,4 +89,8 @@ open class AutoRecacheReadKeyValueRepo<Id, RegisteredObject>(
|
|||||||
): PaginationResult<Id> = actionWrapper.wrap {
|
): PaginationResult<Id> = actionWrapper.wrap {
|
||||||
originalRepo.keys(v, pagination, reversed)
|
originalRepo.keys(v, pagination, reversed)
|
||||||
}.getOrElse { kvCache.keys(v, pagination, reversed) }
|
}.getOrElse { kvCache.keys(v, pagination, reversed) }
|
||||||
|
|
||||||
|
override suspend fun invalidate() {
|
||||||
|
actualizeAll()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -47,4 +47,8 @@ open class AutoRecacheWriteKeyValueRepo<Id, RegisteredObject>(
|
|||||||
).also {
|
).also {
|
||||||
kvCache.set(toSet)
|
kvCache.set(toSet)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override suspend fun invalidate() {
|
||||||
|
kvCache.clear()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,9 +32,7 @@ open class AutoRecacheReadKeyValuesRepo<Id, RegisteredObject>(
|
|||||||
) : ReadKeyValuesRepo<Id, RegisteredObject>, FallbackCacheRepo {
|
) : ReadKeyValuesRepo<Id, RegisteredObject>, FallbackCacheRepo {
|
||||||
val autoUpdateJob = scope.launch {
|
val autoUpdateJob = scope.launch {
|
||||||
while (isActive) {
|
while (isActive) {
|
||||||
runCatchingSafely {
|
actualizeAll()
|
||||||
kvCache.actualizeAll(originalRepo)
|
|
||||||
}
|
|
||||||
|
|
||||||
delay(recacheDelay)
|
delay(recacheDelay)
|
||||||
}
|
}
|
||||||
@ -49,6 +47,12 @@ open class AutoRecacheReadKeyValuesRepo<Id, RegisteredObject>(
|
|||||||
idGetter: (RegisteredObject) -> Id
|
idGetter: (RegisteredObject) -> Id
|
||||||
) : this(originalRepo, scope, kvCache, recacheDelay, ActionWrapper.Timeouted(originalCallTimeoutMillis), idGetter)
|
) : this(originalRepo, scope, kvCache, recacheDelay, ActionWrapper.Timeouted(originalCallTimeoutMillis), idGetter)
|
||||||
|
|
||||||
|
protected suspend fun actualizeAll(): Result<Unit> {
|
||||||
|
return runCatchingSafely {
|
||||||
|
kvCache.actualizeAll(originalRepo)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override suspend fun contains(k: Id): Boolean = actionWrapper.wrap {
|
override suspend fun contains(k: Id): Boolean = actionWrapper.wrap {
|
||||||
originalRepo.contains(k)
|
originalRepo.contains(k)
|
||||||
}.getOrElse {
|
}.getOrElse {
|
||||||
@ -136,4 +140,8 @@ open class AutoRecacheReadKeyValuesRepo<Id, RegisteredObject>(
|
|||||||
kvCache.set(k, ((kvCache.get(k) ?: return@also) + v).distinct())
|
kvCache.set(k, ((kvCache.get(k) ?: return@also) + v).distinct())
|
||||||
}) ?: (kvCache.get(k) ?.contains(v) == true)
|
}) ?: (kvCache.get(k) ?.contains(v) == true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override suspend fun invalidate() {
|
||||||
|
actualizeAll()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -79,4 +79,8 @@ open class AutoRecacheWriteKeyValuesRepo<Id, RegisteredObject>(
|
|||||||
kvCache.set(k, (kvCache.get(k) ?: return@forEach) + v)
|
kvCache.set(k, (kvCache.get(k) ?: return@forEach) + v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override suspend fun invalidate() {
|
||||||
|
kvCache.clear()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user