use std::time::{Duration, SystemTime};
pub(crate) struct CacheEntry<B> {
pub(crate) value: B,
expiration_time: Option<SystemTime>,
}
impl<B> CacheEntry<B> {
pub(crate) fn new(value: B, lifetime: Option<Duration>) -> Self {
Self {
expiration_time: lifetime.map(|dur| SystemTime::now() + dur),
value,
}
}
pub(crate) fn is_expired(&self, current_time: SystemTime) -> bool {
self.expiration_time
.map_or(false, |time| current_time >= time)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn is_expired() {
let now = SystemTime::now();
let entry_expired = CacheEntry::new(1, Some(Duration::from_secs(0)));
let entry_not_expired = CacheEntry::new(1, Some(Duration::from_secs(1)));
let entry_none_duration = CacheEntry::new(1, None);
assert!(entry_expired.is_expired(now));
assert!(!entry_not_expired.is_expired(now));
assert!(!entry_none_duration.is_expired(now));
}
}