43 releases (15 breaking)

new 0.22.1 Feb 3, 2023
0.21.9 Feb 1, 2023
0.8.2 Dec 21, 2022
0.7.8 Nov 21, 2022
0.7.2 Jul 20, 2022

#12 in Caching

Download history 166/week @ 2022-10-13 152/week @ 2022-10-20 85/week @ 2022-10-27 23/week @ 2022-11-03 75/week @ 2022-11-10 235/week @ 2022-11-17 17/week @ 2022-11-24 106/week @ 2022-12-01 410/week @ 2022-12-08 272/week @ 2022-12-15 192/week @ 2022-12-22 132/week @ 2022-12-29 403/week @ 2023-01-05 93/week @ 2023-01-12 339/week @ 2023-01-19 831/week @ 2023-01-26

1,693 downloads per month

Apache-2.0

3.5MB
2K SLoC

Momento client-sdk-rust

⚠️ Experimental SDK ⚠️

Rust SDK for Momento is experimental and under active development. There could be non-backward compatible changes or removal in the future. Please be aware that you may need to update your source code with the current version of the SDK when its version gets upgraded.



Rust SDK for Momento, a serverless cache that automatically scales without any of the operational overhead required by traditional caching solutions.


Preview Features

This SDK contains APIs for interacting with collection data structures: Lists, Sets, and Dictionaries. These APIs are currently in preview. If you would like to request early access to the data structure APIs, please contact us at support@momentohq.com.

Note that if you call the List, Set, or Dictionary APIs without first signing up for our early access preview, you the calls will result in an Unsupported operation error.

Getting Started 🏃

Requirements

  • A Momento Auth Token is required, you can generate one using the Momento CLI

Installing Momento and Running the Example

Check out example directory!


Using Momento

use momento::simple_cache_client::SimpleCacheClientBuilder;
use std::env;
use std::num::NonZeroU64;

async fn demo_cache_usage() {
    // Initialize Momento
    let auth_token = env::var("MOMENTO_AUTH_TOKEN")
        .expect("env var MOMENTO_AUTH_TOKEN must be set to your auth token");
    let item_default_ttl_seconds = 60;
    let mut cache_client = SimpleCacheClientBuilder::new(
        auth_token,
        NonZeroU64::new(item_default_ttl_seconds).unwrap(),
    )
    .unwrap()
    .build();

    // Create a cache named "cache"
    let cache_name = String::from("cache");
    cache_client.create_cache(&cache_name).await.unwrap();

    // Set key with default TTL and get value with that key
    let key = String::from("my_key");
    let value = String::from("my_value");
    cache_client
        .set(&cache_name, key.clone(), value.clone(), None)
        .await
        .unwrap();
    let result = cache_client.get(&cache_name, key.clone()).await.unwrap();
    println!("Looked up value: {:?}", result.as_string());

    // Set key with TTL of 5 seconds
    cache_client
        .set(&cache_name, key.clone(), value.clone(), NonZeroU64::new(5))
        .await
        .unwrap();

    // Permanently delete cache
    cache_client.delete_cache(&cache_name).await.unwrap();
}

Running Tests ⚡

Doc and integration tests require an auth token for testing. Set the env var TEST_AUTH_TOKEN to provide it.

Running unit tests:

cargo test --lib

Running doc tests:

TEST_AUTH_TOKEN=<auth token> cargo test --doc

Running integration tests:

TEST_AUTH_TOKEN=<auth token> cargo test --tests

Development 🔨

At Momento, we believe exceptions are bugs. In Rust, this means the unchecked use of .unwrap() calls inherently fills your code with bugs and makes debugging panic calls a lot more difficult.

We rigorously check for proper formatting and use clippy to check for good code practices as well as avoiding .unwrap() calls. Instead, try to use an alternative, including but not limited to:

  • .expect("descriptive error message")
  • .unwrap_or_default("default string goes here")
  • Use ? to have the caller handle the error

Building

Run this command to verify everything passes so that you can save yourself some time when our GitHub actions are ran against the commit:

cargo build && cargo clippy --all-targets --all-features -- -D warnings -W clippy::unwrap_used

Dependencies

~14–22MB
~448K SLoC