Extensibility
The number of commands which Redis supports is enormous. This client library provides code for the most common commands, but if you need to use commands for which no implementation is provided here, you can simply write it yourself (or send a pull request of course):
Define your command and response handling like this:
import com.dehnes.kotlinredis.StreamWithLength
import com.dehnes.kotlinredis.readString
import com.dehnes.kotlinredis.core.BulkStringStream
import com.dehnes.kotlinredis.core.RedisRequest
import com.dehnes.kotlinredis.core.SimpleString
import com.dehnes.kotlinredis.extentions.pipeline
import com.dehnes.kotlinredis.toStreamWithLength
fun pingCommand(
message: StreamWithLength?,
onResult: (r: StreamWithLength) -> Unit = {}
) = RedisRequest(
"PING", // command
listOfNotNull( // arguments
message
),
) { r ->
when (r) { // response handling
is BulkStringStream -> {
check(!r.isError)
onResult(r.toStreamWithLength())
}
is SimpleString -> {
if (r.isError) {
error("got error from Redis: ${r.value}")
}
onResult(r.value.toStreamWithLength())
}
else -> error("Unexpected response")
}
}
Then use your command, for example:
connPool.withConnection { conn ->
conn.pipeline {
add(pingCommand("ping-message".toStreamWithLength()))
}
}
If you want that .pipeline {} returns the results from your command, you can
use the resultCollector from the pipeline builder: