-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusing-builder.rs
37 lines (31 loc) · 1.22 KB
/
using-builder.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use cuttlestore::{Cuttlestore, CuttlestoreBuilder, PutOptions};
use serde::{Deserialize, Serialize};
use std::time::Duration;
#[derive(Debug, Serialize, Deserialize)]
struct SelfDestructingMessage {
message: String,
}
#[tokio::main]
async fn main() {
let store: Cuttlestore<SelfDestructingMessage> =
CuttlestoreBuilder::new("filesystem://./example-store/using-builder")
// Every 3 seconds, the store will be swept for stale values.
.clean_every_secs(3)
.finish()
.await
.unwrap();
let value: Option<SelfDestructingMessage> = store.get("impossible").await.unwrap();
println!("At first: {value:?}");
let mission = SelfDestructingMessage {
message: "Your mission, should you choose to accept it, ...".to_string(),
};
store
.put_with("impossible", &mission, PutOptions::ttl_secs(1))
.await
.unwrap();
let value: Option<SelfDestructingMessage> = store.get("impossible").await.unwrap();
println!("After writing: {value:?}");
tokio::time::sleep(Duration::from_secs(4)).await;
let value: Option<SelfDestructingMessage> = store.get("impossible").await.unwrap();
println!("After waiting: {value:?}");
}