use std::any::Any;
use std::cmp::Ordering;
use std::time::{SystemTime, UNIX_EPOCH};
pub enum CacheTask {
INVALIDATION { exp_time: u128, cache_id: &'static str, key: Box<dyn Any + Send> }
}
impl CacheTask {
pub fn invalidation(expires_in: u64, cache_id: &'static str, key: Box<dyn Any + Send>) -> CacheTask {
let exp_time = SystemTime::now().duration_since(UNIX_EPOCH)
.expect("Time went backwards").as_millis() + expires_in as u128;
CacheTask::INVALIDATION {
exp_time,
cache_id,
key,
}
}
pub fn exp_time(&self) -> u128 {
match self {
CacheTask::INVALIDATION { exp_time, .. } => {
*exp_time
}
}
}
pub fn is_expired(&self) -> bool {
self.exp_time() <= SystemTime::now().duration_since(UNIX_EPOCH)
.expect("Time went backwards").as_millis()
}
}
impl Eq for CacheTask {}
impl PartialEq<Self> for CacheTask {
fn eq(&self, other: &Self) -> bool {
self.exp_time() == other.exp_time()
}
}
impl PartialOrd<Self> for CacheTask {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for CacheTask {
fn cmp(&self, other: &Self) -> Ordering {
if self.exp_time() > other.exp_time() {
return Ordering::Less;
}
Ordering::Greater
}
}