3个不稳定版本

0.2.1 2023年3月15日
0.2.0 2023年3月11日
0.1.0 2023年3月11日

#1989数据库接口

21 每月下载量

MIT 协议

37KB
567

Rust异步memcached客户端。

Software License Crates.io

内容

使用

设置

use memento::Item;

#[tokio::main]
async fn main() -> memento::Result<()> {
    let mut memento = memento::new("localhost:11211").await?;

    match memento.set("x".parse()?, Item::timeless("y")).await? {
        memento::CommandResp::Stored => println!("OK"),
        cmd => println!("{:#?}", cmd),
    }
    
    Ok(())
}

获取

use memento::Item;
    
#[tokio::main]
async fn main() -> memento::Result<()> {
    let mut memento = memento::new("localhost:11211").await?;

    match memento.get("x").await? {
        memento::CommandResp::Value { key, item } => {
            println!(
                "{key}: {value}",
                key = key.to_string(),
                value = item.to_string()
            )
        }
        cmd => println!("{:#?}", cmd),
    }
    
    Ok(())
}

获取多个

use memento::Item;

#[tokio::main]
async fn main() -> memento::Result<()> {
    let mut memento = memento::new("localhost:11211").await?;

    match memento.set("a".parse()?, Item::timeless("b")).await? {
        memento::CommandResp::Stored => println!("OK"),
        cmd => println!("{:#?}", cmd),
    }

    match memento.set("c".parse()?, Item::timeless("d")).await? {
        memento::CommandResp::Stored => println!("OK"),
        cmd => println!("{:#?}", cmd),
    }

    match memento.gets(vec!["a".parse()?, "c".parse()?]).await? {
        memento::CommandResp::Values(values) => {
            for (key, value) in values {
                println!(
                    "{key}: {value}",
                    key = key.to_string(),
                    value = value.to_string()
                )
            }
        }
        cmd => println!("{:#?}", cmd),
    }

    Ok(())
}

递增

use memento::Item;

#[tokio::main]
async fn main() -> memento::Result<()> {
    let mut memento = memento::new("localhost:11211").await?;

    match memento.set("counter".parse()?, Item::timeless(0)).await? {
        memento::CommandResp::Stored => println!("OK"),
        cmd => println!("{:#?}", cmd),
    }

    match memento.incr("counter".parse()?, 1).await? {
        memento::CommandResp::Counter(value) => {
            println!("counter: {value}")
        }
        cmd => println!("{:#?}", cmd),
    }
    
    Ok(())
}

递减

use memento::Item;
    
#[tokio::main]
async fn main() -> memento::Result<()> {
    let mut memento = memento::new("localhost:11211").await?;

    match memento.set("counter".parse()?, Item::timeless(0)).await? {
        memento::CommandResp::Stored => println!("OK"),
        cmd => println!("{:#?}", cmd),
    }

    match memento.decr("counter".parse()?, 1).await? {
        memento::CommandResp::Counter(value) => {
            println!("counter: {value}")
        }
        cmd => println!("{:#?}", cmd),
    }
    
    Ok(())
}

删除

use memento::Item;
    
#[tokio::main]
async fn main() -> memento::Result<()> {
    let mut memento = memento::new("localhost:11211").await?;

    match memento.set("counter".parse()?, Item::timeless(0)).await? {
        memento::CommandResp::Stored => println!("OK"),
        cmd => println!("{:#?}", cmd),
    }

    match memento.delete("counter".parse()?).await? {
        memento::CommandResp::Deleted => println!("deleted"),
        cmd => println!("{:#?}", cmd),
    }
    
    Ok(())
}

添加

use memento::Item;
    
#[tokio::main]
async fn main() -> memento::Result<()> {
    let mut memento = memento::new("localhost:11211").await?;

    match memento.set("counter".parse()?, Item::timeless(0)).await? {
        memento::CommandResp::Stored => println!("OK"),
        cmd => println!("{:#?}", cmd),
    }

    match memento.add("counter".parse()?, Item::timeless(10)).await? {
        memento::CommandResp::NotStored => println!("value exists"),
        cmd => println!("{:#?}", cmd),
    }

    match memento
        .add("another_counter".parse()?, Item::timeless(10))
        .await?
    {
        memento::CommandResp::Stored => println!("value stored"),
        cmd => println!("{:#?}", cmd),
    }
    
    Ok(())
}

