Execute Custom Commands
GLIDE’s custom_command method lets you execute any Valkey command directly, allowing you to execute custom commands, custom modules, and commands not yet supported by clients’ API.
Execute a Custom Command
Section titled “Execute a Custom Command”Pass the command name and arguments as an array of strings:
import asynciofrom glide import GlideClient, GlideClientConfiguration, NodeAddress
async def main(): config = GlideClientConfiguration( addresses=[NodeAddress("localhost", 6379)] ) client = await GlideClient.create(config)
# Execute a SET command await client.custom_command(["SET", "mykey", "hello"])
# Execute a GET command result = await client.custom_command(["GET", "mykey"]) print(result) # b'hello'
if __name__ == "__main__": asyncio.run(main())import glide.api.GlideClient;import glide.api.models.configuration.GlideClientConfiguration;import glide.api.models.configuration.NodeAddress;
public class CustomCommandExample { public static void main(String[] args) throws Exception { GlideClientConfiguration config = GlideClientConfiguration.builder() .address(NodeAddress.builder().host("localhost").port(6379).build()) .build(); GlideClient client = GlideClient.createClient(config).get();
// Execute a SET command client.customCommand(new String[]{"SET", "mykey", "hello"}).get();
// Execute a GET command Object result = client.customCommand(new String[]{"GET", "mykey"}).get(); System.out.println(result); // hello }}import { GlideClient } from "@valkey/valkey-glide";
async function main() { const client = await GlideClient.createClient({ addresses: [{ host: "localhost", port: 6379 }], });
// Execute a SET command await client.customCommand(["SET", "mykey", "hello"]);
// Execute a GET command const result = await client.customCommand(["GET", "mykey"]); console.log(result); // hello}
main();package main
import ( "context" "fmt" "github.com/valkey-io/valkey-glide/go/v2/glide")
func main() { ctx := context.Background() config := glide.NewGlideClientConfiguration(). WithAddress(&glide.NodeAddress{Host: "localhost", Port: 6379}) client, err := glide.NewGlideClient(config) if err != nil { panic(err) }
// Execute a SET command _, err = client.CustomCommand(ctx, []string{"SET", "mykey", "hello"}) if err != nil { panic(err) }
// Execute a GET command result, err := client.CustomCommand(ctx, []string{"GET", "mykey"}) if err != nil { panic(err) } fmt.Println(result) // hello}require 'vendor/autoload.php';
use Valkey\Glide\GlideClient;
$client = GlideClient::create("localhost:6379");
// Execute a SET command$client->rawCommand("SET", "mykey", "hello");
// Execute a GET command$result = $client->rawCommand("GET", "mykey");echo $result; // hellousing Valkey.Glide;using static Valkey.Glide.ConnectionConfiguration;
var config = new StandaloneClientConfigurationBuilder() .WithAddress("localhost", 6379) .Build();await using var client = await GlideClient.CreateClient(config);
// Execute a SET commandawait client.CustomCommand(["SET", "mykey", "hello"]);
// Execute a GET commandvar result = await client.CustomCommand(["GET", "mykey"]);Console.WriteLine(result); // helloLimitations
Section titled “Limitations”Keep these constraints in mind when using custom commands:
- Do not use for commands that alter connection state — e.g.,
SUBSCRIBEon RESP2. - Single-response commands only — Do not use for commands that return multiple responses (e.g.,
XREAD). - Key-based routing — In cluster mode, key-based commands are routed by the first key. Do not use multi-key commands that span different hash slots.
Related
Section titled “Related”- Custom Commands concept — design rationale and additional notes
- Execute Custom Scripts — for Lua scripting via the
ScriptAPI