Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/backing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ 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, Duration};

pub trait CacheBacking<K, V>
where K: Eq + Hash + Sized + Clone + Send,
Expand Down Expand Up @@ -88,8 +88,8 @@ impl<
#[cfg(feature = "ttl-cache")]
pub struct TtlCacheBacking<K, V> {
ttl: Duration,
expiry_queue: VecDeque<(K, SystemTime)>,
map: HashMap<K, (V, SystemTime)>,
expiry_queue: VecDeque<(K, Instant)>,
map: HashMap<K, (V, Instant)>,
}

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

fn set(&mut self, key: K, value: V) -> Option<V> {
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));
Expand All @@ -132,7 +132,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()
}

Expand Down Expand Up @@ -165,7 +165,7 @@ impl<K: Hash + Sized + PartialEq + Eq, V> TtlCacheBacking<K, V> {
}

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));
Expand Down