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 ValkeyRequests like this:

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

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

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


var value: String? = null

// low-level API execPipeline() 
connection.execPipeline(listOf(
    ValkeyRequest(
        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.