Skip to content
This repository was archived by the owner on Jan 4, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
73 changes: 37 additions & 36 deletions common/src/main/kotlin/gg/grounds/discovery/ValkeyClient.kt
Original file line number Diff line number Diff line change
@@ -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<String, String> {
/* Redis SCAN logic as documented here:
* https://redis.io/docs/latest/commands/scan/
*/
return withJedis { jedis ->
val params = ScanParams().match(pattern)
val values = mutableMapOf<String, String>()

var cursor = ScanParams.SCAN_POINTER_START
do {
val page: ScanResult<String> = 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<String, String>()

values
}
var cursor = ScanParams.SCAN_POINTER_START
do {
val page: ScanResult<String> = 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 <T> withJedis(block: (Jedis) -> T): T = pool.resource.use(block)
}