diff --git a/CHANGELOG.md b/CHANGELOG.md index 9120f045a2d..3c9c34c0ed3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,15 @@ # Changelog +## 0.5.7 + +* `Pagination` + * `Ktor` + * `Server` + * Fixes in extension `extractPagination` +* `Repos` + * `Cache` + * All standard cache repos have been separated to read and read/write repos + ## 0.5.6 * `Versions` diff --git a/gradle.properties b/gradle.properties index 5d03522015e..70bf502ed43 100644 --- a/gradle.properties +++ b/gradle.properties @@ -45,5 +45,5 @@ dokka_version=1.4.32 # Project data group=dev.inmo -version=0.5.6 -android_code_version=47 +version=0.5.7 +android_code_version=48 diff --git a/pagination/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/pagination/ServerPaginationHelpers.kt b/pagination/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/pagination/ServerPaginationHelpers.kt index 744cc4e39dc..7c68d913a5a 100644 --- a/pagination/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/pagination/ServerPaginationHelpers.kt +++ b/pagination/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/pagination/ServerPaginationHelpers.kt @@ -5,8 +5,8 @@ import io.ktor.http.Parameters val Parameters.extractPagination: Pagination get() = SimplePagination( - get("page") ?.toIntOrNull() ?: 0, - get("size") ?.toIntOrNull() ?: defaultPaginationPageSize + get(paginationPageKey) ?.toIntOrNull() ?: 0, + get(paginationSizeKey) ?.toIntOrNull() ?: defaultPaginationPageSize ) val ApplicationCall.extractPagination: Pagination 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 22ffbb489c9..13289e997e3 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 @@ -6,19 +6,29 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach -open class CRUDCacheRepo( - protected val parentRepo: CRUDRepo, +open class ReadCRUDCacheRepo( + protected val parentRepo: ReadCRUDRepo, protected val kvCache: KVCache, - scope: CoroutineScope = CoroutineScope(Dispatchers.Default), protected val idGetter: (ObjectType) -> IdType -) : CRUDRepo 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) - +) : ReadCRUDRepo by parentRepo { override suspend fun getById(id: IdType): ObjectType? = kvCache.get(id) ?: (parentRepo.getById(id) ?.also { kvCache.set(id, it) }) override suspend fun contains(id: IdType): Boolean = kvCache.contains(id) || parentRepo.contains(id) } + +open class CRUDCacheRepo( + parentRepo: CRUDRepo, + kvCache: KVCache, + scope: CoroutineScope = CoroutineScope(Dispatchers.Default), + idGetter: (ObjectType) -> IdType +) : ReadCRUDCacheRepo( + parentRepo, + kvCache, + idGetter +), CRUDRepo, WriteCRUDRepo 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) +} 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 86d865afe03..ad1e24ab696 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 @@ -7,14 +7,19 @@ import kotlinx.coroutines.flow.* import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock -open class KeyValueCacheRepo( - protected val parentRepo: KeyValueRepo, +open class ReadKeyValueCacheRepo( + protected val parentRepo: ReadKeyValueRepo, protected val kvCache: KVCache, - scope: CoroutineScope = CoroutineScope(Dispatchers.Default) -) : KeyValueRepo by parentRepo { - protected val onNewJob = parentRepo.onNewValue.onEach { kvCache.set(it.first, it.second) }.launchIn(scope) - protected val onRemoveJob = parentRepo.onValueRemoved.onEach { kvCache.unset(it) }.launchIn(scope) - +) : ReadKeyValueRepo by parentRepo { override suspend fun get(k: Key): Value? = kvCache.get(k) ?: parentRepo.get(k) ?.also { kvCache.set(k, it) } override suspend fun contains(key: Key): Boolean = kvCache.contains(key) || parentRepo.contains(key) } + +open class KeyValueCacheRepo( + parentRepo: KeyValueRepo, + kvCache: KVCache, + scope: CoroutineScope = CoroutineScope(Dispatchers.Default) +) : ReadKeyValueCacheRepo(parentRepo, kvCache), KeyValueRepo, WriteKeyValueRepo by parentRepo { + protected val onNewJob = parentRepo.onNewValue.onEach { kvCache.set(it.first, it.second) }.launchIn(scope) + protected val onRemoveJob = parentRepo.onValueRemoved.onEach { kvCache.unset(it) }.launchIn(scope) +} 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 26b12af4fd2..ff2b36fbf01 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 @@ -11,15 +11,10 @@ import kotlinx.coroutines.flow.* import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock -open class KeyValuesCacheRepo( - protected val parentRepo: KeyValuesRepo, - protected val kvCache: KVCache>, - scope: CoroutineScope = CoroutineScope(Dispatchers.Default) -) : KeyValuesRepo by parentRepo { - protected val onNewJob = parentRepo.onNewValue.onEach { kvCache.set(it.first, kvCache.get(it.first) ?.plus(it.second) ?: listOf(it.second)) }.launchIn(scope) - protected val onRemoveJob = parentRepo.onValueRemoved.onEach { kvCache.set(it.first, kvCache.get(it.first) ?.minus(it.second) ?: return@onEach) }.launchIn(scope) - protected val onDataClearedJob = parentRepo.onDataCleared.onEach { kvCache.unset(it) }.launchIn(scope) - +open class ReadKeyValuesCacheRepo( + protected val parentRepo: ReadKeyValuesRepo, + protected val kvCache: KVCache> +) : ReadKeyValuesRepo by parentRepo { override suspend fun get(k: Key, pagination: Pagination, reversed: Boolean): PaginationResult { return kvCache.get(k) ?.paginate( pagination.let { if (reversed) it.reverse(count(k)) else it } @@ -35,3 +30,13 @@ open class KeyValuesCacheRepo( override suspend fun contains(k: Key, v: Value): Boolean = kvCache.get(k) ?.contains(v) ?: parentRepo.contains(k, v) override suspend fun contains(k: Key): Boolean = kvCache.contains(k) || parentRepo.contains(k) } + +open class KeyValuesCacheRepo( + parentRepo: KeyValuesRepo, + kvCache: KVCache>, + scope: CoroutineScope = CoroutineScope(Dispatchers.Default) +) : ReadKeyValuesCacheRepo(parentRepo, kvCache), KeyValuesRepo, WriteKeyValuesRepo by parentRepo { + protected val onNewJob = parentRepo.onNewValue.onEach { kvCache.set(it.first, kvCache.get(it.first) ?.plus(it.second) ?: listOf(it.second)) }.launchIn(scope) + protected val onRemoveJob = parentRepo.onValueRemoved.onEach { kvCache.set(it.first, kvCache.get(it.first) ?.minus(it.second) ?: return@onEach) }.launchIn(scope) + protected val onDataClearedJob = parentRepo.onDataCleared.onEach { kvCache.unset(it) }.launchIn(scope) +}