Skip to content

Commit

Permalink
4.0.0 (#6)
Browse files Browse the repository at this point in the history
* Generic response types
  • Loading branch information
aembke authored Sep 18, 2021
1 parent 49b814a commit 0e88b3f
Show file tree
Hide file tree
Showing 28 changed files with 1,895 additions and 1,021 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@

## 4.0.0

* Add generic response interface.
* Add tests

## 3.0.0

See below.

## 3.0.0-beta.4

* Add support for the `MONITOR` command.
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fred"
version = "3.0.0-beta.4"
version = "4.0.0"
authors = ["Alec Embke <[email protected]>"]
edition = "2018"
description = "An async Redis client for Rust built on Futures and Tokio."
Expand Down
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,18 @@ async fn main() -> Result<(), RedisError> {
let _ = client.connect(Some(policy));
// wait for the client to connect
let _ = client.wait_for_connect().await?;
let _ = client.flushall(false).await?;

let foo: Option<String> = client.get("foo").await?;
println!("Foo: {:?}", foo);
assert_eq!(foo, None);

println!("Foo: {:?}", client.get("foo").await?);
let _ = client.set("foo", "bar", None, None, false).await?;
println!("Foo: {:?}", client.get("foo".to_owned()).await?);
let _: () = client.set("foo", "bar", None, None, false).await?;
// or use turbofish to declare types
println!("Foo: {:?}", client.get<String, _>("foo").await?);
// or use a lower level interface for responses to defer parsing, etc
let foo: RedisValue = client.get("foo").await?;
assert_eq!(foo.as_str().unwrap(), "bar");

let _ = client.quit().await?;
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion bin/pipeline_test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ fn spawn_client_task(

tokio::spawn(async move {
while utils::incr_atomic(&counter) < argv.count {
let _ = client.incr(TEST_KEY).await?;
let _: () = client.incr(TEST_KEY).await?;
}

Ok(())
Expand Down
10 changes: 6 additions & 4 deletions examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,16 @@ async fn main() -> Result<(), RedisError> {
let _ = client.connect(Some(policy));
let _ = client.wait_for_connect().await?;

println!("Foo: {:?}", client.get("foo").await?);
// declare types on response values
let foo: Option<String> = client.get("foo").await?;
println!("Foo: {:?}", foo);

let _ = client
.set("foo", "bar", Some(Expiration::EX(1)), Some(SetOptions::NX), false)
.await?;
println!("Foo: {:?}", client.get("foo").await?);

sleep(Duration::from_millis(1000)).await;
println!("Foo: {:?}", client.get("foo").await?);
// or use turbofish. the first type is always the response type.
println!("Foo: {:?}", client.get::<String, _>("foo").await?);

let _ = client.quit().await?;
Ok(())
Expand Down
14 changes: 8 additions & 6 deletions examples/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ use fred::prelude::*;
use std::time::Duration;
use tokio::time::sleep;

static COUNT: usize = 50;
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());
Expand All @@ -16,15 +18,15 @@ async fn main() -> Result<(), RedisError> {
let _ = subscriber_client.wait_for_connect().await?;

let subscriber_jh = tokio::spawn(async move {
while let Ok(result) = subscriber_client.blpop("foo", 5.0).await {
println!("Blocking pop result: {:?}", result);
while let Ok((key, value)) = subscriber_client.blpop::<(String, i64), _>("foo", 5.0).await {
println!("Blocking pop result on {}: {}", key, value);
}

Ok::<_, RedisError>(())
Ok::<(), RedisError>(())
});

for _ in 0..COUNT {
let _ = publisher_client.rpush("foo", "bar").await?;
for idx in 0..COUNT {
let _ = publisher_client.rpush("foo", idx).await?;
sleep(Duration::from_millis(1000)).await;
}

Expand Down
2 changes: 1 addition & 1 deletion examples/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async fn main() -> Result<(), RedisError> {
let client = page.create_client();

for key in keys.into_iter() {
let value = client.get(&key).await?;
let value: RedisValue = client.get(&key).await?;
println!("Scanned {} -> {:?}", key.as_str_lossy(), value);
buffer.push((key, value));
}
Expand Down
10 changes: 1 addition & 9 deletions examples/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,9 @@ async fn main() -> Result<(), RedisError> {
}),
..RedisConfig::default()
};
let policy = ReconnectPolicy::new_exponential(0, 100, 30_000, 2);
let client = RedisClient::new(config);

tokio::spawn(client.on_error().for_each(|e| async move {
println!("Client received connection error: {:?}", e);
}));
tokio::spawn(client.on_reconnect().for_each(|client| async move {
println!("Client {} reconnected.", client.id());
}));

let jh = client.connect(Some(policy));
let jh = client.connect(None);
if let Err(error) = client.wait_for_connect().await {
println!("Client failed to connect with error: {:?}", error);
}
Expand Down
17 changes: 7 additions & 10 deletions examples/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,20 @@ async fn main() -> Result<(), RedisError> {
let config = RedisConfig::default();
let client = RedisClient::new(config);

let jh = client.connect(None);
let _ = client.connect(None);
let _ = client.wait_for_connect().await?;

// a special function to clear all data in a cluster
let _ = client.flushall_cluster().await?;
let _ = client.flushall(false).await?;

let trx = client.multi(true).await?;
let res1 = trx.get("foo").await?;
let res1: RedisValue = trx.get("foo").await?;
assert!(res1.is_queued());
let res2 = trx.set("foo", "bar", None, None, false).await?;
let res2: RedisValue = trx.set("foo", "bar", None, None, false).await?;
assert!(res2.is_queued());
let res3 = trx.get("foo").await?;
let res3: RedisValue = trx.get("foo").await?;
assert!(res3.is_queued());

if let RedisValue::Array(values) = trx.exec().await? {
println!("Transaction results: {:?}", values);
}
let values: (Option<String>, (), String) = trx.exec().await?;
println!("Transaction results: {:?}", values);

let _ = client.quit().await?;
Ok(())
Expand Down
Loading

0 comments on commit 0e88b3f

Please sign in to comment.