2023-05-14 07:03:45 +03:00
|
|
|
mod cache;
|
|
|
|
mod kq_cache;
|
2023-05-14 02:28:27 +03:00
|
|
|
|
2023-05-14 07:03:45 +03:00
|
|
|
pub use cache::CacheWithTTL;
|
|
|
|
pub use kq_cache::{KQCacheWithTTL, PlaceholderGuardWithTTL};
|
2023-05-18 23:34:22 +03:00
|
|
|
pub use quick_cache::sync::{Cache, KQCache};
|
2023-05-15 20:48:59 +03:00
|
|
|
pub use quick_cache::{DefaultHashBuilder, UnitWeighter, Weighter};
|
2023-05-30 01:48:22 +03:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use std::time::Duration;
|
|
|
|
use tokio::task::yield_now;
|
|
|
|
use tokio::time;
|
|
|
|
|
|
|
|
use crate::CacheWithTTL;
|
|
|
|
|
|
|
|
#[tokio::test(start_paused = true)]
|
|
|
|
async fn test_time_passing() {
|
|
|
|
let x = CacheWithTTL::<u32, u32>::new(2, Duration::from_secs(2)).await;
|
|
|
|
|
|
|
|
assert!(x.get(&0).is_none());
|
|
|
|
|
|
|
|
x.try_insert(0, 0).unwrap();
|
|
|
|
|
|
|
|
assert!(x.get(&0).is_some());
|
|
|
|
|
|
|
|
time::advance(Duration::from_secs(1)).await;
|
|
|
|
|
|
|
|
assert!(x.get(&0).is_some());
|
|
|
|
|
|
|
|
time::advance(Duration::from_secs(1)).await;
|
|
|
|
|
|
|
|
// yield so that the expiration code gets a chance to run
|
|
|
|
yield_now().await;
|
|
|
|
|
|
|
|
assert!(x.get(&0).is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test(start_paused = true)]
|
|
|
|
async fn test_capacity_based_eviction() {
|
|
|
|
let x = CacheWithTTL::<u32, ()>::new(1, Duration::from_secs(2)).await;
|
|
|
|
|
|
|
|
assert!(x.get(&0).is_none());
|
|
|
|
|
|
|
|
x.try_insert(0, ()).unwrap();
|
|
|
|
|
|
|
|
assert!(x.get(&0).is_some());
|
|
|
|
|
|
|
|
x.try_insert(1, ()).unwrap();
|
|
|
|
|
|
|
|
assert!(x.get(&1).is_some());
|
|
|
|
assert!(x.get(&0).is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
// #[tokio::test(start_paused = true)]
|
|
|
|
// async fn test_overweight() {
|
|
|
|
// todo!("wip");
|
|
|
|
// }
|
|
|
|
}
|