Connecting to Redis
While you could use a standalone connection, it is recommended to use
the provided connection pool RedisConnectionPool.
import com.dehnes.kotlinredis.pool.RedisConnectionPool
import java.net.InetSocketAddress
// setup a connection pool
val connPool = RedisConnectionPool(
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 Redis.
Using a standalone connection
You can also connect to Redis using a standalone connection as follows, but be aware that it is not safe to share those connections between multiple threads.