diff --git a/common/build.gradle.kts b/common/build.gradle.kts index a4a489d..955dfeb 100644 --- a/common/build.gradle.kts +++ b/common/build.gradle.kts @@ -1,7 +1,7 @@ plugins { `java-library` } dependencies { - api("redis.clients:jedis:3.9.0") + api("redis.clients:jedis:7.2.0") testImplementation("org.junit.jupiter:junit-jupiter:6.0.1") testRuntimeOnly("org.junit.platform:junit-platform-launcher") diff --git a/common/src/main/kotlin/gg/grounds/discovery/ValkeyClient.kt b/common/src/main/kotlin/gg/grounds/discovery/ValkeyClient.kt index 4215c13..c2c606f 100644 --- a/common/src/main/kotlin/gg/grounds/discovery/ValkeyClient.kt +++ b/common/src/main/kotlin/gg/grounds/discovery/ValkeyClient.kt @@ -1,54 +1,55 @@ package gg.grounds.discovery -import redis.clients.jedis.Jedis -import redis.clients.jedis.JedisPool -import redis.clients.jedis.ScanParams -import redis.clients.jedis.ScanResult +import redis.clients.jedis.ConnectionPoolConfig +import redis.clients.jedis.RedisClient +import redis.clients.jedis.params.ScanParams +import redis.clients.jedis.resps.ScanResult class ValkeyClient(config: ValkeyConfig) : AutoCloseable { - private val pool = JedisPool(config.host, config.port) + + private val client: RedisClient = + RedisClient.builder() + .hostAndPort(config.host, config.port) + .poolConfig( + ConnectionPoolConfig().apply { + maxTotal = 8 + maxIdle = 8 + minIdle = 1 + } + ) + .build() fun setWithTtl(key: String, value: String, ttlSeconds: Long) { - withJedis { jedis -> jedis.setex(key, ttlSeconds, value) } + client.setex(key, ttlSeconds, value) } fun delete(key: String) { - withJedis { jedis -> jedis.del(key) } + client.del(key) } fun scanValues(pattern: String): Map { - /* Redis SCAN logic as documented here: - * https://redis.io/docs/latest/commands/scan/ - */ - return withJedis { jedis -> - val params = ScanParams().match(pattern) - val values = mutableMapOf() - - var cursor = ScanParams.SCAN_POINTER_START - do { - val page: ScanResult = jedis.scan(cursor, params) - cursor = page.cursor - - val keys = page.result - if (keys.isEmpty()) { - continue - } - val fetchedValues = jedis.mget(*keys.toTypedArray()) - for (i in keys.indices) { - val value = fetchedValues[i] - if (value != null) { - values[keys[i]] = value - } - } - } while (cursor != ScanParams.SCAN_POINTER_START) + val params = ScanParams().match(pattern) + val values = mutableMapOf() - values - } + var cursor = ScanParams.SCAN_POINTER_START + do { + val page: ScanResult = client.scan(cursor, params) + cursor = page.cursor + + val keys = page.result + if (keys.isEmpty()) continue + + val fetchedValues = client.mget(*keys.toTypedArray()) + for (i in keys.indices) { + val value = fetchedValues[i] + if (value != null) values[keys[i]] = value + } + } while (cursor != ScanParams.SCAN_POINTER_START) + + return values } override fun close() { - pool.close() + client.close() } - - private inline fun withJedis(block: (Jedis) -> T): T = pool.resource.use(block) }