From d2c27e17897f80929d2ba5fed1055eade27a6b08 Mon Sep 17 00:00:00 2001 From: alyx Date: Fri, 12 Jan 2024 12:17:57 -0500 Subject: Cleanup --- src/cache.rs | 148 ++++++++++++++++++----------------------------------------- 1 file changed, 45 insertions(+), 103 deletions(-) (limited to 'src/cache.rs') diff --git a/src/cache.rs b/src/cache.rs index 631f900..dbb3759 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -2,125 +2,67 @@ use std::{future::Future, time::*, collections::HashMap, hash::Hash}; use reqwest::StatusCode; #[derive(Debug)] pub struct AsyncCache { - func: F, - cache: HashMap, - interval: Duration + func: F, + cache: HashMap, + interval: Duration } impl AsyncCache where - for<'a> F: FnMut(&'a K) -> Fut + 'a, - K: Hash + PartialEq + Eq + Clone, - Fut: Future> + Send + Sync + for<'a> F: FnMut(&'a K) -> Fut + 'a, + K: Hash + PartialEq + Eq + Clone, + Fut: Future> + Send + Sync { - pub fn new(interval: Duration, func: F) -> Self { - Self{ - cache: HashMap::new(), - interval, func - } - } - - 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) - } - } - - pub async fn renew(&mut self, key: &K) -> Result<&V, (StatusCode, &'static str)> { - let val = (self.func)(&key).await?; - self.cache.insert(key.clone(), (Instant::now(), val)); - Ok(&self.cache.get(key).unwrap().1) + pub fn new(interval: Duration, func: F) -> Self { + Self{ + cache: HashMap::new(), + interval, func } + } - pub fn is_stale(&self, key: &K) -> bool { - if let Some((last_update, _)) = self.cache.get(key) { - let now = Instant::now(); - log::trace!(target: "lfm::cache", "Key exists, last update {:?} ago.", now - *last_update); - now > (*last_update + self.interval) - } - else { true } - } - - pub async fn get_opt(&self, key: &K) -> Option<&V> { - if self.is_stale(key) { - self.cache.get(key).map(|(_, v)| v) - } - else { None } + 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) } + } - pub fn interval(&self) -> Duration { self.interval } -} + pub async fn renew(&mut self, key: &K) -> Result<&V, (StatusCode, &'static str)> { + let val = (self.func)(&key).await?; + self.cache.insert(key.clone(), (Instant::now(), val)); + Ok(&self.cache.get(key).unwrap().1) + } -impl AsyncCache -where - for<'a> F: FnMut(&'a K) -> Fut + 'a, - K: Hash + PartialEq + Eq + Clone, - V: Clone, - Fut: Future> + Send + Sync -{ - pub async fn get_owned(&mut self, key: &K) -> Result { - self.get(key).await.cloned() + pub fn is_stale(&self, key: &K) -> bool { + if let Some((last_update, _)) = self.cache.get(key) { + let now = Instant::now(); + log::trace!(target: "lfm::cache", "Key exists, last update {:?} ago.", now - *last_update); + now > (*last_update + self.interval) } -} -/* -pub struct AsyncCache { - func: F, - cache: HashMap, - interval: Duration -} + else { true } + } -impl AsyncCache -where - for<'a> F: FnMut(&'a K) -> Fut + 'a, - Fut: Future -{ - pub fn new(interval: Duration, mut func: F) -> Self { - Self{ - cache: HashMap::new(), - interval, func - } - } - - pub async fn get(&mut self, key: &K) -> &V { - if self.is_stale(key) { - self.renew().await - } else { - self.cache.get(key) - } - } - - pub async fn renew(&mut self, key: &K) -> &V { - self.cache.get_mut(key).0 = now; - self.cache.get_mut(key).1 = (self.func)(key).await; - self.cache.get(key) + pub async fn get_opt(&self, key: &K) -> Option<&V> { + if self.is_stale(key) { + self.cache.get(key).map(|(_, v)| v) } + else { None } + } - pub fn is_stale(&self, key: &K) -> bool { - let now = Instant::now(); - let last_update = self.cache.get(key).0; - now < (last_update + self.interval) - } - - pub fn get_opt(&self, key: &K) -> Option<&T> { - if self.is_stale(key) { - Some(self.cache.get(key)) - } - else { None } - } + pub fn interval(&self) -> Duration { self.interval } } -impl AsyncCache +impl AsyncCache where - F: for<'a> FnMut(&'a K) -> Fut + 'a, - Fut: Future, - V: Clone + for<'a> F: FnMut(&'a K) -> Fut + 'a, + K: Hash + PartialEq + Eq + Clone, + V: Clone, + Fut: Future> + Send + Sync { - pub async fn get_owned(&mut self, key: &K) -> V { - self.get(key).await.clone() - } + pub async fn get_owned(&mut self, key: &K) -> Result { + self.get(key).await.cloned() + } } -*/ -- cgit v1.2.3-54-g00ecf