const DEFAULT_MAX_CACHE_SIZE: usize = 1024;
const DEFAULT_CACHE_ITEM_TTL: u128 = 3600000000000; const DEFAULT_VERSION_STAGE: &str = "AWSCURRENT";
pub struct CacheConfig {
pub max_cache_size: usize,
pub cache_item_ttl: u128,
pub version_stage: String,
}
impl CacheConfig {
pub fn new() -> Self {
CacheConfig {
max_cache_size: DEFAULT_MAX_CACHE_SIZE,
cache_item_ttl: DEFAULT_CACHE_ITEM_TTL,
version_stage: DEFAULT_VERSION_STAGE.to_string(),
}
}
pub fn max_cache_size(mut self, max_cache_size: usize) -> Self {
self.max_cache_size = max_cache_size;
self
}
pub fn cache_item_ttl(mut self, cache_item_ttl: u128) -> Self {
self.cache_item_ttl = cache_item_ttl;
self
}
}
impl Default for CacheConfig {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::time;
#[test]
fn cache_config_default() {
let cache_config = CacheConfig::new();
assert_eq!(cache_config.cache_item_ttl, DEFAULT_CACHE_ITEM_TTL);
assert_eq!(cache_config.max_cache_size, DEFAULT_MAX_CACHE_SIZE);
assert_eq!(cache_config.version_stage, DEFAULT_VERSION_STAGE);
}
#[test]
fn cache_config_custom() {
let custom_cache_ttl = time::Duration::from_secs(30).as_nanos();
let cache_config = CacheConfig::new()
.max_cache_size(10)
.cache_item_ttl(custom_cache_ttl);
assert_eq!(cache_config.cache_item_ttl, custom_cache_ttl);
assert_eq!(cache_config.max_cache_size, 10);
assert_eq!(cache_config.version_stage, DEFAULT_VERSION_STAGE);
}
#[test]
fn cache_config_partial_config() {
let cache_config = CacheConfig::new().max_cache_size(10);
assert_eq!(cache_config.cache_item_ttl, DEFAULT_CACHE_ITEM_TTL);
assert_eq!(cache_config.max_cache_size, 10);
assert_eq!(cache_config.version_stage, DEFAULT_VERSION_STAGE);
}
}