|
| 1 | +use log::info; |
| 2 | +use momento::{CredentialProvider, MomentoError, ProtosocketCacheClient}; |
| 3 | +use std::process; |
| 4 | +use std::time::Duration; |
| 5 | +use tokio_rustls::rustls::crypto::aws_lc_rs::default_provider; |
| 6 | + |
| 7 | +#[tokio::main] |
| 8 | +async fn main() -> Result<(), MomentoError> { |
| 9 | + tokio_rustls::rustls::crypto::CryptoProvider::install_default(default_provider()) |
| 10 | + .expect("Error installing default crypto provider"); |
| 11 | + |
| 12 | + env_logger::init(); |
| 13 | + info!("Starting Momento ProtosocketCacheClient example"); |
| 14 | + |
| 15 | + let credential_provider = CredentialProvider::from_env_var("MOMENTO_API_KEY".to_string()) |
| 16 | + .expect("auth token should be valid"); |
| 17 | + // .with_private_endpoints(); // use this if you want to connect to private endpoints instead of public ones |
| 18 | + |
| 19 | + let config = momento::protosocket::cache::Configuration::builder() |
| 20 | + .timeout(Duration::from_secs(60)) |
| 21 | + .connection_count(1) |
| 22 | + .az_id(None) |
| 23 | + .build(); |
| 24 | + |
| 25 | + // Initializing Momento protosocket cache client |
| 26 | + let cache_client = match ProtosocketCacheClient::builder() |
| 27 | + .default_ttl(Duration::from_secs(60)) |
| 28 | + .configuration(config) |
| 29 | + .credential_provider(credential_provider) |
| 30 | + .runtime(tokio::runtime::Handle::current()) |
| 31 | + .build() |
| 32 | + .await |
| 33 | + { |
| 34 | + Ok(client) => client, |
| 35 | + Err(err) => { |
| 36 | + eprintln!("{err}"); |
| 37 | + process::exit(1); |
| 38 | + } |
| 39 | + }; |
| 40 | + |
| 41 | + // Assumes this cache exists already -- you can make one in the Momento Console |
| 42 | + let cache_name = "cache"; |
| 43 | + |
| 44 | + // First get should result in a miss |
| 45 | + match cache_client.get(cache_name, "key").await { |
| 46 | + Ok(resp) => { |
| 47 | + println!("Get response: {:?}", resp); |
| 48 | + } |
| 49 | + Err(err) => { |
| 50 | + eprintln!("{err}"); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + // Set the value |
| 55 | + match cache_client.set(cache_name, "key", "value").await { |
| 56 | + Ok(_) => { |
| 57 | + println!("Successfully stored item in cache"); |
| 58 | + } |
| 59 | + Err(err) => { |
| 60 | + eprintln!("{err}"); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + // Second get should result in a hit |
| 65 | + match cache_client.get(cache_name, "key").await { |
| 66 | + Ok(resp) => { |
| 67 | + println!("Get response: {:?}", resp); |
| 68 | + } |
| 69 | + Err(err) => { |
| 70 | + eprintln!("{err}"); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + Ok(()) |
| 75 | +} |
0 commit comments