sqlx_cache/
cache_task.rs

1use std::any::Any;
2use std::cmp::Ordering;
3use std::time::{SystemTime, UNIX_EPOCH};
4
5pub enum CacheTask {
6    INVALIDATION { exp_time: u128, cache_id: &'static str, key: Box<dyn Any + Send> }
7}
8
9
10impl CacheTask {
11    pub fn invalidation(expires_in: u64, cache_id: &'static str, key: Box<dyn Any + Send>) -> CacheTask {
12        let exp_time = SystemTime::now().duration_since(UNIX_EPOCH)
13            .expect("Time went backwards").as_millis() + expires_in as u128;
14
15        CacheTask::INVALIDATION {
16            exp_time,
17            cache_id,
18            key,
19        }
20    }
21
22    pub fn exp_time(&self) -> u128 {
23        match self {
24            CacheTask::INVALIDATION { exp_time, .. } => {
25                *exp_time
26            }
27        }
28    }
29    pub fn is_expired(&self) -> bool {
30        self.exp_time() <= SystemTime::now().duration_since(UNIX_EPOCH)
31            .expect("Time went backwards").as_millis()
32    }
33}
34
35impl Eq for CacheTask {}
36
37impl PartialEq<Self> for CacheTask {
38    fn eq(&self, other: &Self) -> bool {
39        self.exp_time() == other.exp_time()
40    }
41}
42
43impl PartialOrd<Self> for CacheTask {
44    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
45        Some(self.cmp(other))
46    }
47}
48
49impl Ord for CacheTask {
50    // Ordering as priority
51    fn cmp(&self, other: &Self) -> Ordering {
52        if self.exp_time() > other.exp_time() {
53            return Ordering::Less;
54        }
55
56        Ordering::Greater
57    }
58}
59
60
61
62
63