Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use owned variables in examples #49

Merged
merged 2 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions examples/async_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,55 @@ use stretto::AsyncCache;

#[tokio::main]
async fn main() {
let c: AsyncCache<&str, &str> = AsyncCache::new(12960, 1e6 as i64, tokio::spawn).unwrap();
// cache is intended to take ownership of key and value
let c: AsyncCache<String, String> = AsyncCache::new(12960, 1e6 as i64, tokio::spawn).unwrap();

// set a value with a cost of 1
c.insert("a", "a", 1).await;
c.insert("key1".to_string(), "value1".to_string(), 1).await;

// set a value with a cost of 1 and ttl
c.insert_with_ttl("b", "b", 1, Duration::from_secs(3)).await;
c.insert_with_ttl(
"key2".to_string(),
"value2".to_string(),
1,
Duration::from_secs(3),
)
.await;

// wait for value to pass through buffers
c.wait().await.unwrap();

// Create a search key
let key1 = "key1".to_string();
// when we get the value, we will get a ValueRef, which contains a RwLockReadGuard
// so when we finish use this value, we must release the ValueRef
let v = c.get(&"a").await.unwrap();
assert_eq!(v.value(), &"a");
let v = c.get(&key1).await.unwrap();
assert_eq!(v.value(), &"value1");
// release the value
v.release(); // or drop(v)

// lock will be auto released when out of scope
{
// when we get the value, we will get a ValueRef, which contains a RwLockWriteGuard
// so when we finish use this value, we must release the ValueRefMut
let mut v = c.get_mut(&"a").await.unwrap();
v.write("aa");
assert_eq!(v.value(), &"aa");
let mut v = c.get_mut(&key1).await.unwrap();
v.write("value2".to_string());
assert_eq!(v.value(), &"value2");
// release the value
}

// if you just want to do one operation
let v = c.get_mut(&"a").await.unwrap();
v.write_once("aaa");
let v = c.get_mut(&key1).await.unwrap();
v.write_once("value3".to_string());

let v = c.get(&"a").await.unwrap();
println!("{}", v);
assert_eq!(v.value(), &"aaa");
let v = c.get(&key1).await.unwrap();
assert_eq!(v.value(), &"value3");
v.release();

// clear the cache
c.clear().await.unwrap();
// wait all the operations are finished
c.wait().await.unwrap();

assert!(c.get(&"a").await.is_none());
assert!(c.get(&key1).await.is_none());
}
32 changes: 20 additions & 12 deletions examples/sync_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,51 @@ use std::time::Duration;
use stretto::Cache;

fn main() {
// cache is intended to take ownership of key and value
let c = Cache::new(12960, 1e6 as i64).unwrap();

// set a value with a cost of 1
c.insert("a", "a", 1);
c.insert("key1".to_string(), "value1".to_string(), 1);
// set a value with a cost of 1 and ttl
c.insert_with_ttl("b", "b", 1, Duration::from_secs(3));
c.insert_with_ttl(
"key2".to_string(),
"value2".to_string(),
1,
Duration::from_secs(3),
);

// wait for value to pass through buffers
c.wait().unwrap();

// Create a search key
let key1 = "key1".to_string();
// when we get the value, we will get a ValueRef, which contains a RwLockReadGuard
// so when we finish use this value, we must release the ValueRef
let v = c.get(&"a").unwrap();
assert_eq!(v.value(), &"a");
let v = c.get(&key1).unwrap();
assert_eq!(v.value(), &"value1");
v.release();

// lock will be auto released when out of scope
{
// when we get the value, we will get a ValueRef, which contains a RwLockWriteGuard
// so when we finish use this value, we must release the ValueRefMut
let mut v = c.get_mut(&"a").unwrap();
v.write("aa");
assert_eq!(v.value(), &"aa");
let mut v = c.get_mut(&key1).unwrap();
v.write("value3".to_string());
assert_eq!(v.value(), &"value3");
// release the value
}

// if you just want to do one operation
let v = c.get_mut(&"a").unwrap();
v.write_once("aaa");
let v = c.get_mut(&key1).unwrap();
v.write_once("value4".to_string());

let v = c.get(&"a").unwrap();
assert_eq!(v.value(), &"aaa");
let v = c.get(&key1).unwrap();
assert_eq!(v.value(), &"value4");
v.release();

// clear the cache
c.clear().unwrap();
// wait all the operations are finished
c.wait().unwrap();
assert!(c.get(&"a").is_none());
assert!(c.get(&key1).is_none());
}