Skip to content

Commit 7dcfc64

Browse files
authored
Replace SystemTime with Instant (#8)
* Replace SystemTime with instants * Fix import
1 parent 0055df8 commit 7dcfc64

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

src/backing.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ use lru::LruCache;
55
#[cfg(feature = "ttl-cache")]
66
use std::collections::VecDeque;
77
#[cfg(feature = "ttl-cache")]
8-
use std::time::{SystemTime, Duration};
9-
#[cfg(feature = "ttl-cache")]
108
use std::ops::Add;
9+
#[cfg(feature = "ttl-cache")]
10+
use tokio::time::{Instant, Duration};
1111

1212
pub trait CacheBacking<K, V>
1313
where K: Eq + Hash + Sized + Clone + Send,
@@ -88,8 +88,8 @@ impl<
8888
#[cfg(feature = "ttl-cache")]
8989
pub struct TtlCacheBacking<K, V> {
9090
ttl: Duration,
91-
expiry_queue: VecDeque<(K, SystemTime)>,
92-
map: HashMap<K, (V, SystemTime)>,
91+
expiry_queue: VecDeque<(K, Instant)>,
92+
map: HashMap<K, (V, Instant)>,
9393
}
9494

9595
#[cfg(feature = "ttl-cache")]
@@ -111,7 +111,7 @@ impl<
111111

112112
fn set(&mut self, key: K, value: V) -> Option<V> {
113113
self.remove_old();
114-
let expiry = SystemTime::now().add(self.ttl);
114+
let expiry = Instant::now().add(self.ttl);
115115
let option = self.map.insert(key.clone(), (value, expiry));
116116
if option.is_some() {
117117
self.expiry_queue.retain(|(vec_key, _)| vec_key.ne(&key));
@@ -132,7 +132,7 @@ impl<
132132
fn contains_key(&self, key: &K) -> bool {
133133
// we cant clean old keys on this, since the self ref is not mutable :(
134134
self.map.get(key)
135-
.filter(|(_, expiry)| SystemTime::now().lt(expiry))
135+
.filter(|(_, expiry)| Instant::now().lt(expiry))
136136
.is_some()
137137
}
138138

@@ -165,7 +165,7 @@ impl<K: Hash + Sized + PartialEq + Eq, V> TtlCacheBacking<K, V> {
165165
}
166166

167167
fn remove_old(&mut self) {
168-
let now = SystemTime::now();
168+
let now = Instant::now();
169169
while let Some((key, expiry)) = self.expiry_queue.pop_front() {
170170
if now.lt(&expiry) {
171171
self.expiry_queue.push_front((key, expiry));

0 commit comments

Comments
 (0)