realize invalidate method in fallback cache repos

This commit is contained in:
InsanusMokrassar 2023-01-31 10:09:55 +06:00
parent 2645ea29d6
commit 3f6f6ebc2b
6 changed files with 46 additions and 10 deletions

View File

@ -16,7 +16,7 @@ import kotlinx.coroutines.launch
import kotlin.time.Duration.Companion.seconds
open class AutoRecacheReadCRUDRepo<RegisteredObject, Id>(
protected val originalRepo: ReadCRUDRepo<RegisteredObject, Id>,
protected open val originalRepo: ReadCRUDRepo<RegisteredObject, Id>,
protected val scope: CoroutineScope,
protected val kvCache: FullKVCache<Id, RegisteredObject> = FullKVCache(),
protected val recacheDelay: Long = 60.seconds.inWholeMilliseconds,
@ -25,9 +25,7 @@ open class AutoRecacheReadCRUDRepo<RegisteredObject, Id>(
) : ReadCRUDRepo<RegisteredObject, Id>, FallbackCacheRepo {
val autoUpdateJob = scope.launch {
while (isActive) {
runCatchingSafely {
kvCache.actualizeAll(originalRepo)
}
actualizeAll()
delay(recacheDelay)
}
@ -42,6 +40,12 @@ open class AutoRecacheReadCRUDRepo<RegisteredObject, Id>(
idGetter: (RegisteredObject) -> Id
) : 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 {
originalRepo.contains(id)
}.getOrElse {
@ -75,4 +79,8 @@ open class AutoRecacheReadCRUDRepo<RegisteredObject, Id>(
}.getOrNull() ?.also {
kvCache.set(idGetter(it), it)
} ?: kvCache.get(id)
override suspend fun invalidate() {
actualizeAll()
}
}

View File

@ -58,4 +58,8 @@ open class AutoRecacheWriteCRUDRepo<RegisteredObject, Id, InputObject>(
override suspend fun create(values: List<InputObject>): List<RegisteredObject> = originalRepo.create(values).onEach {
kvCache.set(idGetter(it), it)
}
override suspend fun invalidate() {
kvCache.clear()
}
}

View File

@ -25,9 +25,7 @@ open class AutoRecacheReadKeyValueRepo<Id, RegisteredObject>(
) : ReadKeyValueRepo<Id, RegisteredObject>, FallbackCacheRepo {
val autoUpdateJob = scope.launch {
while (isActive) {
runCatchingSafely {
kvCache.actualizeAll(originalRepo)
}
actualizeAll()
delay(recacheDelay)
}
@ -42,6 +40,12 @@ open class AutoRecacheReadKeyValueRepo<Id, RegisteredObject>(
idGetter: (RegisteredObject) -> Id
) : 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 {
originalRepo.contains(key)
}.getOrElse {
@ -85,4 +89,8 @@ open class AutoRecacheReadKeyValueRepo<Id, RegisteredObject>(
): PaginationResult<Id> = actionWrapper.wrap {
originalRepo.keys(v, pagination, reversed)
}.getOrElse { kvCache.keys(v, pagination, reversed) }
override suspend fun invalidate() {
actualizeAll()
}
}

View File

@ -47,4 +47,8 @@ open class AutoRecacheWriteKeyValueRepo<Id, RegisteredObject>(
).also {
kvCache.set(toSet)
}
override suspend fun invalidate() {
kvCache.clear()
}
}

View File

@ -32,9 +32,7 @@ open class AutoRecacheReadKeyValuesRepo<Id, RegisteredObject>(
) : ReadKeyValuesRepo<Id, RegisteredObject>, FallbackCacheRepo {
val autoUpdateJob = scope.launch {
while (isActive) {
runCatchingSafely {
kvCache.actualizeAll(originalRepo)
}
actualizeAll()
delay(recacheDelay)
}
@ -49,6 +47,12 @@ open class AutoRecacheReadKeyValuesRepo<Id, RegisteredObject>(
idGetter: (RegisteredObject) -> Id
) : 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 {
originalRepo.contains(k)
}.getOrElse {
@ -136,4 +140,8 @@ open class AutoRecacheReadKeyValuesRepo<Id, RegisteredObject>(
kvCache.set(k, ((kvCache.get(k) ?: return@also) + v).distinct())
}) ?: (kvCache.get(k) ?.contains(v) == true)
}
override suspend fun invalidate() {
actualizeAll()
}
}

View File

@ -79,4 +79,8 @@ open class AutoRecacheWriteKeyValuesRepo<Id, RegisteredObject>(
kvCache.set(k, (kvCache.get(k) ?: return@forEach) + v)
}
}
override suspend fun invalidate() {
kvCache.clear()
}
}