Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 55 additions & 23 deletions src/content/docs/getting-started/basic-operations.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,17 @@ To update keys in Valkey, clients can send [SET](https://valkey.io/commands/set/
<TabItem label="Python">
Comment thread
Aryex marked this conversation as resolved.
Resolved
```python
import asyncio
from glide import GlideClient, NodeAddress
from glide import GlideClient, GlideClientConfiguration, NodeAddress

async def string_operations():
client = GlideClient([NodeAddress("localhost", 6379)])

config = GlideClientConfiguration(
addresses=[NodeAddress("localhost", 6379)]
)
client = None

try:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the try/catch block really needed for an example? Could we just have the happy path, so readers focus on the string operations? Do other Python examples also include try/catch every time we create/use/close a client?

client = await GlideClient.create(config)

# Set a key-value pair
await client.set("user:1000", "john_doe")
print("✓ Key set successfully")
Expand All @@ -45,8 +50,12 @@ To update keys in Valkey, clients can send [SET](https://valkey.io/commands/set/
value = await client.get("user:1000")
print(f"✓ Retrieved value: {value}")

except Exception as e:
print(f"Operation failed: {e}")

finally:
await client.close()
if client is not None:
await client.close()

if __name__ == "__main__":
asyncio.run(string_operations())
Expand Down Expand Up @@ -88,21 +97,26 @@ To update keys in Valkey, clients can send [SET](https://valkey.io/commands/set/
const { GlideClient } = require('@valkey/valkey-glide');

async function stringOperations() {
const client = GlideClient.createClient({
addresses: [{ host: 'localhost', port: 6379 }]
});

let client;

try {
client = await GlideClient.createClient({
addresses: [{ host: 'localhost', port: 6379 }]
});
// 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}`);


} catch (error) {
console.error(`Operation failed: ${error.message}`);
} finally {
client.close();
if (client) {
client.close();
}
}
}

Expand Down Expand Up @@ -135,14 +149,16 @@ To update keys in Valkey, clients can send [SET](https://valkey.io/commands/set/
// Set a key-value pair
_, err = client.Set(context.Background(), "user:1000", "john_doe")
if err != nil {
log.Fatalf("Set failed: %v", err)
log.Printf("Set failed: %v", err)
return
}
fmt.Println("✓ Key set successfully")

// Get the value
value, err := client.Get(context.Background(), "user:1000")
if err != nil {
log.Fatalf("Get failed: %v", err)
log.Printf("Get failed: %v", err)
return
}
fmt.Printf("✓ Retrieved value: %v\n", value)
}
Expand All @@ -153,17 +169,20 @@ To update keys in Valkey, clients can send [SET](https://valkey.io/commands/set/
```php
// Create ValkeyGlide client
$client = new ValkeyGlide();
$client->connect(addresses: [['host' => 'localhost', 'port' => 6379]]);

try {
$client->connect(addresses: [['host' => 'localhost', 'port' => 6379]]);

// Set a key-value pair
$client->set('user:1000', 'john_doe');
echo "✓ Key set successfully\n";

// Get the value
$value = $client->get('user:1000');
echo "✓ Retrieved value: {$value}\n";


} catch (ValkeyGlideException $e) {
echo "Operation failed: {$e->getMessage()}\n";
} finally {
$client->close();
}
Expand All @@ -176,39 +195,52 @@ To update keys in Valkey, clients can send [SET](https://valkey.io/commands/set/
using static Valkey.Glide.ConnectionConfiguration;

var config = new StandaloneClientConfigurationBuilder().Build();
await using var client = await GlideClient.CreateClient(config);
try
{
await using var client = await GlideClient.CreateClient(config);

// Set a key-value pair
await client.SetAsync("user:1000", "john_doe");
Console.WriteLine("✓ Key set successfully");
// Set a key-value pair
await client.SetAsync("user:1000", "john_doe");
Console.WriteLine("✓ Key set successfully");

// Get the value
ValkeyValue value = await client.GetAsync("user:1000");
Console.WriteLine($"✓ Retrieved value: {value}");
// Get the value
ValkeyValue value = await client.GetAsync("user:1000");
Console.WriteLine($"✓ Retrieved value: {value}");
}
catch (Exception e)
{
Console.WriteLine($"Operation failed: {e.Message}");
}
```
</TabItem>

<TabItem label="Ruby">
```ruby
require "valkey"

client = Valkey.new(host: "localhost", port: 6379)
client = nil

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed? Not sure about Ruby syntax, but could we just declare in begin?


begin
client = Valkey.new(host: "localhost", port: 6379)

# Set a key-value pair
client.set("user:1000", "john_doe")
puts "✓ Key set successfully"

# Get the value
value = client.get("user:1000")
puts "✓ Retrieved value: #{value}"
rescue StandardError => e
puts "Operation failed: #{e.message}"
Comment on lines +233 to +234

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to previous comment, is this necessary in a basic operations example? Or can we just assume the client gets created successfully?

ensure
client.close
client&.close
end
```
</TabItem>
</Tabs>

The remaining examples assume that `client` was created with the error handling and cleanup pattern shown above.

## Making Multiple Atomic Operations

Clients can send [MSET](https://valkey.io/commands/mset/) and [MGET](https://valkey.io/commands/mget/) commands, which are used to set/get keys and values atomically.
Expand Down
Loading
Loading