From a548b009790c7cfb5eba3312a04d620a247234ed Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 29 Jun 2022 19:31:57 +0600 Subject: [PATCH] update repos cache --- CHANGELOG.md | 6 +++ .../micro_utils/repos/cache/CRUDCacheRepo.kt | 1 + .../inmo/micro_utils/repos/cache/KVCache.kt | 43 ++----------------- .../repos/cache/KeyValueCacheRepo.kt | 1 + .../repos/cache/KeyValuesCacheRepo.kt | 1 + .../micro_utils/repos/cache/cache/KVCache.kt | 6 +++ .../repos/cache/cache/SimpleKVCache.kt | 37 ++++++++++++++++ .../repos/cache/cache/UnlimitedKVCache.kt | 24 +++++++++++ 8 files changed, 80 insertions(+), 39 deletions(-) create mode 100644 repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/cache/KVCache.kt create mode 100644 repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/cache/SimpleKVCache.kt create mode 100644 repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/cache/UnlimitedKVCache.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index ace54eab52b..2846dd6e09f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ ## 0.11.10 +* `Repos`: + * `Cache`: + * `KVCache` has been replaced to the package `dev.inmo.micro_utils.repos.cache` + * `SimpleKVCache` has been replaced to the package `dev.inmo.micro_utils.repos.cache` + * New `KVCache` - `UnlimitedKVCache` + ## 0.11.9 * `Versions` diff --git a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/CRUDCacheRepo.kt b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/CRUDCacheRepo.kt index 13289e997e3..53812a575a7 100644 --- a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/CRUDCacheRepo.kt +++ b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/CRUDCacheRepo.kt @@ -1,6 +1,7 @@ package dev.inmo.micro_utils.repos.cache import dev.inmo.micro_utils.repos.* +import dev.inmo.micro_utils.repos.cache.cache.KVCache import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.launchIn diff --git a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/KVCache.kt b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/KVCache.kt index a7248352446..51f000ba299 100644 --- a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/KVCache.kt +++ b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/KVCache.kt @@ -1,41 +1,6 @@ package dev.inmo.micro_utils.repos.cache -import dev.inmo.micro_utils.repos.* -import dev.inmo.micro_utils.pagination.utils.getAllWithNextPaging -import kotlinx.coroutines.sync.Mutex -import kotlinx.coroutines.sync.withLock - -interface KVCache : KeyValueRepo - -open class SimpleKVCache( - protected val cachedValuesCount: Int, - private val kvParent: KeyValueRepo = MapKeyValueRepo() -) : KVCache, KeyValueRepo by kvParent { - protected open val cacheStack = ArrayList(cachedValuesCount) - protected val syncMutex = Mutex() - - protected suspend fun makeUnset(toUnset: List) { - cacheStack.removeAll(toUnset) - kvParent.unset(toUnset) - } - - override suspend fun set(toSet: Map) { - syncMutex.withLock { - if (toSet.size > cachedValuesCount) { - cacheStack.clear() - - kvParent.unset(getAllWithNextPaging { kvParent.keys(it) }) - val keysToInclude = toSet.keys.drop(toSet.size - cachedValuesCount) - - cacheStack.addAll(keysToInclude) - kvParent.set(keysToInclude.associateWith { toSet.getValue(it) }) - } else { - makeUnset(cacheStack.take(toSet.size)) - } - } - } - - override suspend fun unset(toUnset: List) { - syncMutex.withLock { makeUnset(toUnset) } - } -} +@Deprecated("Replaced", ReplaceWith("KVCache", "dev.inmo.micro_utils.repos.cache.cache.KVCache")) +typealias KVCache = dev.inmo.micro_utils.repos.cache.cache.KVCache +@Deprecated("Replaced", ReplaceWith("SimpleKVCache", "dev.inmo.micro_utils.repos.cache.cache.SimpleKVCache")) +typealias SimpleKVCache = dev.inmo.micro_utils.repos.cache.cache.SimpleKVCache diff --git a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/KeyValueCacheRepo.kt b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/KeyValueCacheRepo.kt index 79ae1392d1b..0f6569e83ef 100644 --- a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/KeyValueCacheRepo.kt +++ b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/KeyValueCacheRepo.kt @@ -1,6 +1,7 @@ package dev.inmo.micro_utils.repos.cache import dev.inmo.micro_utils.repos.* +import dev.inmo.micro_utils.repos.cache.cache.KVCache import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.* diff --git a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/KeyValuesCacheRepo.kt b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/KeyValuesCacheRepo.kt index 1948279461c..cb16722f6d4 100644 --- a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/KeyValuesCacheRepo.kt +++ b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/KeyValuesCacheRepo.kt @@ -5,6 +5,7 @@ import dev.inmo.micro_utils.pagination.PaginationResult import dev.inmo.micro_utils.pagination.utils.paginate import dev.inmo.micro_utils.pagination.utils.reverse import dev.inmo.micro_utils.repos.* +import dev.inmo.micro_utils.repos.cache.cache.KVCache import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.* diff --git a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/cache/KVCache.kt b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/cache/KVCache.kt new file mode 100644 index 00000000000..04a4f2320f7 --- /dev/null +++ b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/cache/KVCache.kt @@ -0,0 +1,6 @@ +package dev.inmo.micro_utils.repos.cache.cache + +import dev.inmo.micro_utils.repos.* + +interface KVCache : KeyValueRepo + diff --git a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/cache/SimpleKVCache.kt b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/cache/SimpleKVCache.kt new file mode 100644 index 00000000000..3469e4ccf5c --- /dev/null +++ b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/cache/SimpleKVCache.kt @@ -0,0 +1,37 @@ +package dev.inmo.micro_utils.repos.cache.cache + +import dev.inmo.micro_utils.pagination.utils.getAllWithNextPaging +import dev.inmo.micro_utils.repos.* +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.sync.withLock + +open class SimpleKVCache( + protected val cachedValuesCount: Int, + private val kvParent: KeyValueRepo = MapKeyValueRepo() +) : KVCache, KeyValueRepo by kvParent { + protected open val cacheQueue = ArrayDeque(cachedValuesCount) + protected val syncMutex = Mutex() + + protected suspend fun makeUnset(toUnset: List) { + cacheQueue.removeAll(toUnset) + kvParent.unset(toUnset) + } + + override suspend fun set(toSet: Map) { + syncMutex.withLock { + for ((k, v) in toSet) { + if (cacheQueue.size >= cachedValuesCount) { + cacheQueue.removeFirstOrNull() ?.let { + kvParent.unset(it) + } + } + cacheQueue.addLast(k) + kvParent.set(k, v) + } + } + } + + override suspend fun unset(toUnset: List) { + syncMutex.withLock { makeUnset(toUnset) } + } +} diff --git a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/cache/UnlimitedKVCache.kt b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/cache/UnlimitedKVCache.kt new file mode 100644 index 00000000000..61e80524151 --- /dev/null +++ b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/cache/UnlimitedKVCache.kt @@ -0,0 +1,24 @@ +package dev.inmo.micro_utils.repos.cache.cache + +import dev.inmo.micro_utils.repos.KeyValueRepo +import dev.inmo.micro_utils.repos.MapKeyValueRepo +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.sync.withLock + +open class UnlimitedKVCache( + private val kvParent: KeyValueRepo = MapKeyValueRepo() +) : KVCache, KeyValueRepo by kvParent { + protected val syncMutex = Mutex() + + override suspend fun set(toSet: Map) { + syncMutex.withLock { + kvParent.set(toSet) + } + } + + override suspend fun unset(toUnset: List) { + syncMutex.withLock { + kvParent.unset(toUnset) + } + } +}