From d1e0d2f6f9504c06d7846a7b1f5bca24d4655f1a Mon Sep 17 00:00:00 2001 From: Shiina Kin Date: Fri, 13 Sep 2024 13:43:40 +0800 Subject: [PATCH] feat: switch cache type based on config --- .../main/kotlin/io/sakurasou/plugins/Cache.kt | 20 ++++++++++--------- app/src/main/resources/application.yaml | 4 ++-- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/app/src/main/kotlin/io/sakurasou/plugins/Cache.kt b/app/src/main/kotlin/io/sakurasou/plugins/Cache.kt index 1fc4be7f..5adffb88 100644 --- a/app/src/main/kotlin/io/sakurasou/plugins/Cache.kt +++ b/app/src/main/kotlin/io/sakurasou/plugins/Cache.kt @@ -2,6 +2,7 @@ package io.sakurasou.plugins import com.ucasoft.ktor.simpleCache.SimpleCache import com.ucasoft.ktor.simpleMemoryCache.memoryCache +import com.ucasoft.ktor.simpleRedisCache.redisCache import io.ktor.server.application.* import kotlin.time.Duration.Companion.seconds @@ -11,15 +12,16 @@ import kotlin.time.Duration.Companion.seconds */ fun Application.configureCache(host: String, port: String) { install(SimpleCache) { - memoryCache { - invalidateAt = 10.seconds + if (host != "disabled") { + redisCache { + invalidateAt = 10.seconds + this.host = host + this.port = port.toInt() + } + } else { + memoryCache { + invalidateAt = 10.seconds + } } } - // install(SimpleCache) { - // redisCache { - // invalidateAt = 10.seconds - // host = "localhost" - // port = 6379 - // } - // } } \ No newline at end of file diff --git a/app/src/main/resources/application.yaml b/app/src/main/resources/application.yaml index 3f3b7c7b..9bd0b34f 100644 --- a/app/src/main/resources/application.yaml +++ b/app/src/main/resources/application.yaml @@ -10,8 +10,8 @@ ktor: password: $DATABASE_PASSWORD cache: redis: - host: $REDIS_HOST - port: $REDIS_PORT + host: "$REDIS_HOST:disable" + port: "$REDIS_PORT:6379" deployment: port: 8080 watch: