diff options
Diffstat (limited to 'src/shared_cache.rs')
| -rw-r--r-- | src/shared_cache.rs | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/shared_cache.rs b/src/shared_cache.rs new file mode 100644 index 0000000..4d6aec2 --- /dev/null +++ b/src/shared_cache.rs @@ -0,0 +1,27 @@ +use std::{ + collections::HashMap, + sync::{Arc, Mutex}, + time::{SystemTime, UNIX_EPOCH}, +}; + +#[derive(Debug, Clone)] +pub struct CacheEntry { + pub value: String, + pub expires_at: Option<u64>, // Unix timestamp in milliseconds +} + +impl CacheEntry { + pub fn is_expired(&self) -> bool { + if let Some(expiry) = self.expires_at { + let now = SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_millis() as u64; + now > expiry + } else { + false + } + } +} + +pub type SharedCache = Arc<Mutex<HashMap<String, CacheEntry>>>; |
