Skip to content

Using the low level API

You can use the low-level API directly. This involves using InputStream because the low-level API is based on streaming. This enables sending/receiving large values efficiently without holding the data in memory.

Running commands using the low-level API consists of sending a list of RedisRequests like this:

connection.execPipeline(listOf(
    // your Redis command(s) - see https://redis.io/docs/latest/commands/
))
Since the low-level API is not typed, it is not restricted to the built-in Redis commands. You can use the low-level API to send any command which Redis supports.

For example to GET the string value stored for key foo using the low-level API:

import com.dehnes.kotlinredis.core.*
import com.dehnes.kotlinredis.readString
import com.dehnes.kotlinredis.toStreamWithLength


var value: String? = null

// low-level API execPipeline() 
connection.execPipeline(listOf(
    RedisRequest(
        command = "GET",
        args = listOf("foo".toStreamWithLength())
    ) {
        // response handling - code that consumes/converts
        // the response-stream into the variable 'value'
        check(it is BulkStringStream)
        value = it.toStreamWithLength().readString()
    }
))

println(value)

The low-level API uses pipelining and streaming. All other APIs are built on top of this.