Skip to content

Commit 419df26

Browse files
authored
chore: add protosocket cache client example (#504)
* chore: add protosocket cache client example * remove unnecessary example override * small fixes
1 parent 3ed656e commit 419df26

File tree

3 files changed

+189
-5
lines changed

3 files changed

+189
-5
lines changed

example/rust/Cargo.lock

Lines changed: 111 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/rust/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@ momento = { version = "0.57.0" }
2121
tokio = { version = "1.37.0", features = ["full"] }
2222
uuid = { version = "1.8.0", features = ["v4"] }
2323
anyhow = "1"
24+
tokio-rustls = { version = "0.26" }
25+
log = "0.4.28"
26+
env_logger = "0.11"
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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

Comments
 (0)