credence_lib/configuration/
caching.rs

1use super::super::resolve::*;
2
3use {
4    compris::resolve::*,
5    kutil::{
6        cli::depict::*,
7        http::cache::{implementation::moka::*, *},
8        std::metric::*,
9    },
10    moka::future::Cache,
11    std::time::*,
12};
13
14//
15// CachingConfiguration
16//
17
18/// Caching configuration.
19#[derive(Clone, Debug, Depict, Resolve)]
20pub struct CachingConfiguration {
21    /// Default.
22    #[resolve]
23    #[depict(style(symbol))]
24    pub default: bool,
25
26    /// Capacity.
27    #[resolve]
28    #[depict(as(display), style(symbol))]
29    pub capacity: ResolveByteCount,
30
31    /// Cache entry duration.
32    #[resolve(key = "duration")]
33    #[depict(as(custom(resolve_duration_to_string)), style(symbol))]
34    pub duration: ResolveDuration,
35
36    /// Minimum cacheable body size.
37    #[resolve(key = "min-body-size")]
38    #[depict(as(display), style(symbol))]
39    pub min_body_size: ResolveByteCount,
40
41    /// Maximum cacheable body size.
42    #[resolve(key = "max-body-size")]
43    #[depict(as(display), style(symbol))]
44    pub max_body_size: ResolveByteCount,
45}
46
47impl CachingConfiguration {
48    /// Cache.
49    pub fn cache(&self) -> MokaCacheImplementation {
50        let cache = Cache::<CommonCacheKey, _>::builder()
51            .for_http_response()
52            .max_capacity(self.capacity.inner.into())
53            .time_to_live(self.duration.inner.into())
54            .eviction_listener(|key, _value, cause| {
55                tracing::debug!("evict ({:?}): {}", cause, key);
56            })
57            .build();
58
59        MokaCacheImplementation::new(cache)
60    }
61}
62
63impl Default for CachingConfiguration {
64    fn default() -> Self {
65        Self {
66            default: true,
67            capacity: ByteCount::from_gibibytes(1).into(),
68            duration: Duration::from_secs(5).into(),
69            min_body_size: Default::default(),
70            max_body_size: ByteCount::from_mebibytes(10).into(),
71        }
72    }
73}