From 4dc0d361aeb9d8cb829f01d0e37b043c7f14ea6f Mon Sep 17 00:00:00 2001 From: ByteAlex Date: Tue, 21 Sep 2021 09:02:56 +0200 Subject: [PATCH 1/2] Replace SystemTime with instants --- src/backing.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/backing.rs b/src/backing.rs index c8e91f8..4e75edc 100644 --- a/src/backing.rs +++ b/src/backing.rs @@ -5,9 +5,10 @@ use lru::LruCache; #[cfg(feature = "ttl-cache")] use std::collections::VecDeque; #[cfg(feature = "ttl-cache")] -use std::time::{SystemTime, Duration}; -#[cfg(feature = "ttl-cache")] use std::ops::Add; +#[cfg(feature = "ttl-cache")] +use tokio::time::Instant; +use tokio::time::Duration; pub trait CacheBacking where K: Eq + Hash + Sized + Clone + Send, @@ -88,8 +89,8 @@ impl< #[cfg(feature = "ttl-cache")] pub struct TtlCacheBacking { ttl: Duration, - expiry_queue: VecDeque<(K, SystemTime)>, - map: HashMap, + expiry_queue: VecDeque<(K, Instant)>, + map: HashMap, } #[cfg(feature = "ttl-cache")] @@ -111,7 +112,7 @@ impl< fn set(&mut self, key: K, value: V) -> Option { self.remove_old(); - let expiry = SystemTime::now().add(self.ttl); + let expiry = Instant::now().add(self.ttl); let option = self.map.insert(key.clone(), (value, expiry)); if option.is_some() { self.expiry_queue.retain(|(vec_key, _)| vec_key.ne(&key)); @@ -132,7 +133,7 @@ impl< fn contains_key(&self, key: &K) -> bool { // we cant clean old keys on this, since the self ref is not mutable :( self.map.get(key) - .filter(|(_, expiry)| SystemTime::now().lt(expiry)) + .filter(|(_, expiry)| Instant::now().lt(expiry)) .is_some() } @@ -165,7 +166,7 @@ impl TtlCacheBacking { } fn remove_old(&mut self) { - let now = SystemTime::now(); + let now = Instant::now(); while let Some((key, expiry)) = self.expiry_queue.pop_front() { if now.lt(&expiry) { self.expiry_queue.push_front((key, expiry)); From 8c7e185889df5ba7b29f217c972aacaf6104d1fe Mon Sep 17 00:00:00 2001 From: ByteAlex Date: Tue, 21 Sep 2021 09:03:57 +0200 Subject: [PATCH 2/2] Fix import --- src/backing.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/backing.rs b/src/backing.rs index 4e75edc..c85e34f 100644 --- a/src/backing.rs +++ b/src/backing.rs @@ -7,8 +7,7 @@ use std::collections::VecDeque; #[cfg(feature = "ttl-cache")] use std::ops::Add; #[cfg(feature = "ttl-cache")] -use tokio::time::Instant; -use tokio::time::Duration; +use tokio::time::{Instant, Duration}; pub trait CacheBacking where K: Eq + Hash + Sized + Clone + Send,