74 releases (36 breaking)

new 0.44.0 Nov 21, 2024
0.43.0 Oct 9, 2024
0.42.0 Jul 12, 2024
0.34.0 Mar 27, 2024
0.7.2 Jul 20, 2022

#48 in Caching

Download history 135/week @ 2024-08-04 89/week @ 2024-08-11 40/week @ 2024-08-18 19/week @ 2024-08-25 21/week @ 2024-09-01 31/week @ 2024-09-08 195/week @ 2024-09-15 427/week @ 2024-09-22 218/week @ 2024-09-29 372/week @ 2024-10-06 101/week @ 2024-10-13 18/week @ 2024-10-20 15/week @ 2024-10-27 231/week @ 2024-11-03 108/week @ 2024-11-10 134/week @ 2024-11-17

488 downloads per month

Apache-2.0

550KB
7.5K SLoC

logo

project status project stability

Momento Client Library for Rust

Momento Cache is a fast, simple, pay-as-you-go caching solution without any of the operational overhead required by traditional caching solutions. This repo contains the source code for the Momento client library for Rust.

To get started with Momento you will need a Momento Auth Token. You can get one from the Momento Console.

Packages

The Momento Rust SDK package is available on crates.io: momento.

You will need to install additional dependencies to make full use of our SDK:

cargo add momento
cargo add tokio --features full
cargo add futures

Note: you will only need to install futures if you use Momento Topics.

Usage

Here is a quickstart you can use in your own project:

use momento::cache::{configurations::Laptop, GetResponse};
use momento::{CacheClient, CredentialProvider, MomentoError};
use std::time::Duration;

const CACHE_NAME: &str = "cache";

#[tokio::main]
pub async fn main() -> Result<(), MomentoError> {
    let cache_client = CacheClient::builder()
        .default_ttl(Duration::from_secs(60))
        .configuration(Laptop::latest())
        .credential_provider(CredentialProvider::from_env_var(
            "MOMENTO_API_KEY".to_string(),
        )?)
        .build()?;

    cache_client.create_cache(CACHE_NAME.to_string()).await?;

    match cache_client.set(CACHE_NAME, "mykey", "myvalue").await {
        Ok(_) => println!("Successfully stored key 'mykey' with value 'myvalue'"),
        Err(e) => println!("Error: {}", e),
    }

    let value: String = match cache_client.get(CACHE_NAME, "mykey").await? {
        GetResponse::Hit { value } => value.try_into().expect("I stored a string!"),
        GetResponse::Miss => {
            println!("Cache miss!");
            return Ok(()); // probably you'll do something else here
        }
    };
    println!("Successfully retrieved value: {}", value);

    Ok(())
}

Note that the above code requires an environment variable named MOMENTO_API_KEY which must be set to a valid Momento authentication token.

Getting Started and Documentation

Documentation is available on the Momento Docs website.

Examples

Ready to dive right in? Just check out the example directory for complete, working examples of how to use the SDK.

Developing

If you are interested in contributing to the SDK, please see the CONTRIBUTING docs.


For more info, visit our website at https://gomomento.com!

Dependencies

~19–29MB
~521K SLoC