forked from aembke/fred.rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan.rs
58 lines (47 loc) · 1.77 KB
/
scan.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use fred::prelude::*;
use futures::stream::StreamExt;
static COUNT: u32 = 50;
async fn create_fake_data(client: &RedisClient) -> Result<(), RedisError> {
for idx in 0..COUNT {
let _ = client.set(format!("foo-{}", idx), idx, None, None, false).await?;
}
Ok(())
}
async fn delete_fake_data(client: &RedisClient) -> Result<(), RedisError> {
for idx in 0..COUNT {
let _ = client.del(format!("foo-{}", idx)).await?;
}
Ok(())
}
#[tokio::main]
async fn main() -> Result<(), RedisError> {
let config = RedisConfig::default();
let client = RedisClient::new(config);
let jh = client.connect(None);
let _ = client.wait_for_connect().await?;
let _ = delete_fake_data(&client).await?;
let _ = create_fake_data(&client).await?;
// build up a buffer of (key, value) pairs from pages with 10 keys per page
let mut buffer = Vec::with_capacity(COUNT as usize);
let mut scan_stream = client.scan("foo*", Some(10), None);
while let Some(Ok(mut page)) = scan_stream.next().await {
if let Some(keys) = page.take_results() {
// create a client from the scan result, reusing the existing connection(s)
let client = page.create_client();
for key in keys.into_iter() {
let value: RedisValue = client.get(&key).await?;
println!("Scanned {} -> {:?}", key.as_str_lossy(), value);
buffer.push((key, value));
}
}
// move on to the next page now that we're done reading the values
// or move this before we call `get` on each key to scan results in the background as fast as possible
let _ = page.next();
}
assert_eq!(buffer.len(), COUNT as usize);
let _ = delete_fake_data(&client).await?;
let _ = client.quit().await?;
// optionally wait for the task driving the connection to finish
let _ = jh.await;
Ok(())
}