Skip to content

kafkiansky/memento

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Memcached client on Rust.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages