mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2026-01-17 18:57:59 +00:00
Compare commits
4 Commits
0.11.9
...
540d5cce7c
| Author | SHA1 | Date | |
|---|---|---|---|
| 540d5cce7c | |||
| a548b00979 | |||
| 4b7ca6d565 | |||
| 0473fa238c |
@@ -1,5 +1,13 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## 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
|
## 0.11.9
|
||||||
|
|
||||||
* `Versions`
|
* `Versions`
|
||||||
|
|||||||
@@ -14,5 +14,5 @@ crypto_js_version=4.1.1
|
|||||||
# Project data
|
# Project data
|
||||||
|
|
||||||
group=dev.inmo
|
group=dev.inmo
|
||||||
version=0.11.9
|
version=0.11.10
|
||||||
android_code_version=133
|
android_code_version=134
|
||||||
|
|||||||
2
repos/cache/build.gradle
vendored
2
repos/cache/build.gradle
vendored
@@ -15,4 +15,4 @@ kotlin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package dev.inmo.micro_utils.repos.cache
|
package dev.inmo.micro_utils.repos.cache
|
||||||
|
|
||||||
import dev.inmo.micro_utils.repos.*
|
import dev.inmo.micro_utils.repos.*
|
||||||
|
import dev.inmo.micro_utils.repos.cache.cache.KVCache
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.flow.launchIn
|
import kotlinx.coroutines.flow.launchIn
|
||||||
|
|||||||
@@ -1,41 +1,6 @@
|
|||||||
package dev.inmo.micro_utils.repos.cache
|
package dev.inmo.micro_utils.repos.cache
|
||||||
|
|
||||||
import dev.inmo.micro_utils.repos.*
|
@Deprecated("Replaced", ReplaceWith("KVCache", "dev.inmo.micro_utils.repos.cache.cache.KVCache"))
|
||||||
import dev.inmo.micro_utils.pagination.utils.getAllWithNextPaging
|
typealias KVCache<K, V> = dev.inmo.micro_utils.repos.cache.cache.KVCache<K, V>
|
||||||
import kotlinx.coroutines.sync.Mutex
|
@Deprecated("Replaced", ReplaceWith("SimpleKVCache", "dev.inmo.micro_utils.repos.cache.cache.SimpleKVCache"))
|
||||||
import kotlinx.coroutines.sync.withLock
|
typealias SimpleKVCache<K, V> = dev.inmo.micro_utils.repos.cache.cache.SimpleKVCache<K, V>
|
||||||
|
|
||||||
interface KVCache<K, V> : KeyValueRepo<K, V>
|
|
||||||
|
|
||||||
open class SimpleKVCache<K, V>(
|
|
||||||
protected val cachedValuesCount: Int,
|
|
||||||
private val kvParent: KeyValueRepo<K, V> = MapKeyValueRepo<K, V>()
|
|
||||||
) : KVCache<K, V>, KeyValueRepo<K, V> by kvParent {
|
|
||||||
protected open val cacheStack = ArrayList<K>(cachedValuesCount)
|
|
||||||
protected val syncMutex = Mutex()
|
|
||||||
|
|
||||||
protected suspend fun makeUnset(toUnset: List<K>) {
|
|
||||||
cacheStack.removeAll(toUnset)
|
|
||||||
kvParent.unset(toUnset)
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun set(toSet: Map<K, V>) {
|
|
||||||
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<K>) {
|
|
||||||
syncMutex.withLock { makeUnset(toUnset) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package dev.inmo.micro_utils.repos.cache
|
package dev.inmo.micro_utils.repos.cache
|
||||||
|
|
||||||
import dev.inmo.micro_utils.repos.*
|
import dev.inmo.micro_utils.repos.*
|
||||||
|
import dev.inmo.micro_utils.repos.cache.cache.KVCache
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.flow.*
|
import kotlinx.coroutines.flow.*
|
||||||
|
|||||||
@@ -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.paginate
|
||||||
import dev.inmo.micro_utils.pagination.utils.reverse
|
import dev.inmo.micro_utils.pagination.utils.reverse
|
||||||
import dev.inmo.micro_utils.repos.*
|
import dev.inmo.micro_utils.repos.*
|
||||||
|
import dev.inmo.micro_utils.repos.cache.cache.KVCache
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.flow.*
|
import kotlinx.coroutines.flow.*
|
||||||
|
|||||||
6
repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/cache/KVCache.kt
vendored
Normal file
6
repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/cache/KVCache.kt
vendored
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package dev.inmo.micro_utils.repos.cache.cache
|
||||||
|
|
||||||
|
import dev.inmo.micro_utils.repos.*
|
||||||
|
|
||||||
|
interface KVCache<K, V> : KeyValueRepo<K, V>
|
||||||
|
|
||||||
37
repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/cache/SimpleKVCache.kt
vendored
Normal file
37
repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/cache/SimpleKVCache.kt
vendored
Normal file
@@ -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<K, V>(
|
||||||
|
protected val cachedValuesCount: Int,
|
||||||
|
private val kvParent: KeyValueRepo<K, V> = MapKeyValueRepo<K, V>()
|
||||||
|
) : KVCache<K, V>, KeyValueRepo<K, V> by kvParent {
|
||||||
|
protected open val cacheQueue = ArrayDeque<K>(cachedValuesCount)
|
||||||
|
protected val syncMutex = Mutex()
|
||||||
|
|
||||||
|
protected suspend fun makeUnset(toUnset: List<K>) {
|
||||||
|
cacheQueue.removeAll(toUnset)
|
||||||
|
kvParent.unset(toUnset)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun set(toSet: Map<K, V>) {
|
||||||
|
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<K>) {
|
||||||
|
syncMutex.withLock { makeUnset(toUnset) }
|
||||||
|
}
|
||||||
|
}
|
||||||
24
repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/cache/UnlimitedKVCache.kt
vendored
Normal file
24
repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/cache/UnlimitedKVCache.kt
vendored
Normal file
@@ -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<K, V>(
|
||||||
|
private val kvParent: KeyValueRepo<K, V> = MapKeyValueRepo<K, V>()
|
||||||
|
) : KVCache<K, V>, KeyValueRepo<K, V> by kvParent {
|
||||||
|
protected val syncMutex = Mutex()
|
||||||
|
|
||||||
|
override suspend fun set(toSet: Map<K, V>) {
|
||||||
|
syncMutex.withLock {
|
||||||
|
kvParent.set(toSet)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun unset(toUnset: List<K>) {
|
||||||
|
syncMutex.withLock {
|
||||||
|
kvParent.unset(toUnset)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
41
repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/full/FullCRUDCacheRepo.kt
vendored
Normal file
41
repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/full/FullCRUDCacheRepo.kt
vendored
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
package dev.inmo.micro_utils.repos.cache.full
|
||||||
|
|
||||||
|
import dev.inmo.micro_utils.pagination.Pagination
|
||||||
|
import dev.inmo.micro_utils.pagination.PaginationResult
|
||||||
|
import dev.inmo.micro_utils.repos.*
|
||||||
|
import dev.inmo.micro_utils.repos.cache.ReadCRUDCacheRepo
|
||||||
|
import dev.inmo.micro_utils.repos.cache.cache.KVCache
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.flow.launchIn
|
||||||
|
import kotlinx.coroutines.flow.onEach
|
||||||
|
|
||||||
|
|
||||||
|
open class FullReadCRUDCacheRepo<ObjectType, IdType>(
|
||||||
|
parentRepo: ReadCRUDRepo<ObjectType, IdType>,
|
||||||
|
kvCache: KVCache<IdType, ObjectType>,
|
||||||
|
idGetter: (ObjectType) -> IdType
|
||||||
|
) : ReadCRUDRepo<ObjectType, IdType>, ReadCRUDCacheRepo<ObjectType, IdType>(parentRepo, kvCache, idGetter) {
|
||||||
|
override suspend fun getByPagination(pagination: Pagination): PaginationResult<ObjectType> {
|
||||||
|
return kvCache.values(pagination)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun count(): Long = kvCache.count()
|
||||||
|
}
|
||||||
|
|
||||||
|
open class FullCRUDCacheRepo<ObjectType, IdType, InputValueType>(
|
||||||
|
parentRepo: CRUDRepo<ObjectType, IdType, InputValueType>,
|
||||||
|
kvCache: KVCache<IdType, ObjectType>,
|
||||||
|
scope: CoroutineScope = CoroutineScope(Dispatchers.Default),
|
||||||
|
idGetter: (ObjectType) -> IdType
|
||||||
|
) : FullReadCRUDCacheRepo<ObjectType, IdType>(
|
||||||
|
parentRepo,
|
||||||
|
kvCache,
|
||||||
|
idGetter
|
||||||
|
),
|
||||||
|
CRUDRepo<ObjectType, IdType, InputValueType>,
|
||||||
|
WriteCRUDRepo<ObjectType, IdType, InputValueType> by parentRepo {
|
||||||
|
protected val onNewJob = parentRepo.newObjectsFlow.onEach { kvCache.set(idGetter(it), it) }.launchIn(scope)
|
||||||
|
protected val onUpdatedJob = parentRepo.updatedObjectsFlow.onEach { kvCache.set(idGetter(it), it) }.launchIn(scope)
|
||||||
|
protected val onRemoveJob = parentRepo.deletedObjectsIdsFlow.onEach { kvCache.unset(it) }.launchIn(scope)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user