oxify_authz/
cache.rs

1//! Caching layer for authorization decisions
2//!
3//! Implements a multi-level cache to minimize database queries:
4//! 1. In-memory cache (moka) for hot paths
5//! 2. Optional Redis cache for distributed deployments
6
7use std::time::Duration;
8
9/// Cache statistics for monitoring
10#[derive(Debug, Clone, Default)]
11pub struct CacheStats {
12    pub hits: u64,
13    pub misses: u64,
14    pub evictions: u64,
15}
16
17impl CacheStats {
18    pub fn hit_rate(&self) -> f64 {
19        if self.hits + self.misses == 0 {
20            0.0
21        } else {
22            self.hits as f64 / (self.hits + self.misses) as f64
23        }
24    }
25}
26
27/// Cache invalidation strategy
28#[derive(Debug, Clone)]
29pub enum InvalidationStrategy {
30    /// Invalidate immediately
31    Immediate,
32
33    /// Invalidate after a delay (eventual consistency)
34    Delayed(Duration),
35
36    /// Invalidate specific patterns
37    Pattern(String),
38}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43
44    #[test]
45    fn test_cache_stats() {
46        let stats = CacheStats {
47            hits: 80,
48            misses: 20,
49            ..Default::default()
50        };
51
52        assert_eq!(stats.hit_rate(), 0.8);
53    }
54}