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

Reduce the TTL from 3 secs to 2.5 secs in the cascading drop example #365

Merged
merged 2 commits into from
Dec 28, 2023
Merged
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
19 changes: 12 additions & 7 deletions examples/cascading_drop_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ pub struct Session {

impl Drop for Session {
fn drop(&mut self) {
let user_id;
user_id = self.ptr.as_ref().unwrap().lock().unwrap().user_id;
let user_id = self.ptr.as_ref().unwrap().lock().unwrap().user_id;
println!("Dropping session holding a reference to user {}", user_id);
self.ptr = None; // Must drop Arc before verify Btree!!!
let _ = self.sender.send(user_id);
Expand Down Expand Up @@ -115,11 +114,17 @@ async fn main() {
}
});

// Make an artificially small cache and 1-second ttl to observe pruning of the tree.
let ttl = 3;
// Later, we will check the entry count of the session_cache with this time_step
// interval.
let time_step = 1; // second

// Make an artificially small cache and 2.5-second ttl to observe pruning of the tree.
// Caution: setting ttl to exact integer multiples of the time steps may cause
// different behavior than you expect, due to rounding or race conditions.
let ttl_ms = 2500;
let sessions_cache = Cache::builder()
.max_capacity(10)
.time_to_live(Duration::from_secs(ttl))
.time_to_live(Duration::from_millis(ttl_ms))
.eviction_listener(|key, value: Arc<Mutex<Session>>, cause| {
println!(
"Evicted session with key {:08X} of user_id {:?} because {:?}",
Expand Down Expand Up @@ -178,8 +183,8 @@ async fn main() {
}

println!("Waiting");
for t in 1..=ttl + 1 {
sleep(Duration::from_secs(1));
for t in 1..=4 {
sleep(Duration::from_secs(time_step));
sessions_cache.get(&0).await;
sessions_cache.run_pending_tasks().await;
println!("t = {}, pending: {}", t, sessions_cache.entry_count());
Expand Down
Loading