Rust Rocket 之Redis

作者: adm 分类: Rust 发布时间: 2024-07-11

cargo.toml

[dependencies]
rocket = { version = "0.5.1", features = ["json"] }
serde = { version = "1.0.204", features = ["derive"] }
deadpool-redis = { version = "0.9", features = ["serde"] }
redis = { version = "0.21", default-features = false, features = ["tls"] }

main.rs

#[macro_use] extern crate rocket;

use deadpool_redis::{redis::{cmd, FromRedisValue}, Config, Runtime};





#[tokio::main]
async fn main() {
    //redis://[:password]@host:port/?username=your_username
    let mut cfg = Config::from_url("redis://@127.0.0.1:6379/?username=your_username");
    let pool = cfg.create_pool(Some(Runtime::Tokio1)).unwrap();

        let mut conn = pool.get().await.unwrap();
        cmd("SET")
            .arg(&["deadpool/test_key", "42"])
            .query_async::<_, ()>(&mut conn)
            .await.unwrap();


    let mut conn = pool.get().await.unwrap();
    let ss: String = cmd("GET")
        .arg(&["deadpool/test_key"])
        .query_async(&mut conn)
        .await.unwrap();
    println!("wwww{}",ss)
}

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!