aboutsummaryrefslogtreecommitdiffstats
path: root/src/cache.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/cache.rs')
-rw-r--r--src/cache.rs15
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()
}
}