🍣

0003 redis-client

に公開

async version

use redis::AsyncCommands;
use anyhow::Result;
#[tokio::main]
async fn main() -> Result<()> {
    let client = redis::Client::open("redis://127.0.0.1/").unwrap();
    let mut connection = client.get_multiplexed_async_connection().await?;
    let _ : () = connection.set("my_key", "my_value").await?;
    let result:String = connection.get("my_key").await?;
    println!("{}", result);
    Ok(())
}

normalなのは use redis::Commands;してget_connectionする

[package]
name = "redis-client"
version = "0.1.0"
edition = "2024"

[dependencies]
redis = { version = "0.30.0", features = ["tokio-comp"] }
tokio = { version = "1", features = ["full"] }
anyhow = "1.0"

Discussion