Skip to main content

cortexai_cache/
config.rs

1//! Cache configuration
2
3use std::time::Duration;
4
5/// Configuration for cache behavior
6#[derive(Clone, Debug)]
7pub struct CacheConfig {
8    /// Maximum number of entries (for memory cache)
9    pub max_entries: usize,
10    /// Time-to-live for cache entries
11    pub ttl: Duration,
12    /// Prefix for cache keys (for Redis/Valkey)
13    pub key_prefix: String,
14    /// Whether to enable semantic matching (requires embeddings)
15    pub semantic_matching: bool,
16    /// Similarity threshold for semantic matching (0.0 to 1.0)
17    pub similarity_threshold: f32,
18}
19
20impl Default for CacheConfig {
21    fn default() -> Self {
22        Self {
23            max_entries: 1000,
24            ttl: Duration::from_secs(3600), // 1 hour
25            key_prefix: "ai-agents:cache:".to_string(),
26            semantic_matching: false,
27            similarity_threshold: 0.92,
28        }
29    }
30}
31
32impl CacheConfig {
33    /// Create a new config with custom settings
34    pub fn new() -> Self {
35        Self::default()
36    }
37
38    /// Set maximum entries
39    pub fn max_entries(mut self, max: usize) -> Self {
40        self.max_entries = max;
41        self
42    }
43
44    /// Set TTL
45    pub fn ttl(mut self, ttl: Duration) -> Self {
46        self.ttl = ttl;
47        self
48    }
49
50    /// Set key prefix
51    pub fn key_prefix(mut self, prefix: impl Into<String>) -> Self {
52        self.key_prefix = prefix.into();
53        self
54    }
55
56    /// Enable semantic matching
57    pub fn with_semantic_matching(mut self, threshold: f32) -> Self {
58        self.semantic_matching = true;
59        self.similarity_threshold = threshold.clamp(0.0, 1.0);
60        self
61    }
62
63    /// Config for short-lived cache (5 minutes)
64    pub fn short_lived() -> Self {
65        Self {
66            ttl: Duration::from_secs(300),
67            ..Default::default()
68        }
69    }
70
71    /// Config for long-lived cache (24 hours)
72    pub fn long_lived() -> Self {
73        Self {
74            ttl: Duration::from_secs(86400),
75            max_entries: 5000,
76            ..Default::default()
77        }
78    }
79
80    /// Config for session cache (2 hours)
81    pub fn session() -> Self {
82        Self {
83            ttl: Duration::from_secs(7200),
84            max_entries: 500,
85            key_prefix: "ai-agents:session:".to_string(),
86            ..Default::default()
87        }
88    }
89
90    /// Config for agent responses
91    pub fn for_agents() -> Self {
92        Self {
93            ttl: Duration::from_secs(3600),
94            max_entries: 2000,
95            key_prefix: "ai-agents:responses:".to_string(),
96            ..Default::default()
97        }
98    }
99}