Skip to content

Commit f5b9fc8

Browse files
DOC-5760 added async content
1 parent fb7e306 commit f5b9fc8

File tree

4 files changed

+135
-19
lines changed

4 files changed

+135
-19
lines changed

content/develop/clients/hiredis/_index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ categories:
1212
description: Connect your C application to a Redis database.
1313
linkTitle: hiredis (C)
1414
title: hiredis guide (C)
15-
weight: 9
15+
weight: 10
1616
---
1717

1818
[`hiredis`](https://github.com/redis/hiredis) is the

content/develop/clients/rust/_index.md

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,50 @@ client library and is not developed or supported directly by Redis.
2626

2727
## Install
2828

29-
Add the `redis` crate as a dependency in your `Cargo.toml` file:
29+
To use the synchronous API, add the `redis` crate as a dependency in your
30+
`Cargo.toml` file:
3031

3132
```toml
3233
[dependencies]
3334
redis = "0.32.5"
3435
```
3536

37+
If you want to use the asynchronous API, you should also enable either
38+
[`tokio`](https://tokio.rs/) or [`smol`](https://crates.io/crates/smol)
39+
as your async platform:
40+
41+
```toml
42+
[dependencies]
43+
# if you use tokio
44+
tokio = { version = "1.32.0", features = ["full"] }
45+
redis = { version = "0.32.5", features = ["tokio-comp"] }
46+
47+
# if you use smol
48+
smol = "2.0.2"
49+
redis = { version = "0.32.5", features = ["smol-comp"] }
50+
```
51+
3652
## Connect
3753

38-
Start by importing the `Commands` trait from the `redis` crate:
54+
Start by importing the `Commands` or `AsyncCommands` trait from the `redis` crate:
3955

40-
{{< clients-example set="landing" step="import" lang_filter="Rust-Sync" >}}
56+
{{< clients-example set="landing" step="import" lang_filter="Rust-Sync,Rust-Async" >}}
4157
{{< /clients-example >}}
4258

4359
The following example shows the simplest way to connect to a Redis server:
4460

45-
{{< clients-example set="landing" step="connect" lang_filter="Rust-Sync" >}}
61+
{{< clients-example set="landing" step="connect" lang_filter="Rust-Sync,Rust-Async" >}}
4662
{{< /clients-example >}}
4763

4864
After connecting, you can test the connection by storing and retrieving
4965
a simple [string]({{< relref "/develop/data-types/strings" >}}):
5066

51-
{{< clients-example set="landing" step="set_get_string" lang_filter="Rust-Sync" >}}
67+
{{< clients-example set="landing" step="set_get_string" lang_filter="Rust-Sync,Rust-Async" >}}
5268
{{< /clients-example >}}
5369

5470
You can also easily store and retrieve a [hash]({{< relref "/develop/data-types/hashes" >}}):
5571

56-
{{< clients-example set="landing" step="set_get_hash" lang_filter="Rust-Sync" >}}
72+
{{< clients-example set="landing" step="set_get_hash" lang_filter="Rust-Sync,Rust-Async" >}}
5773
{{< /clients-example >}}
5874

5975
## More information
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// EXAMPLE: landing
2+
// STEP_START import
3+
use redis::AsyncCommands;
4+
// STEP_END
5+
6+
#[tokio::main]
7+
async fn main() {
8+
// STEP_START connect
9+
let mut r = match redis::Client::open("redis://127.0.0.1") {
10+
Ok(client) => {
11+
match client.get_multiplexed_async_connection().await {
12+
Ok(conn) => conn,
13+
Err(e) => {
14+
println!("Failed to connect to Redis: {e}");
15+
return;
16+
}
17+
}
18+
},
19+
Err(e) => {
20+
println!("Failed to create Redis client: {e}");
21+
return;
22+
}
23+
};
24+
// STEP_END
25+
26+
// STEP_START set_get_string
27+
if let Ok(res) = r.set("foo", "bar").await {
28+
let res: String = res;
29+
println!("{res}"); // >>> OK
30+
} else {
31+
println!("Error setting foo");
32+
}
33+
34+
match r.get("foo").await {
35+
Ok(res) => {
36+
let res: String = res;
37+
println!("{res}"); // >>> bar
38+
},
39+
Err(e) => {
40+
println!("Error getting foo: {e}");
41+
return;
42+
}
43+
};
44+
// STEP_END
45+
46+
// STEP_START set_get_hash
47+
let hash_fields = [
48+
("model", "Deimos"),
49+
("brand", "Ergonom"),
50+
("type", "Enduro bikes"),
51+
("price", "4972"),
52+
];
53+
54+
if let Ok(res) = r.hset_multiple("bike:1", &hash_fields).await {
55+
let res: String = res;
56+
println!("{res}"); // >>> OK
57+
} else {
58+
println!("Error setting bike:1");
59+
}
60+
61+
match r.hget("bike:1", "model").await {
62+
Ok(res) => {
63+
let res: String = res;
64+
println!("{res}"); // >>> Deimos
65+
},
66+
Err(e) => {
67+
println!("Error getting bike:1 model: {e}");
68+
return;
69+
}
70+
}
71+
72+
match r.hget("bike:1", "price").await {
73+
Ok(res) => {
74+
let res: String = res;
75+
println!("{res}"); // >>> 4972
76+
},
77+
Err(e) => {
78+
println!("Error getting bike:1 price: {e}");
79+
return;
80+
}
81+
}
82+
83+
match r.hgetall("bike:1").await {
84+
Ok(res) => {
85+
let res: Vec<(String, String)> = res;
86+
for (key, value) in res {
87+
println!("{key}: {value}");
88+
}
89+
// >>> model: Deimos
90+
// >>> brand: Ergonom
91+
// >>> type: Enduro bikes
92+
// >>> price: 4972
93+
},
94+
Err(e) => {
95+
println!("Error getting bike:1: {e}");
96+
return;
97+
}
98+
// STEP_END
99+
}
100+
}

local_examples/client-specific/rust/landing.rs renamed to local_examples/client-specific/rust-sync/landing.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ fn main() {
1010
match client.get_connection() {
1111
Ok(conn) => conn,
1212
Err(e) => {
13-
println!("Failed to connect to Redis: {}", e);
13+
println!("Failed to connect to Redis: {e}");
1414
return;
1515
}
1616
}
1717
},
1818
Err(e) => {
19-
println!("Failed to create Redis client: {}", e);
19+
println!("Failed to create Redis client: {e}");
2020
return;
2121
}
2222
};
@@ -25,18 +25,18 @@ fn main() {
2525
// STEP_START set_get_string
2626
if let Ok(res) = r.set("foo", "bar") {
2727
let res: String = res;
28-
println!("{}", res); // >>> OK
28+
println!("{res}"); // >>> OK
2929
} else {
3030
println!("Error setting foo");
3131
}
3232

3333
match r.get("foo") {
3434
Ok(res) => {
3535
let res: String = res;
36-
println!("{}", res); // >>> bar
36+
println!("{res}"); // >>> bar
3737
},
3838
Err(e) => {
39-
println!("Error getting foo: {}", e);
39+
println!("Error getting foo: {e}");
4040
return;
4141
}
4242
};
@@ -52,29 +52,29 @@ fn main() {
5252

5353
if let Ok(res) = r.hset_multiple("bike:1", &hash_fields) {
5454
let res: String = res;
55-
println!("{}", res); // >>> OK
55+
println!("{res}"); // >>> OK
5656
} else {
5757
println!("Error setting bike:1");
5858
}
5959

6060
match r.hget("bike:1", "model") {
6161
Ok(res) => {
6262
let res: String = res;
63-
println!("{}", res); // >>> Deimos
63+
println!("{res}"); // >>> Deimos
6464
},
6565
Err(e) => {
66-
println!("Error getting bike:1: {}", e);
66+
println!("Error getting bike:1 model: {e}");
6767
return;
6868
}
6969
}
7070

7171
match r.hget("bike:1", "price") {
7272
Ok(res) => {
7373
let res: String = res;
74-
println!("{}", res); // >>> 4972
74+
println!("{res}"); // >>> 4972
7575
},
7676
Err(e) => {
77-
println!("Error getting bike:1: {}", e);
77+
println!("Error getting bike:1 price: {e}");
7878
return;
7979
}
8080
}
@@ -83,15 +83,15 @@ fn main() {
8383
Ok(res) => {
8484
let res: Vec<(String, String)> = res;
8585
for (key, value) in res {
86-
println!("{}: {}", key, value);
86+
println!("{key}: {value}");
8787
}
8888
// >>> model: Deimos
8989
// >>> brand: Ergonom
9090
// >>> type: Enduro bikes
9191
// >>> price: 4972
9292
},
9393
Err(e) => {
94-
println!("Error getting bike:1: {}", e);
94+
println!("Error getting bike:1: {e}");
9595
return;
9696
}
9797
}

0 commit comments

Comments
 (0)