aws_secretsmanager_cache/
config.rs

1const DEFAULT_MAX_CACHE_SIZE: usize = 1024;
2const DEFAULT_CACHE_ITEM_TTL: u128 = 3600000000000; // 1 hour in nanoseconds
3const DEFAULT_VERSION_STAGE: &str = "AWSCURRENT";
4
5/// Configuration options for the SecretCache.
6///
7/// Defaults:
8/// - max_cache_size: 1024
9/// - cache_item_ttl: 3600000000000 (1hr)
10/// - version_stage: "AWSCURRENT"
11pub struct CacheConfig {
12    /// The maximum number of secrets to maintain in the cache.
13    ///
14    /// The least frequently accessed items will be evicted from the cache
15    /// once a max_cache_size number of items are stored.
16    ///
17    /// Default: 1024
18    pub max_cache_size: usize,
19
20    /// The TTL expiry of items in the cache.
21    ///
22    /// Determines the number of nanoseconds a cached secret will be considered valid before
23    /// the secret value is required to be refreshed. Refreshing happens synchronously.
24    ///
25    /// Default: 3600000000000 (1 hour in nanoseconds)
26    pub cache_item_ttl: u128,
27
28    /// The version stage used when requesting secrets from AWS Secrets Manager.
29    ///
30    /// Default: "AWSCURRENT"
31    pub version_stage: String,
32}
33
34impl CacheConfig {
35    /// Returns a new Cache Configuration with default values set.
36    ///
37    /// Defaults:
38    /// - max_cache_size: 1024
39    /// - cache_item_ttl: 3600000000000 (1hr)
40    /// - version_stage: "AWSCURRENT"
41    pub fn new() -> Self {
42        CacheConfig {
43            max_cache_size: DEFAULT_MAX_CACHE_SIZE,
44            cache_item_ttl: DEFAULT_CACHE_ITEM_TTL,
45            version_stage: DEFAULT_VERSION_STAGE.to_string(),
46        }
47    }
48
49    /// Sets the max_cache_size cache configuration option to a different value.
50    pub fn max_cache_size(mut self, max_cache_size: usize) -> Self {
51        self.max_cache_size = max_cache_size;
52        self
53    }
54
55    /// Sets the cache_item_ttl cache configuration option to a different value.
56    pub fn cache_item_ttl(mut self, cache_item_ttl: u128) -> Self {
57        self.cache_item_ttl = cache_item_ttl;
58        self
59    }
60}
61
62impl Default for CacheConfig {
63    fn default() -> Self {
64        Self::new()
65    }
66}
67
68#[cfg(test)]
69mod tests {
70    use super::*;
71    use std::time;
72
73    #[test]
74    fn cache_config_default() {
75        let cache_config = CacheConfig::new();
76
77        assert_eq!(cache_config.cache_item_ttl, DEFAULT_CACHE_ITEM_TTL);
78        assert_eq!(cache_config.max_cache_size, DEFAULT_MAX_CACHE_SIZE);
79        assert_eq!(cache_config.version_stage, DEFAULT_VERSION_STAGE);
80    }
81
82    #[test]
83    fn cache_config_custom() {
84        let custom_cache_ttl = time::Duration::from_secs(30).as_nanos();
85        let cache_config = CacheConfig::new()
86            .max_cache_size(10)
87            .cache_item_ttl(custom_cache_ttl);
88
89        assert_eq!(cache_config.cache_item_ttl, custom_cache_ttl);
90        assert_eq!(cache_config.max_cache_size, 10);
91        assert_eq!(cache_config.version_stage, DEFAULT_VERSION_STAGE);
92    }
93
94    #[test]
95    fn cache_config_partial_config() {
96        let cache_config = CacheConfig::new().max_cache_size(10);
97
98        assert_eq!(cache_config.cache_item_ttl, DEFAULT_CACHE_ITEM_TTL);
99        assert_eq!(cache_config.max_cache_size, 10);
100        assert_eq!(cache_config.version_stage, DEFAULT_VERSION_STAGE);
101    }
102}