Connecting to Valkey
While you could use a standalone connection, it is recommended to use
the provided connection pool ValkeyConnectionPool.
import com.dehnes.kotlinvalkey.pool.ValkeyConnectionPool
import java.net.InetSocketAddress
// setup a connection pool
val connPool = ValkeyConnectionPool(
addr = InetSocketAddress("localhost", 6379),
)
// use it
connPool.withConnection { conn ->
// your code using the connection goes here
}
The connection pool does not provide any way to get a connection directly, to ensure a connection is always returned.
Note that if you have other requirements which are not covered by the current connection pool implementation, you can implement your own connection pool as well, as all underlying methods are public.
Next, pick your API and send commands to Valkey.
Using a standalone connection
You can also connect to Valkey using a standalone connection as follows, but be aware that it is not safe to share those connections between multiple threads.