From f943daf1ab4d677598d5a2d5de3ea7ff6c73b161 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 21 Aug 2022 18:25:51 +0600 Subject: [PATCH] add cacheable settings --- gradle/libs.versions.toml | 1 + settings/build.gradle | 1 + .../kotlin/exposed/ExposedSettingsRepo.kt | 39 +++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 122871e..dda0e83 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -34,6 +34,7 @@ plagubot-plugin = { module = "dev.inmo:plagubot.plugin", version.ref = "plagubot plagubot-bot = { module = "dev.inmo:plagubot.bot", version.ref = "plagubot" } microutils-repos-common = { module = "dev.inmo:micro_utils.repos.common", version.ref = "microutils" } microutils-repos-exposed = { module = "dev.inmo:micro_utils.repos.exposed", version.ref = "microutils" } +microutils-repos-cache = { module = "dev.inmo:micro_utils.repos.cache", version.ref = "microutils" } kslog = { module = "dev.inmo:kslog", version.ref = "kslog" } # buildscript classpaths diff --git a/settings/build.gradle b/settings/build.gradle index c3a8634..d101bd6 100644 --- a/settings/build.gradle +++ b/settings/build.gradle @@ -15,6 +15,7 @@ kotlin { } jvmMain { dependencies { + api libs.microutils.repos.cache } } } diff --git a/settings/src/jvmMain/kotlin/exposed/ExposedSettingsRepo.kt b/settings/src/jvmMain/kotlin/exposed/ExposedSettingsRepo.kt index 8502ac0..541f243 100644 --- a/settings/src/jvmMain/kotlin/exposed/ExposedSettingsRepo.kt +++ b/settings/src/jvmMain/kotlin/exposed/ExposedSettingsRepo.kt @@ -1,10 +1,15 @@ package dev.inmo.plaguposter.settings.exposed import dev.inmo.micro_utils.repos.KeyValueRepo +import dev.inmo.micro_utils.repos.MapKeyValueRepo +import dev.inmo.micro_utils.repos.cache.cache.FullKVCache +import dev.inmo.micro_utils.repos.cache.full.FullKeyValueCacheRepo +import dev.inmo.micro_utils.repos.cache.full.cached import dev.inmo.micro_utils.repos.exposed.keyvalue.ExposedKeyValueRepo import dev.inmo.micro_utils.repos.mappers.withMapper import dev.inmo.plaguposter.settings.repo.SettingsRepo import dev.inmo.tgbotapi.types.ChatId +import kotlinx.coroutines.CoroutineScope import kotlinx.serialization.* import kotlinx.serialization.json.Json import org.jetbrains.exposed.sql.Database @@ -44,6 +49,27 @@ inline fun ExposedSettingsRepo( serializer ) +@OptIn(InternalSerializationApi::class) +inline fun CachedExposedSettingsRepo( + database: Database, + json: Json, + scope: CoroutineScope, + serializer: KSerializer = T::class.serializer(), + tableName: String = "settings_${T::class.simpleName!!}" +) = ExposedSettingsRepo( + ExposedKeyValueRepo( + database, + { long("chat_id") }, + { text("settings") }, + tableName + ).cached( + FullKVCache(MapKeyValueRepo()), + scope + ), + json, + serializer +) + @OptIn(InternalSerializationApi::class) inline fun SettingsRepo( parent: KeyValueRepo, @@ -58,3 +84,16 @@ inline fun SettingsRepo( serializer: KSerializer = T::class.serializer(), tableName: String = "settings_${T::class.simpleName!!}" ) = ExposedSettingsRepo(database, json, serializer, tableName) + + + +@OptIn(InternalSerializationApi::class) +inline fun CachedSettingsRepo( + database: Database, + json: Json, + scope: CoroutineScope, + serializer: KSerializer = T::class.serializer(), + tableName: String = "settings_${T::class.simpleName!!}" +) = CachedExposedSettingsRepo(database, json, scope, serializer, tableName) + +