追加

use memento::Item;

#[tokio::main]
async fn main() -> memento::Result<()> {
    let mut memento = memento::new("localhost:11211").await?;

    match memento
        .set("language".parse()?, Item::timeless("rust"))
        .await?
    {
        memento::CommandResp::Stored => println!("OK"),
        cmd => println!("{:#?}", cmd),
    }

    match memento
        .append("language".parse()?, Item::timeless(" c++"))
        .await?
    {
        memento::CommandResp::Stored => println!("OK"),
        cmd => println!("{:#?}", cmd),
    }

    match memento.get("language".parse()?).await? {
        memento::CommandResp::Value { key, item } => {
            println!(
                "{key}: {value}",
                key = key.to_string(),
                value = item.to_string()
            )
        }
        cmd => println!("{:#?}", cmd),
    }

    Ok(())
}

预追加

use memento::Item;

#[tokio::main]
async fn main() -> memento::Result<()> {
    let mut memento = memento::new("localhost:11211").await?;

    match memento
        .set("language".parse()?, Item::timeless("rust"))
        .await?
    {
        memento::CommandResp::Stored => println!("OK"),
        cmd => println!("{:#?}", cmd),
    }

    match memento
        .prepend("language".parse()?, Item::timeless("c++ "))
        .await?
    {
        memento::CommandResp::Stored => println!("OK"),
        cmd => println!("{:#?}", cmd),
    }

    match memento.get("language".parse()?).await? {
        memento::CommandResp::Value { key, item } => {
            println!(
                "{key}: {value}",
                key = key.to_string(),
                value = item.to_string()
            )
        }
        cmd => println!("{:#?}", cmd),
    }

    Ok(())
}

替换

use memento::Item;
use std::time::Duration;

#[tokio::main]
async fn main() -> memento::Result<()> {
    let mut memento = memento::new("localhost:11211").await?;

    match memento.set("a".parse()?, Item::timeless("b")).await? {
        memento::CommandResp::Stored => println!("OK"),
        cmd => println!("{:#?}", cmd),
    }

    match memento
        .replace("a".parse()?, Item::expires("d", Duration::from_secs(10)))
        .await?
    {
        memento::CommandResp::Stored => println!("Replaced"),
        cmd => println!("{:#?}", cmd),
    }

    match memento.get("a".parse()?).await? {
        memento::CommandResp::Value { key, item } => {
            println!(
                "{key}: {value}",
                key = key.to_string(),
                value = item.to_string()
            )
        }
        cmd => println!("{:#?}", cmd),
    }

    Ok(())
}

版本

#[tokio::main]
async fn main() -> memento::Result<()> {
    let mut memento = memento::new("localhost:11211").await?;

    match memento.version().await? {
        memento::CommandResp::Version(version) => println!("version {version}"),
        cmd => println!("{:#?}", cmd),
    }

    Ok(())
}

退出

#[tokio::main]
async fn main() -> memento::Result<()> {
    let mut memento = memento::new("localhost:11211").await?;

    match memento.quit().await? {
        memento::CommandResp::NoResponse => println!("connection closed"),
        cmd => println!("{:#?}", cmd),
    }

    Ok(())
}

状态

use memento::Stat;

#[tokio::main]
async fn main() -> memento::Result<()> {
    let mut memento = memento::new("localhost:11211").await?;

    match memento.stats().await? {
        memento::CommandResp::Stats(stats) => {
            for stat in stats {
                match stat {
                    Stat::Pid(pid) => println!("pid {pid}"),
                    Stat::CmdSet(sets) => println!("sets {sets}"),
                    Stat::CmdGet(gets) => println!("gets {gets}"),
                    Stat::Other { name, value } => {
                        println!("{name}: {value}")
                    }
                    _ => {}
                }
            }
        }
        _ => {}
    }

    Ok(())
}

依赖项

~3–14MB
~113K SLoC