Skip to main content

reinhardt_conf/settings/
cache.rs

1//! Cache settings fragment
2//!
3//! Provides composable cache configuration as a [`SettingsFragment`](crate::settings::fragment::SettingsFragment).
4
5use reinhardt_core::macros::settings;
6use serde::{Deserialize, Serialize};
7
8fn default_backend() -> String {
9	"memory".to_string()
10}
11
12fn default_timeout() -> u64 {
13	300
14}
15
16/// Cache configuration fragment.
17///
18/// Controls the cache backend, location, and default timeout.
19#[settings(fragment = true, section = "cache")]
20#[non_exhaustive]
21#[derive(Clone, Debug, Serialize, Deserialize)]
22pub struct CacheSettings {
23	/// Cache backend type (e.g., `"memory"`, `"redis"`, `"database"`).
24	#[serde(default = "default_backend")]
25	pub backend: String,
26	/// Backend-specific connection location or URL.
27	pub location: Option<String>,
28	/// Default cache entry timeout in seconds.
29	#[serde(default = "default_timeout")]
30	pub timeout: u64,
31}
32
33impl Default for CacheSettings {
34	fn default() -> Self {
35		Self {
36			backend: "memory".to_string(),
37			location: None,
38			timeout: 300,
39		}
40	}
41}
42
43#[cfg(test)]
44mod tests {
45	use super::*;
46	use crate::settings::fragment::SettingsFragment;
47	use rstest::rstest;
48
49	#[rstest]
50	fn test_cache_section_name() {
51		// Arrange / Act
52		let section = CacheSettings::section();
53
54		// Assert
55		assert_eq!(section, "cache");
56	}
57
58	#[rstest]
59	fn test_cache_default_values() {
60		// Arrange / Act
61		let settings = CacheSettings::default();
62
63		// Assert
64		assert_eq!(settings.backend, "memory");
65		assert!(settings.location.is_none());
66		assert_eq!(settings.timeout, 300);
67	}
68}