Skip to content

Execute Custom Scripts

Valkey scripting allows you to execute Lua code directly on the Valkey server. This guide will go over executing custom Lua scripts with GLIDE.

  • Running Valkey server (standalone or cluster)
  • GLIDE installed.

The following steps shows how to run a simple a custom Lua script using GLIDE.

  1. Define the lua script

    String lua = """
    server.call('SET', KEYS[1], ARGV[1])
    return KEYS[1] .. ': ' .. server.call('GET', KEYS[1])
    """;
  2. Create a Script object with the Lua code

    Script script = new Script(lua, false);
  3. Execute script with keys and arguments

    String[] keys = {"username"};
    String[] args = {"John Doe"};
    Object result = client.invokeScript(script,
    ScriptOptions.builder().key(keys[0]).arg(args[0]).build()).get();
    System.out.println(result); // username: John Doe
Full Example
import glide.api.GlideClient;
import glide.api.models.configuration.GlideClientConfiguration;
import glide.api.models.configuration.NodeAddress;
import glide.api.models.Script;
import glide.api.models.commands.ScriptOptions;
public class Example {
public static void main(String[] args) {
GlideClientConfiguration config = GlideClientConfiguration.builder()
.address(NodeAddress.builder()
.host("localhost")
.port(6379)
.build())
.build();
try (GlideClient client = GlideClient.createClient(config).get()) {
String lua = """
server.call('SET', KEYS[1], ARGV[1])
return KEYS[1] .. ': ' .. server.call('GET', KEYS[1])
""";
Script script = new Script(lua, false);
Object result = client.invokeScript(script,
ScriptOptions.builder()
.key("username")
.arg("John Doe")
.build()).get();
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
}

See our documentations for more on how GLIDE support scripting.