Skip to content

Commit

Permalink
Merge pull request #365 from moka-rs/tweak-cascading-drop-example
Browse files Browse the repository at this point in the history
Reduce the TTL from 3 secs to 2.5 secs in the cascading drop example
  • Loading branch information
tatsuya6502 authored Dec 28, 2023
2 parents e63c842 + 7d8f64f commit cdb0028
Showing 1 changed file with 12 additions and 7 deletions.
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

0 comments on commit cdb0028

Please sign in to comment.