use std::net::SocketAddr;
use std::path::PathBuf;
use std::time::Duration;
use super::tiers::{EvictionPolicy, CompressionType};
#[derive(Debug, Clone)]
pub struct DistribCacheConfig {
pub enabled: bool,
pub l1_size_mb: usize,
pub l1_max_entry_size: usize,
pub l1_eviction_policy: EvictionPolicy,
pub l2_enabled: bool,
pub l2_size_gb: u64,
pub l2_path: PathBuf,
pub l2_compression: CompressionType,
pub l3_enabled: bool,
pub l3_replication_factor: u32,
pub l3_peers: Vec<SocketAddr>,
pub oltp_cache_ttl: Duration,
pub olap_cache_ttl: Duration,
pub vector_cache_ttl: Duration,
pub ai_agent_cache_ttl: Duration,
pub rag_cache_ttl: Duration,
pub default_cache_ttl: Duration,
pub prefetch_enabled: bool,
pub prefetch_lookahead: u32,
pub prefetch_confidence_threshold: f32,
pub max_prefetch_queue: usize,
pub invalidation_mode: InvalidationMode,
pub wal_endpoint: Option<String>,
pub wal_lag_tolerance: Duration,
pub scheduling_policy: SchedulingPolicy,
pub oltp_priority: f64,
pub olap_priority: f64,
pub vector_priority: f64,
pub ai_agent_priority: f64,
pub max_concurrent_oltp: u32,
pub max_concurrent_olap: u32,
pub max_concurrent_vector: u32,
pub max_concurrent_ai: u32,
pub heatmap_enabled: bool,
pub heatmap_bucket_size: Duration,
pub heatmap_retention: Duration,
pub max_conversation_turns: usize,
pub semantic_similarity_threshold: f32,
}
impl Default for DistribCacheConfig {
fn default() -> Self {
Self {
enabled: true,
l1_size_mb: 256,
l1_max_entry_size: 1024 * 1024, l1_eviction_policy: EvictionPolicy::LFU,
l2_enabled: true,
l2_size_gb: 5,
l2_path: PathBuf::from("/var/lib/heliosproxy/cache"),
l2_compression: CompressionType::Lz4,
l3_enabled: false,
l3_replication_factor: 2,
l3_peers: Vec::new(),
oltp_cache_ttl: Duration::from_secs(60),
olap_cache_ttl: Duration::from_secs(30 * 60),
vector_cache_ttl: Duration::from_secs(5 * 60),
ai_agent_cache_ttl: Duration::from_secs(10 * 60),
rag_cache_ttl: Duration::from_secs(15 * 60),
default_cache_ttl: Duration::from_secs(5 * 60),
prefetch_enabled: true,
prefetch_lookahead: 3,
prefetch_confidence_threshold: 0.3,
max_prefetch_queue: 100,
invalidation_mode: InvalidationMode::Hybrid,
wal_endpoint: None,
wal_lag_tolerance: Duration::from_millis(100),
scheduling_policy: SchedulingPolicy::WeightedFair,
oltp_priority: 1.0,
olap_priority: 0.3,
vector_priority: 0.5,
ai_agent_priority: 0.7,
max_concurrent_oltp: 500,
max_concurrent_olap: 50,
max_concurrent_vector: 100,
max_concurrent_ai: 200,
heatmap_enabled: true,
heatmap_bucket_size: Duration::from_secs(5 * 60),
heatmap_retention: Duration::from_secs(7 * 24 * 60 * 60),
max_conversation_turns: 50,
semantic_similarity_threshold: 0.85,
}
}
}
impl DistribCacheConfig {
pub fn builder() -> DistribCacheConfigBuilder {
DistribCacheConfigBuilder::default()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InvalidationMode {
TTL,
WAL,
Hybrid,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SchedulingPolicy {
StrictPriority,
WeightedFair,
TimeBased,
Adaptive,
}
#[derive(Debug, Default)]
pub struct DistribCacheConfigBuilder {
config: DistribCacheConfig,
}
impl DistribCacheConfigBuilder {
pub fn enabled(mut self, enabled: bool) -> Self {
self.config.enabled = enabled;
self
}
pub fn l1_size_mb(mut self, size: usize) -> Self {
self.config.l1_size_mb = size;
self
}
pub fn l1_max_entry_size(mut self, size: usize) -> Self {
self.config.l1_max_entry_size = size;
self
}
pub fn l1_eviction_policy(mut self, policy: EvictionPolicy) -> Self {
self.config.l1_eviction_policy = policy;
self
}
pub fn l2_enabled(mut self, enabled: bool) -> Self {
self.config.l2_enabled = enabled;
self
}
pub fn l2_size_gb(mut self, size: u64) -> Self {
self.config.l2_size_gb = size;
self
}
pub fn l2_path(mut self, path: impl Into<PathBuf>) -> Self {
self.config.l2_path = path.into();
self
}
pub fn l2_compression(mut self, compression: CompressionType) -> Self {
self.config.l2_compression = compression;
self
}
pub fn l3_enabled(mut self, enabled: bool) -> Self {
self.config.l3_enabled = enabled;
self
}
pub fn l3_replication_factor(mut self, factor: u32) -> Self {
self.config.l3_replication_factor = factor;
self
}
pub fn l3_peers(mut self, peers: Vec<SocketAddr>) -> Self {
self.config.l3_peers = peers;
self
}
pub fn oltp_cache_ttl(mut self, ttl: Duration) -> Self {
self.config.oltp_cache_ttl = ttl;
self
}
pub fn olap_cache_ttl(mut self, ttl: Duration) -> Self {
self.config.olap_cache_ttl = ttl;
self
}
pub fn vector_cache_ttl(mut self, ttl: Duration) -> Self {
self.config.vector_cache_ttl = ttl;
self
}
pub fn ai_agent_cache_ttl(mut self, ttl: Duration) -> Self {
self.config.ai_agent_cache_ttl = ttl;
self
}
pub fn rag_cache_ttl(mut self, ttl: Duration) -> Self {
self.config.rag_cache_ttl = ttl;
self
}
pub fn prefetch_enabled(mut self, enabled: bool) -> Self {
self.config.prefetch_enabled = enabled;
self
}
pub fn prefetch_lookahead(mut self, lookahead: u32) -> Self {
self.config.prefetch_lookahead = lookahead;
self
}
pub fn prefetch_confidence_threshold(mut self, threshold: f32) -> Self {
self.config.prefetch_confidence_threshold = threshold;
self
}
pub fn invalidation_mode(mut self, mode: InvalidationMode) -> Self {
self.config.invalidation_mode = mode;
self
}
pub fn wal_endpoint(mut self, endpoint: impl Into<String>) -> Self {
self.config.wal_endpoint = Some(endpoint.into());
self
}
pub fn scheduling_policy(mut self, policy: SchedulingPolicy) -> Self {
self.config.scheduling_policy = policy;
self
}
pub fn heatmap_enabled(mut self, enabled: bool) -> Self {
self.config.heatmap_enabled = enabled;
self
}
pub fn max_conversation_turns(mut self, turns: usize) -> Self {
self.config.max_conversation_turns = turns;
self
}
pub fn semantic_similarity_threshold(mut self, threshold: f32) -> Self {
self.config.semantic_similarity_threshold = threshold;
self
}
pub fn build(self) -> DistribCacheConfig {
self.config
}
}
#[derive(Debug, Clone)]
pub struct WorkloadLimits {
pub max_concurrent: u32,
pub max_cache_mb: usize,
pub priority_weight: f64,
pub cache_ttl: Duration,
}
impl Default for WorkloadLimits {
fn default() -> Self {
Self {
max_concurrent: 100,
max_cache_mb: 64,
priority_weight: 0.5,
cache_ttl: Duration::from_secs(300),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_config() {
let config = DistribCacheConfig::default();
assert!(config.enabled);
assert_eq!(config.l1_size_mb, 256);
assert!(config.l2_enabled);
assert!(!config.l3_enabled);
assert!(config.prefetch_enabled);
assert!(config.heatmap_enabled);
}
#[test]
fn test_config_builder() {
let config = DistribCacheConfig::builder()
.l1_size_mb(512)
.l2_enabled(false)
.l3_enabled(true)
.l3_replication_factor(3)
.prefetch_enabled(false)
.build();
assert_eq!(config.l1_size_mb, 512);
assert!(!config.l2_enabled);
assert!(config.l3_enabled);
assert_eq!(config.l3_replication_factor, 3);
assert!(!config.prefetch_enabled);
}
#[test]
fn test_workload_limits_default() {
let limits = WorkloadLimits::default();
assert_eq!(limits.max_concurrent, 100);
assert_eq!(limits.priority_weight, 0.5);
}
}