use crate::{Cache, CacheConfig};
use std::hash::Hash;
use std::sync::Arc;
use tower::Layer;
#[derive(Clone)]
pub struct CacheLayer<Req, K> {
config: Arc<CacheConfig<Req, K>>,
}
impl<Req, K> CacheLayer<Req, K>
where
K: Hash + Eq + Clone + Send + 'static,
{
pub fn new(config: CacheConfig<Req, K>) -> Self {
Self {
config: Arc::new(config),
}
}
pub fn builder() -> crate::CacheConfigBuilder<Req, K> {
crate::CacheConfigBuilder::new()
}
}
impl<S, Req, K> Layer<S> for CacheLayer<Req, K>
where
K: Hash + Eq + Clone + Send + 'static,
S: tower::Service<Req>,
S::Response: Clone + Send + 'static,
{
type Service = Cache<S, Req, K, S::Response>;
fn layer(&self, service: S) -> Self::Service {
Cache::new(service, Arc::clone(&self.config))
}
}