diff options
Diffstat (limited to 'src/cache.rs')
-rw-r--r-- | src/cache.rs | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/src/cache.rs b/src/cache.rs index dfe23e7..631f900 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -11,7 +11,7 @@ impl<K, V, F, Fut> AsyncCache<K, V, F> where for<'a> F: FnMut(&'a K) -> Fut + 'a, K: Hash + PartialEq + Eq + Clone, - Fut: Future<Output = Result<V, (StatusCode, &'static str)>> + Fut: Future<Output = Result<V, (StatusCode, &'static str)>> + Send + Sync { pub fn new(interval: Duration, func: F) -> Self { Self{ @@ -22,8 +22,10 @@ where pub async fn get(&mut self, key: &K) -> Result<&V, (StatusCode, &'static str)> { if self.is_stale(&key) { + log::trace!(target: "lfm::cache", "MISS : interval = {:?}", self.interval); self.renew(&key).await } else { + log::trace!(target: "lfm::cache", "HIT : interval = {:?}", self.interval); Ok(&self.cache.get(&key).unwrap().1) } } @@ -37,7 +39,8 @@ where pub fn is_stale(&self, key: &K) -> bool { if let Some((last_update, _)) = self.cache.get(key) { let now = Instant::now(); - now < (*last_update + self.interval) + log::trace!(target: "lfm::cache", "Key exists, last update {:?} ago.", now - *last_update); + now > (*last_update + self.interval) } else { true } } @@ -48,6 +51,8 @@ where } else { None } } + + pub fn interval(&self) -> Duration { self.interval } } impl<K, V, F, Fut> AsyncCache<K, V, F> @@ -55,7 +60,7 @@ where for<'a> F: FnMut(&'a K) -> Fut + 'a, K: Hash + PartialEq + Eq + Clone, V: Clone, - Fut: Future<Output = Result<V, (StatusCode, &'static str)>> + Fut: Future<Output = Result<V, (StatusCode, &'static str)>> + Send + Sync { pub async fn get_owned(&mut self, key: &K) -> Result<V, (StatusCode, &'static str)> { self.get(key).await.cloned() |