add cacheable settings

This commit is contained in:
InsanusMokrassar 2022-08-21 18:25:51 +06:00
parent 463a590861
commit f943daf1ab
3 changed files with 41 additions and 0 deletions

View File

@ -34,6 +34,7 @@ plagubot-plugin = { module = "dev.inmo:plagubot.plugin", version.ref = "plagubot
plagubot-bot = { module = "dev.inmo:plagubot.bot", 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-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-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" } kslog = { module = "dev.inmo:kslog", version.ref = "kslog" }
# buildscript classpaths # buildscript classpaths

View File

@ -15,6 +15,7 @@ kotlin {
} }
jvmMain { jvmMain {
dependencies { dependencies {
api libs.microutils.repos.cache
} }
} }
} }

View File

@ -1,10 +1,15 @@
package dev.inmo.plaguposter.settings.exposed package dev.inmo.plaguposter.settings.exposed
import dev.inmo.micro_utils.repos.KeyValueRepo 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.exposed.keyvalue.ExposedKeyValueRepo
import dev.inmo.micro_utils.repos.mappers.withMapper import dev.inmo.micro_utils.repos.mappers.withMapper
import dev.inmo.plaguposter.settings.repo.SettingsRepo import dev.inmo.plaguposter.settings.repo.SettingsRepo
import dev.inmo.tgbotapi.types.ChatId import dev.inmo.tgbotapi.types.ChatId
import kotlinx.coroutines.CoroutineScope
import kotlinx.serialization.* import kotlinx.serialization.*
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import org.jetbrains.exposed.sql.Database import org.jetbrains.exposed.sql.Database
@ -44,6 +49,27 @@ inline fun <reified T : Any> ExposedSettingsRepo(
serializer serializer
) )
@OptIn(InternalSerializationApi::class)
inline fun <reified T : Any> CachedExposedSettingsRepo(
database: Database,
json: Json,
scope: CoroutineScope,
serializer: KSerializer<T> = T::class.serializer(),
tableName: String = "settings_${T::class.simpleName!!}"
) = ExposedSettingsRepo<T>(
ExposedKeyValueRepo(
database,
{ long("chat_id") },
{ text("settings") },
tableName
).cached(
FullKVCache(MapKeyValueRepo()),
scope
),
json,
serializer
)
@OptIn(InternalSerializationApi::class) @OptIn(InternalSerializationApi::class)
inline fun <reified T : Any> SettingsRepo( inline fun <reified T : Any> SettingsRepo(
parent: KeyValueRepo<Long, String>, parent: KeyValueRepo<Long, String>,
@ -58,3 +84,16 @@ inline fun <reified T : Any> SettingsRepo(
serializer: KSerializer<T> = T::class.serializer(), serializer: KSerializer<T> = T::class.serializer(),
tableName: String = "settings_${T::class.simpleName!!}" tableName: String = "settings_${T::class.simpleName!!}"
) = ExposedSettingsRepo(database, json, serializer, tableName) ) = ExposedSettingsRepo(database, json, serializer, tableName)
@OptIn(InternalSerializationApi::class)
inline fun <reified T : Any> CachedSettingsRepo(
database: Database,
json: Json,
scope: CoroutineScope,
serializer: KSerializer<T> = T::class.serializer(),
tableName: String = "settings_${T::class.simpleName!!}"
) = CachedExposedSettingsRepo(database, json, scope, serializer, tableName)