aboutsummaryrefslogtreecommitdiffstats
path: root/src/cache.rs
diff options
context:
space:
mode:
authoralyx <alyx@aleteoryx.me>2023-08-10 03:10:54 -0400
committeralyx <alyx@aleteoryx.me>2023-08-10 03:10:54 -0400
commit4bec6679a8af2a2b5cb53610a80dece3b6d30bb4 (patch)
tree686297ae76fbc05b4e6af04eaf66b1545e055e4a /src/cache.rs
parent60f8bc89d2fb182906fbedb4d4dc39d87af7d8e5 (diff)
downloadlfm_embed-4bec6679a8af2a2b5cb53610a80dece3b6d30bb4.tar.gz
lfm_embed-4bec6679a8af2a2b5cb53610a80dece3b6d30bb4.tar.bz2
lfm_embed-4bec6679a8af2a2b5cb53610a80dece3b6d30bb4.zip
lots of stuff. theming basically done, added some logging, frontend basically done.
Diffstat (limited to 'src/cache.rs')
-rw-r--r--src/cache.rs11
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()