diff options
author | alyx <alyx@aleteoryx.me> | 2023-08-09 16:59:03 -0400 |
---|---|---|
committer | alyx <alyx@aleteoryx.me> | 2023-08-09 16:59:03 -0400 |
commit | ab822e81f8e06fbdc274090fc0a3fb31ef7f7ed1 (patch) | |
tree | aae941f966467b31c6a2dbb4d85f736020fc67db /src/cache.rs | |
parent | 70029b0ba7593c069632be9f434b0fa9e2b80417 (diff) | |
download | lfm_embed-ab822e81f8e06fbdc274090fc0a3fb31ef7f7ed1.tar.gz lfm_embed-ab822e81f8e06fbdc274090fc0a3fb31ef7f7ed1.tar.bz2 lfm_embed-ab822e81f8e06fbdc274090fc0a3fb31ef7f7ed1.zip |
pull down user info
Diffstat (limited to 'src/cache.rs')
-rw-r--r-- | src/cache.rs | 15 |
1 files changed, 8 insertions, 7 deletions
diff --git a/src/cache.rs b/src/cache.rs index 482a9b8..dfe23e7 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -1,4 +1,5 @@ use std::{future::Future, time::*, collections::HashMap, hash::Hash}; +use reqwest::StatusCode; #[derive(Debug)] pub struct AsyncCache<K, V, F> { func: F, @@ -8,9 +9,9 @@ pub struct AsyncCache<K, V, F> { impl<K, V, F, Fut> AsyncCache<K, V, F> where - F: for<'a> FnMut(&'a K) -> Fut, + for<'a> F: FnMut(&'a K) -> Fut + 'a, K: Hash + PartialEq + Eq + Clone, - Fut: Future<Output = Result<V, &'static str>> + Fut: Future<Output = Result<V, (StatusCode, &'static str)>> { pub fn new(interval: Duration, func: F) -> Self { Self{ @@ -19,7 +20,7 @@ where } } - pub async fn get(&mut self, key: &K) -> Result<&V, &'static str> { + pub async fn get(&mut self, key: &K) -> Result<&V, (StatusCode, &'static str)> { if self.is_stale(&key) { self.renew(&key).await } else { @@ -27,7 +28,7 @@ where } } - pub async fn renew(&mut self, key: &K) -> Result<&V, &'static str> { + 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) @@ -51,12 +52,12 @@ where impl<K, V, F, Fut> AsyncCache<K, V, F> where - F: for<'a> FnMut(&'a K) -> Fut, + for<'a> F: FnMut(&'a K) -> Fut + 'a, K: Hash + PartialEq + Eq + Clone, V: Clone, - Fut: Future<Output = Result<V, &'static str>> + Fut: Future<Output = Result<V, (StatusCode, &'static str)>> { - pub async fn get_owned(&mut self, key: &K) -> Result<V, &'static str> { + pub async fn get_owned(&mut self, key: &K) -> Result<V, (StatusCode, &'static str)> { self.get(key).await.cloned() } } |