tinylfu_cached/cache/expiration/
config.rs

1use std::time::Duration;
2use crate::cache::clock::ClockType;
3use crate::cache::types::TotalShards;
4
5/// Defines the config for `crate::cache::expiration::TTLTicker`
6/// TTLTicker is a shared lock based HashMap. Each shard holds a [`parking_lot::RwLock`] protected [`hashbrown::HashMap`]
7/// `shards` define the total number of shards to be used inside `TTLTicker`
8/// `tick_duration` defines the interval at which `TTLTicker` should run
9/// `clock` defines an implementation of [`crate::cache::clock::Clock`] to be used to get the current time
10pub(crate) struct TTLConfig {
11    shards: TotalShards,
12    tick_duration: Duration,
13    clock: ClockType,
14}
15
16impl TTLConfig {
17    pub(crate) fn new(shards: TotalShards, tick_duration: Duration, clock: ClockType) -> Self {
18        TTLConfig {
19            shards,
20            tick_duration,
21            clock,
22        }
23    }
24
25    pub(crate) fn shards(&self) -> TotalShards { self.shards }
26
27    pub(crate) fn tick_duration(&self) -> Duration { self.tick_duration }
28
29    pub(crate) fn clock(&self) -> ClockType { self.clock.clone_box() }
30}
31
32#[cfg(test)]
33mod tests {
34    use std::time::Duration;
35    use crate::cache::clock::SystemClock;
36    use crate::cache::expiration::config::TTLConfig;
37
38    #[test]
39    fn ttl_shards() {
40        let ttl_config = TTLConfig::new(4, Duration::from_millis(10), SystemClock::boxed());
41        assert_eq!(4, ttl_config.shards());
42    }
43
44    #[test]
45    fn ttl_tick_duration() {
46        let ttl_config = TTLConfig::new(4, Duration::from_millis(20), SystemClock::boxed());
47        assert_eq!(Duration::from_millis(20), ttl_config.tick_duration());
48    }
49}