atrium_common/resolver/
cached.rs

1use std::hash::Hash;
2
3use crate::types::cached::r#impl::{Cache, CacheImpl};
4use crate::types::cached::Cached;
5
6use super::Resolver;
7
8pub type CachedResolver<R> = Cached<R, CacheImpl<<R as Resolver>::Input, <R as Resolver>::Output>>;
9
10impl<R, C> Resolver for Cached<R, C>
11where
12    R: Resolver + Send + Sync + 'static,
13    R::Input: Clone + Hash + Eq + Send + Sync + 'static,
14    R::Output: Clone + Send + Sync + 'static,
15    C: Cache<Input = R::Input, Output = R::Output> + Send + Sync + 'static,
16    C::Input: Clone + Hash + Eq + Send + Sync + 'static,
17    C::Output: Clone + Send + Sync + 'static,
18{
19    type Input = R::Input;
20    type Output = R::Output;
21    type Error = R::Error;
22
23    async fn resolve(&self, input: &Self::Input) -> Result<Self::Output, Self::Error> {
24        if let Some(output) = self.cache.get(input).await {
25            return Ok(output);
26        }
27        let output = self.inner.resolve(input).await?;
28        self.cache.set(input.clone(), output.clone()).await;
29        Ok(output)
30    }
31}