Basic Operations
Now that you have a working connection, let’s explore the fundamental operations you can perform with Valkey GLIDE.
Understanding Client Commands
Section titled “Understanding Client Commands”To communicate with Valkey, GLIDE clients provide methods for users to send commands to Valkey. Under the hood, all clients share a common same Rust core to communicate with Valkey. This makes their behavior consistent between the different languages while still supporting language specific features.
Updating Keys
Section titled “Updating Keys”To update keys in Valkey, clients can send SET and GET commands.
import asynciofrom glide import GlideClient, NodeAddress
async def string_operations(): client = GlideClient([NodeAddress("localhost", 6379)])
try: # Set a key-value pair await client.set("user:1000", "john_doe") print("✓ Key set successfully")
# Get the value value = await client.get("user:1000") print(f"✓ Retrieved value: {value}")
finally: await client.close()
if __name__ == "__main__": asyncio.run(string_operations())import glide.api.GlideClient;import glide.api.models.configuration.GlideClientConfiguration;import glide.api.models.configuration.NodeAddress;import glide.api.models.commands.SetOptions;
public class StringOperations { 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()) { // Set a key-value pair client.set("user:1000", "john_doe").get(); System.out.println("✓ Key set successfully");
// Get the value String value = client.get("user:1000").get(); System.out.println("✓ Retrieved value: " + value);
} catch (Exception e) { System.err.println("Error: " + e.getMessage()); } }}const { GlideClient } = require('@valkey/valkey-glide');
async function stringOperations() { const client = GlideClient.createClient({ addresses: [{ host: 'localhost', port: 6379 }] });
try { // Set a key-value pair await client.set('user:1000', 'john_doe'); console.log('✓ Key set successfully');
// Get the value const value = await client.get('user:1000'); console.log(`✓ Retrieved value: ${value}`);
} finally { client.close(); }}
stringOperations().catch(console.error);package main
import ( "fmt" "log" "time"
"github.com/valkey-io/valkey-glide/go/glide/api")
func main() { config := &api.GlideClientConfiguration{ Addresses: []api.NodeAddress{ {Host: "localhost", Port: 6379}, }, }
client := api.NewGlideClient(config) defer client.Close()
// Set a key-value pair err := client.Set("user:1000", "john_doe") if err != nil { log.Fatalf("Set failed: %v", err) } fmt.Println("✓ Key set successfully")
// Get the value value, err := client.Get("user:1000") if err != nil { log.Fatalf("Get failed: %v", err) } fmt.Printf("✓ Retrieved value: %s\n", value)
}Making Multiple Atomic Operations
Section titled “Making Multiple Atomic Operations”Clients can send MSET and MGET commands, which is used to set/get keys and values atomically.
# Set multiple keys at onceawait client.mset({"user:1001": "alice", "user:1002": "bob", "user:1003": "charlie"})print("✓ Multiple keys set")
# Get multiple keys at oncevalues = await client.mget(["user:1001", "user:1002", "user:1003"])print(f"✓ Retrieved values: {values}")// Set multiple keys at onceMap<String, String> keyValues = Map.of( "user:1001", "alice", "user:1002", "bob", "user:1003", "charlie");client.mset(keyValues).get();System.out.println("✓ Multiple keys set");
// Get multiple keys at onceString[] keys = {"user:1001", "user:1002", "user:1003"};String[] values = client.mget(keys).get();System.out.println("✓ Retrieved values: " + Arrays.toString(values));// Set multiple keys at onceawait client.mset({ 'user:1001': 'alice', 'user:1002': 'bob', 'user:1003': 'charlie'});console.log('✓ Multiple keys set');
// Get multiple keys at onceconst values = await client.mget(['user:1001', 'user:1002', 'user:1003']);console.log(`✓ Retrieved values: ${values}`);// Set multiple keys at oncekeyValues := map[string]string{ "user:1001": "alice", "user:1002": "bob", "user:1003": "charlie",}err = client.MSet(keyValues)if err != nil { log.Fatalf("MSet failed: %v", err)}fmt.Println("✓ Multiple keys set")
// Get multiple keys at oncekeys := []string{"user:1001", "user:1002", "user:1003"}values, err := client.MGet(keys)if err != nil { log.Fatalf("MGet failed: %v", err)}fmt.Printf("✓ Retrieved values: %v\n", values)Removing Keys
Section titled “Removing Keys”Clients can also send DEL commands to delete a list of keys.
deleted_count = await client.delete(["user:1001", "user:1002", "user:1003"])print(f"✓ Deleted {deleted_count} keys")String[] keys = {"user:1001", "user:1002", "user:1003"};Long deletedCount = client.del(keys).get();System.out.println("✓ Deleted " + deletedCount + " keys");const deletedCount = await client.del(['user:1001', 'user:1002', 'user:1003']);console.log(`✓ Deleted ${deletedCount} keys`);keys := []string{"user:1001", "user:1002", "user:1003"}
deletedCount, err := client.Del(keys)if err != nil { log.Fatalf("Del failed: %v", err)}
fmt.Printf("✓ Deleted %d keys\n", deletedCount)What’s Next
Section titled “What’s Next”Now that you’ve understand the basics, take a look at some other topics on Valkey GLIDE:
GLIDE's Rust Core Learn more about the underlying engine that connects GLIDE drivers.
Languages For language specific instructions, take a look at our supported languages.