Crate momento

Crate momento 

Source
Expand description

§Momento

Welcome to the Momento Rust SDK!

This crate contains a CacheClient for interacting with a serverless Momento Cache, and a TopicClient for interacting with serverless Momento Topics (pub/sub messaging). For more detailed information see the Momento documentation.

Construct instances of these clients using CacheClient::builder() and TopicClient::builder().

A few conventions you will find in the SDK that are worth knowing about:

§Asynchronous APIs

All APIs in the SDK are asynchronous and return Futures. This means that you will need to use an async runtime; we recommend tokio. Examples that include this dependency and illustrate the use of #[tokio::main] can be found in the example directory of the github repo.

§Configuration

Pre-built configurations are provided, with settings such as timeouts and keep-alives tuned to appropriate values for different environments. For example:

  • momento::cache::configurations::Laptop::latest() - suitable for a development environment with lenient timeouts
  • momento::cache::configurations::InRegion::latest() - suitable for a production configuration with more strict timeouts.

These configurations can be passed to the CacheClient and TopicClient builders. For advanced use cases you can build your own configurations rather than using the pre-builts.

§Credential Providers

The CredentialProvider struct is used to provide the API key for the Momento service. The two most common factory functions for creating a CredentialProvider are:

§Error Handling

Most APIs return a MomentoResult, which is just a type alias for Result<T, MomentoError>. You can use a match statement to handle the Result or use the ? operator to propagate errors.

§Enum Response Types, Type Coercion via into and try_into

Many APIs may have more than one type of response that they can return. For example, CacheClient::get may return a cache hit or a cache miss. These response are represented as enums, which you can interact with via a match statement, or you can use try_into to try to directly coerce the response into your desired type.

All Momento cache values are stored as vec<u8>, but if you are using UTF-8 strings, you can use try_into for these coercions as well.

Here are a few examples of how you can interact with a CacheClient::get response:

Using a match:

let item: String = match(cache_client.get(&cache_name, "key").await?) {
    GetResponse::Hit { value } => value.try_into()?,
    GetResponse::Miss => return Err(anyhow::Error::msg("cache miss"))
};

Or directly via try_into:

let item: String = cache_client.get(&cache_name, "key").await?.try_into()?;

If you are using Momento collection data types, such as lists and dictionaries, we support into for the main Rust types that you would expect to be able to use to represent these. For example, for Momento dictionaries:

let dictionary: HashMap<String, String> =
    cache_client.dictionary_fetch(&cache_name, "dictionary_key")
    .await?
    .try_into()?;

Re-exports§

pub use cache::CacheClient;
pub use cache::CacheClientBuilder;
pub use cache::ReadyToBuild;
pub use leaderboard::Leaderboard;
pub use leaderboard::LeaderboardClient;
pub use topics::TopicClient;
pub use auth::AuthClient;
pub use functions::FunctionClient;
pub use protosocket::cache::ProtosocketCacheClient;
pub use protosocket::cache::ProtosocketCacheClientBuilder;
pub use protosocket::cache::ReadyToBuild as ProtosocketClientReadyToBuild;
pub use errors::*;

Modules§

auth
Contains the AuthClient for calling Momento Auth APIs.
cache
Contains the CacheClient for interacting with Momento Cache.
config
Contains configuration settings for the Momento SDK that are shared between the Cache and Topics clients.
errors
Contains the MomentoError type for representing errors in the Momento SDK.
functions
Types for working with Momento Functions.
leaderboard
Containts the LeaderboardClient for interacting with Momento Leaderboards.
protosocket
Contains the ProtosocketCacheClient for interacting with Momento Cache using the Protosocket protocol.
topics
Contains the TopicClient for interacting with Momento Topics.

Structs§

CredentialProvider
Provides information that the client needs in order to establish a connection to and authenticate with the Momento service.

Traits§

IntoBytes
Convenience trait for converting strings into bytes and allowing methods to accept either string or byte values.
IntoBytesIterable
Convenience trait for converting a list of IntoBytes items into a list of byte values.

Type Aliases§

MomentoResult
Represents the result of a Momento operation.