forked from aembke/fred.rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blocking.rs
35 lines (27 loc) · 963 Bytes
/
blocking.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
use fred::prelude::*;
use std::time::Duration;
use tokio::time::sleep;
static COUNT: i64 = 50;
#[tokio::main]
async fn main() -> Result<(), RedisError> {
pretty_env_logger::init();
let config = RedisConfig::default();
let publisher_client = RedisClient::new(config.clone());
let subscriber_client = RedisClient::new(config.clone());
let _ = publisher_client.connect(None);
let _ = subscriber_client.connect(None);
let _ = publisher_client.wait_for_connect().await?;
let _ = subscriber_client.wait_for_connect().await?;
let subscriber_jh = tokio::spawn(async move {
while let Ok((key, value)) = subscriber_client.blpop::<(String, i64), _>("foo", 5.0).await {
println!("Blocking pop result on {}: {}", key, value);
}
Ok::<(), RedisError>(())
});
for idx in 0..COUNT {
let _ = publisher_client.rpush("foo", idx).await?;
sleep(Duration::from_millis(1000)).await;
}
let _ = subscriber_jh.abort();
Ok(())
}