3 unstable releases

0.2.1 Mar 15, 2023
0.2.0 Mar 11, 2023
0.1.0 Mar 11, 2023

#1914 in Database interfaces

Download history 13/week @ 2024-02-22 8/week @ 2024-02-29 6/week @ 2024-03-07 3/week @ 2024-03-14 34/week @ 2024-03-28 22/week @ 2024-04-04

56 downloads per month

MIT license

37KB
567 lines

Async memcached client for Rust.

Software License Crates.io

Contents

Usage

Set

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(())
}

Get

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(())
}

Gets

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(())
}

Increment

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(())
}

Decrement

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(())
}

Delete

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(())
}

Add

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(())
}

Append

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(())
}

Prepend

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(())
}

Replace

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(())
}

Version

#[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(())
}

Quit

#[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(())
}

Stats

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(())
}

Dependencies

~3–13MB
~114K SLoC