Lua scripting
Lua scripts run atomically inside Valkey. A script can return any type, so the results are converted
into plain values (Long, String or ByteArray, List, Map, Boolean, Double or null).
The ...String variants return bulk strings as String, the ...Bytes variants as ByteArray.
val result = connPool.evalString(
"return server.call('INCRBY', KEYS[1], ARGV[1])",
keys = listOf("counter"),
args = listOf("5"),
) // 5L
Re-using scripts
ValkeyScript computes the SHA1 of the script locally. On a connection (or connection pool) it runs the script
via EVALSHA and loads it with SCRIPT LOAD only if Valkey does not know it yet:
val incrBy = ValkeyScript("return server.call('INCRBY', KEYS[1], ARGV[1])")
connPool.evalString(incrBy, listOf("counter"), listOf("5"))
Inside a pipeline {} or transactionPipeline {} use evalShaString(script.sha1, ...) after loading the
script with scriptLoad(...), as the result of a pipeline is only known after all commands were sent.
Errors
Errors raised by a script (or by a command called with server.call) are thrown as ValkeyErrorReplyException,
which holds the error reply in reply. The connection stays usable.
Read-only scripts
Pass readOnly = true to use EVAL_RO/EVALSHA_RO (Valkey 7+), which reject write commands and can run on replicas.