credence_lib/configuration/
caching.rs

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