multi_tier_cache/
cache_manager.rs

1//! Cache Manager - Unified Cache Operations
2//!
3//! Manages operations across L1 (Moka) and L2 (Redis) caches with intelligent fallback.
4
5use std::sync::Arc;
6use std::time::Duration;
7use std::future::Future;
8use anyhow::Result;
9use serde_json;
10use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
11use dashmap::DashMap;
12use tokio::sync::Mutex;
13
14use super::l1_cache::L1Cache;
15use super::l2_cache::L2Cache;
16use super::traits::{CacheBackend, L2CacheBackend, StreamingBackend};
17use super::invalidation::{
18    InvalidationConfig, InvalidationPublisher, InvalidationSubscriber,
19    InvalidationMessage, AtomicInvalidationStats, InvalidationStats,
20};
21
22/// RAII cleanup guard for in-flight request tracking
23/// Ensures that entries are removed from DashMap even on early return or panic
24struct CleanupGuard<'a> {
25    map: &'a DashMap<String, Arc<Mutex<()>>>,
26    key: String,
27}
28
29impl<'a> Drop for CleanupGuard<'a> {
30    fn drop(&mut self) {
31        self.map.remove(&self.key);
32    }
33}
34
35/// Cache strategies for different data types
36#[derive(Debug, Clone)]
37#[allow(dead_code)]
38pub enum CacheStrategy {
39    /// Real-time data - 10 seconds TTL
40    RealTime,
41    /// Short-term data - 5 minutes TTL  
42    ShortTerm,
43    /// Medium-term data - 1 hour TTL
44    MediumTerm,
45    /// Long-term data - 3 hours TTL
46    LongTerm,
47    /// Custom TTL
48    Custom(Duration),
49    /// Default strategy (5 minutes)
50    Default,
51}
52
53impl CacheStrategy {
54    /// Convert strategy to duration
55    pub fn to_duration(&self) -> Duration {
56        match self {
57            Self::RealTime => Duration::from_secs(10),
58            Self::ShortTerm => Duration::from_secs(300), // 5 minutes
59            Self::MediumTerm => Duration::from_secs(3600), // 1 hour
60            Self::LongTerm => Duration::from_secs(10800), // 3 hours
61            Self::Custom(duration) => *duration,
62            Self::Default => Duration::from_secs(300),
63        }
64    }
65}
66
67/// Statistics for a single cache tier
68#[derive(Debug, Clone)]
69pub struct TierStats {
70    /// Tier level (1 = L1, 2 = L2, 3 = L3, etc.)
71    pub tier_level: usize,
72    /// Number of cache hits at this tier
73    pub hits: Arc<AtomicU64>,
74    /// Backend name for identification
75    pub backend_name: String,
76}
77
78impl TierStats {
79    fn new(tier_level: usize, backend_name: String) -> Self {
80        Self {
81            tier_level,
82            hits: Arc::new(AtomicU64::new(0)),
83            backend_name,
84        }
85    }
86
87    /// Get current hit count
88    pub fn hit_count(&self) -> u64 {
89        self.hits.load(Ordering::Relaxed)
90    }
91}
92
93/// A single cache tier in the multi-tier architecture
94pub struct CacheTier {
95    /// Cache backend for this tier
96    backend: Arc<dyn L2CacheBackend>,
97    /// Tier level (1 = hottest/fastest, higher = colder/slower)
98    tier_level: usize,
99    /// Enable automatic promotion to upper tiers on cache hit
100    promotion_enabled: bool,
101    /// TTL scale factor (multiplier for TTL when storing/promoting)
102    ttl_scale: f64,
103    /// Statistics for this tier
104    stats: TierStats,
105}
106
107impl CacheTier {
108    /// Create a new cache tier
109    pub fn new(
110        backend: Arc<dyn L2CacheBackend>,
111        tier_level: usize,
112        promotion_enabled: bool,
113        ttl_scale: f64,
114    ) -> Self {
115        let backend_name = backend.name().to_string();
116        Self {
117            backend,
118            tier_level,
119            promotion_enabled,
120            ttl_scale,
121            stats: TierStats::new(tier_level, backend_name),
122        }
123    }
124
125    /// Get value with TTL from this tier
126    async fn get_with_ttl(&self, key: &str) -> Option<(serde_json::Value, Option<Duration>)> {
127        self.backend.get_with_ttl(key).await
128    }
129
130    /// Set value with TTL in this tier
131    async fn set_with_ttl(&self, key: &str, value: serde_json::Value, ttl: Duration) -> Result<()> {
132        let scaled_ttl = Duration::from_secs_f64(ttl.as_secs_f64() * self.ttl_scale);
133        self.backend.set_with_ttl(key, value, scaled_ttl).await
134    }
135
136    /// Remove value from this tier
137    async fn remove(&self, key: &str) -> Result<()> {
138        self.backend.remove(key).await
139    }
140
141    /// Record a cache hit for this tier
142    fn record_hit(&self) {
143        self.stats.hits.fetch_add(1, Ordering::Relaxed);
144    }
145}
146
147/// Configuration for a cache tier (used in builder pattern)
148#[derive(Debug, Clone)]
149pub struct TierConfig {
150    /// Tier level (1, 2, 3, 4...)
151    pub tier_level: usize,
152    /// Enable promotion to upper tiers on hit
153    pub promotion_enabled: bool,
154    /// TTL scale factor (1.0 = same as base TTL)
155    pub ttl_scale: f64,
156}
157
158impl TierConfig {
159    /// Create new tier configuration
160    pub fn new(tier_level: usize) -> Self {
161        Self {
162            tier_level,
163            promotion_enabled: true,
164            ttl_scale: 1.0,
165        }
166    }
167
168    /// Configure as L1 (hot tier)
169    pub fn as_l1() -> Self {
170        Self {
171            tier_level: 1,
172            promotion_enabled: false, // L1 is already top tier
173            ttl_scale: 1.0,
174        }
175    }
176
177    /// Configure as L2 (warm tier)
178    pub fn as_l2() -> Self {
179        Self {
180            tier_level: 2,
181            promotion_enabled: true,
182            ttl_scale: 1.0,
183        }
184    }
185
186    /// Configure as L3 (cold tier) with longer TTL
187    pub fn as_l3() -> Self {
188        Self {
189            tier_level: 3,
190            promotion_enabled: true,
191            ttl_scale: 2.0, // Keep data 2x longer
192        }
193    }
194
195    /// Configure as L4 (archive tier) with much longer TTL
196    pub fn as_l4() -> Self {
197        Self {
198            tier_level: 4,
199            promotion_enabled: true,
200            ttl_scale: 8.0, // Keep data 8x longer
201        }
202    }
203
204    /// Set promotion enabled
205    pub fn with_promotion(mut self, enabled: bool) -> Self {
206        self.promotion_enabled = enabled;
207        self
208    }
209
210    /// Set TTL scale factor
211    pub fn with_ttl_scale(mut self, scale: f64) -> Self {
212        self.ttl_scale = scale;
213        self
214    }
215
216    /// Set tier level
217    pub fn with_level(mut self, level: usize) -> Self {
218        self.tier_level = level;
219        self
220    }
221}
222
223/// Proxy wrapper to convert L2CacheBackend to CacheBackend
224/// (Rust doesn't support automatic trait upcasting for trait objects)
225struct ProxyCacheBackend {
226    backend: Arc<dyn L2CacheBackend>,
227}
228
229#[async_trait::async_trait]
230impl CacheBackend for ProxyCacheBackend {
231    async fn get(&self, key: &str) -> Option<serde_json::Value> {
232        self.backend.get(key).await
233    }
234
235    async fn set_with_ttl(&self, key: &str, value: serde_json::Value, ttl: Duration) -> Result<()> {
236        self.backend.set_with_ttl(key, value, ttl).await
237    }
238
239    async fn remove(&self, key: &str) -> Result<()> {
240        self.backend.remove(key).await
241    }
242
243    async fn health_check(&self) -> bool {
244        self.backend.health_check().await
245    }
246
247    fn name(&self) -> &str {
248        self.backend.name()
249    }
250}
251
252/// Cache Manager - Unified operations across multiple cache tiers
253///
254/// Supports both legacy 2-tier (L1+L2) and new multi-tier (L1+L2+L3+L4+...) architectures.
255/// When `tiers` is Some, it uses the dynamic multi-tier system. Otherwise, falls back to
256/// legacy L1+L2 behavior for backward compatibility.
257pub struct CacheManager {
258    /// Dynamic multi-tier cache architecture (v0.5.0+)
259    /// If Some, this takes precedence over l1_cache/l2_cache fields
260    tiers: Option<Vec<CacheTier>>,
261
262    // ===== Legacy fields (v0.1.0 - v0.4.x) =====
263    // Maintained for backward compatibility
264    /// L1 Cache (trait object for pluggable backends)
265    l1_cache: Arc<dyn CacheBackend>,
266    /// L2 Cache (trait object for pluggable backends)
267    l2_cache: Arc<dyn L2CacheBackend>,
268    /// L2 Cache concrete instance (for invalidation scan_keys)
269    l2_cache_concrete: Option<Arc<L2Cache>>,
270
271    /// Optional streaming backend (defaults to L2 if it implements StreamingBackend)
272    streaming_backend: Option<Arc<dyn StreamingBackend>>,
273    /// Statistics
274    total_requests: Arc<AtomicU64>,
275    l1_hits: Arc<AtomicU64>,
276    l2_hits: Arc<AtomicU64>,
277    misses: Arc<AtomicU64>,
278    promotions: Arc<AtomicUsize>,
279    /// In-flight requests to prevent Cache Stampede on L2/compute operations
280    in_flight_requests: Arc<DashMap<String, Arc<Mutex<()>>>>,
281    /// Invalidation publisher (for broadcasting invalidation messages)
282    invalidation_publisher: Option<Arc<Mutex<InvalidationPublisher>>>,
283    /// Invalidation subscriber (for receiving invalidation messages)
284    invalidation_subscriber: Option<Arc<InvalidationSubscriber>>,
285    /// Invalidation statistics
286    invalidation_stats: Arc<AtomicInvalidationStats>,
287}
288
289impl CacheManager {
290    /// Create new cache manager with trait objects (pluggable backends)
291    ///
292    /// This is the primary constructor for v0.3.0+, supporting custom cache backends.
293    ///
294    /// # Arguments
295    ///
296    /// * `l1_cache` - Any L1 cache backend implementing `CacheBackend` trait
297    /// * `l2_cache` - Any L2 cache backend implementing `L2CacheBackend` trait
298    /// * `streaming_backend` - Optional streaming backend (None to disable streaming)
299    ///
300    /// # Example
301    ///
302    /// ```rust,ignore
303    /// use multi_tier_cache::{CacheManager, L1Cache, L2Cache};
304    /// use std::sync::Arc;
305    ///
306    /// let l1: Arc<dyn CacheBackend> = Arc::new(L1Cache::new().await?);
307    /// let l2: Arc<dyn L2CacheBackend> = Arc::new(L2Cache::new().await?);
308    ///
309    /// let manager = CacheManager::new_with_backends(l1, l2, None).await?;
310    /// ```
311    pub async fn new_with_backends(
312        l1_cache: Arc<dyn CacheBackend>,
313        l2_cache: Arc<dyn L2CacheBackend>,
314        streaming_backend: Option<Arc<dyn StreamingBackend>>,
315    ) -> Result<Self> {
316        println!("  🎯 Initializing Cache Manager with custom backends...");
317        println!("    L1: {}", l1_cache.name());
318        println!("    L2: {}", l2_cache.name());
319        if streaming_backend.is_some() {
320            println!("    Streaming: enabled");
321        } else {
322            println!("    Streaming: disabled");
323        }
324
325        Ok(Self {
326            tiers: None, // Legacy mode: use l1_cache/l2_cache fields
327            l1_cache,
328            l2_cache,
329            l2_cache_concrete: None,
330            streaming_backend,
331            total_requests: Arc::new(AtomicU64::new(0)),
332            l1_hits: Arc::new(AtomicU64::new(0)),
333            l2_hits: Arc::new(AtomicU64::new(0)),
334            misses: Arc::new(AtomicU64::new(0)),
335            promotions: Arc::new(AtomicUsize::new(0)),
336            in_flight_requests: Arc::new(DashMap::new()),
337            invalidation_publisher: None,
338            invalidation_subscriber: None,
339            invalidation_stats: Arc::new(AtomicInvalidationStats::default()),
340        })
341    }
342
343    /// Create new cache manager with default backends (backward compatible)
344    ///
345    /// This is the legacy constructor maintained for backward compatibility.
346    /// New code should prefer `new_with_backends()` or `CacheSystemBuilder`.
347    ///
348    /// # Arguments
349    ///
350    /// * `l1_cache` - Moka L1 cache instance
351    /// * `l2_cache` - Redis L2 cache instance
352    pub async fn new(l1_cache: Arc<L1Cache>, l2_cache: Arc<L2Cache>) -> Result<Self> {
353        println!("  🎯 Initializing Cache Manager...");
354
355        // Convert concrete types to trait objects
356        let l1_backend: Arc<dyn CacheBackend> = l1_cache.clone();
357        let l2_backend: Arc<dyn L2CacheBackend> = l2_cache.clone();
358        // L2Cache also implements StreamingBackend, so use it for streaming
359        let streaming_backend: Arc<dyn StreamingBackend> = l2_cache;
360
361        Self::new_with_backends(l1_backend, l2_backend, Some(streaming_backend)).await
362    }
363
364    /// Create new cache manager with invalidation support
365    ///
366    /// This constructor enables cross-instance cache invalidation via Redis Pub/Sub.
367    ///
368    /// # Arguments
369    ///
370    /// * `l1_cache` - Moka L1 cache instance
371    /// * `l2_cache` - Redis L2 cache instance
372    /// * `redis_url` - Redis connection URL for Pub/Sub
373    /// * `config` - Invalidation configuration
374    ///
375    /// # Example
376    ///
377    /// ```rust,ignore
378    /// use multi_tier_cache::{CacheManager, L1Cache, L2Cache, InvalidationConfig};
379    ///
380    /// let config = InvalidationConfig {
381    ///     channel: "my_app:cache:invalidate".to_string(),
382    ///     ..Default::default()
383    /// };
384    ///
385    /// let manager = CacheManager::new_with_invalidation(
386    ///     l1, l2, "redis://localhost", config
387    /// ).await?;
388    /// ```
389    pub async fn new_with_invalidation(
390        l1_cache: Arc<L1Cache>,
391        l2_cache: Arc<L2Cache>,
392        redis_url: &str,
393        config: InvalidationConfig,
394    ) -> Result<Self> {
395        println!("  🎯 Initializing Cache Manager with Invalidation...");
396        println!("    Pub/Sub channel: {}", config.channel);
397
398        // Convert concrete types to trait objects
399        let l1_backend: Arc<dyn CacheBackend> = l1_cache.clone();
400        let l2_backend: Arc<dyn L2CacheBackend> = l2_cache.clone();
401        let streaming_backend: Arc<dyn StreamingBackend> = l2_cache.clone();
402
403        // Create publisher
404        let client = redis::Client::open(redis_url)?;
405        let conn_manager = redis::aio::ConnectionManager::new(client).await?;
406        let publisher = InvalidationPublisher::new(conn_manager, config.clone());
407
408        // Create subscriber
409        let subscriber = InvalidationSubscriber::new(redis_url, config.clone())?;
410        let invalidation_stats = Arc::new(AtomicInvalidationStats::default());
411
412        let manager = Self {
413            tiers: None, // Legacy mode: use l1_cache/l2_cache fields
414            l1_cache: l1_backend,
415            l2_cache: l2_backend,
416            l2_cache_concrete: Some(l2_cache),
417            streaming_backend: Some(streaming_backend),
418            total_requests: Arc::new(AtomicU64::new(0)),
419            l1_hits: Arc::new(AtomicU64::new(0)),
420            l2_hits: Arc::new(AtomicU64::new(0)),
421            misses: Arc::new(AtomicU64::new(0)),
422            promotions: Arc::new(AtomicUsize::new(0)),
423            in_flight_requests: Arc::new(DashMap::new()),
424            invalidation_publisher: Some(Arc::new(Mutex::new(publisher))),
425            invalidation_subscriber: Some(Arc::new(subscriber)),
426            invalidation_stats,
427        };
428
429        // Start subscriber with handler
430        manager.start_invalidation_subscriber();
431
432        println!("  ✅ Cache Manager initialized with invalidation support");
433
434        Ok(manager)
435    }
436
437    /// Create new cache manager with multi-tier architecture (v0.5.0+)
438    ///
439    /// This constructor enables dynamic multi-tier caching with 3, 4, or more tiers.
440    /// Tiers are checked in order (lower tier_level = faster/hotter).
441    ///
442    /// # Arguments
443    ///
444    /// * `tiers` - Vector of configured cache tiers (must be sorted by tier_level ascending)
445    /// * `streaming_backend` - Optional streaming backend
446    ///
447    /// # Example
448    ///
449    /// ```rust,ignore
450    /// use multi_tier_cache::{CacheManager, CacheTier, TierConfig, L1Cache, L2Cache};
451    /// use std::sync::Arc;
452    ///
453    /// // L1 + L2 + L3 setup
454    /// let l1 = Arc::new(L1Cache::new().await?);
455    /// let l2 = Arc::new(L2Cache::new().await?);
456    /// let l3 = Arc::new(RocksDBCache::new("/tmp/cache").await?);
457    ///
458    /// let tiers = vec![
459    ///     CacheTier::new(l1, 1, false, 1.0),  // L1 - no promotion
460    ///     CacheTier::new(l2, 2, true, 1.0),   // L2 - promote to L1
461    ///     CacheTier::new(l3, 3, true, 2.0),   // L3 - promote to L2&L1, 2x TTL
462    /// ];
463    ///
464    /// let manager = CacheManager::new_with_tiers(tiers, None).await?;
465    /// ```
466    pub async fn new_with_tiers(
467        tiers: Vec<CacheTier>,
468        streaming_backend: Option<Arc<dyn StreamingBackend>>,
469    ) -> Result<Self> {
470        println!("  🎯 Initializing Multi-Tier Cache Manager...");
471        println!("    Tier count: {}", tiers.len());
472        for tier in &tiers {
473            println!(
474                "    L{}: {} (promotion={}, ttl_scale={})",
475                tier.tier_level,
476                tier.stats.backend_name,
477                tier.promotion_enabled,
478                tier.ttl_scale
479            );
480        }
481
482        // Validate tiers are sorted by level
483        for i in 1..tiers.len() {
484            if tiers[i].tier_level <= tiers[i - 1].tier_level {
485                anyhow::bail!(
486                    "Tiers must be sorted by tier_level ascending (found L{} after L{})",
487                    tiers[i].tier_level,
488                    tiers[i - 1].tier_level
489                );
490            }
491        }
492
493        // For backward compatibility with legacy code, we need dummy l1/l2 caches
494        // Use first tier as l1, second tier as l2 if available
495        let (l1_cache, l2_cache) = if tiers.len() >= 2 {
496            (tiers[0].backend.clone(), tiers[1].backend.clone())
497        } else if tiers.len() == 1 {
498            // Only one tier - use it for both
499            let tier = tiers[0].backend.clone();
500            (tier.clone(), tier)
501        } else {
502            anyhow::bail!("At least one cache tier is required");
503        };
504
505        // Convert to CacheBackend trait for l1 (L2CacheBackend extends CacheBackend)
506        let l1_backend: Arc<dyn CacheBackend> = Arc::new(ProxyCacheBackend {
507            backend: l1_cache.clone(),
508        });
509
510        Ok(Self {
511            tiers: Some(tiers),
512            l1_cache: l1_backend,
513            l2_cache,
514            l2_cache_concrete: None,
515            streaming_backend,
516            total_requests: Arc::new(AtomicU64::new(0)),
517            l1_hits: Arc::new(AtomicU64::new(0)),
518            l2_hits: Arc::new(AtomicU64::new(0)),
519            misses: Arc::new(AtomicU64::new(0)),
520            promotions: Arc::new(AtomicUsize::new(0)),
521            in_flight_requests: Arc::new(DashMap::new()),
522            invalidation_publisher: None,
523            invalidation_subscriber: None,
524            invalidation_stats: Arc::new(AtomicInvalidationStats::default()),
525        })
526    }
527
528    /// Start the invalidation subscriber background task
529    fn start_invalidation_subscriber(&self) {
530        if let Some(subscriber) = &self.invalidation_subscriber {
531            let l1_cache = Arc::clone(&self.l1_cache);
532            let l2_cache_concrete = self.l2_cache_concrete.clone();
533
534            subscriber.start(move |msg| {
535                let l1 = Arc::clone(&l1_cache);
536                let _l2 = l2_cache_concrete.clone();
537
538                async move {
539                    match msg {
540                        InvalidationMessage::Remove { key } => {
541                            // Remove from L1
542                            l1.remove(&key).await?;
543                            println!("🗑️  [Invalidation] Removed '{}' from L1", key);
544                        }
545                        InvalidationMessage::Update { key, value, ttl_secs } => {
546                            // Update L1 with new value
547                            let ttl = ttl_secs
548                                .map(Duration::from_secs)
549                                .unwrap_or_else(|| Duration::from_secs(300));
550                            l1.set_with_ttl(&key, value, ttl).await?;
551                            println!("🔄 [Invalidation] Updated '{}' in L1", key);
552                        }
553                        InvalidationMessage::RemovePattern { pattern } => {
554                            // For pattern-based invalidation, we can't easily iterate L1 cache
555                            // So we just log it. The pattern invalidation is mainly for L2.
556                            // L1 entries will naturally expire via TTL.
557                            println!("🔍 [Invalidation] Pattern '{}' invalidated (L1 will expire naturally)", pattern);
558                        }
559                        InvalidationMessage::RemoveBulk { keys } => {
560                            // Remove multiple keys from L1
561                            for key in keys {
562                                if let Err(e) = l1.remove(&key).await {
563                                    eprintln!("⚠️  Failed to remove '{}' from L1: {}", key, e);
564                                }
565                            }
566                            println!("🗑️  [Invalidation] Bulk removed keys from L1");
567                        }
568                    }
569                    Ok(())
570                }
571            });
572
573            println!("📡 Invalidation subscriber started");
574        }
575    }
576
577    /// Get value from cache using multi-tier architecture (v0.5.0+)
578    ///
579    /// This method iterates through all configured tiers and automatically promotes
580    /// to upper tiers on cache hit.
581    async fn get_multi_tier(&self, key: &str) -> Result<Option<serde_json::Value>> {
582        let tiers = self.tiers.as_ref().unwrap(); // Safe: only called when tiers is Some
583
584        // Try each tier sequentially (sorted by tier_level)
585        for (tier_index, tier) in tiers.iter().enumerate() {
586            if let Some((value, ttl)) = tier.get_with_ttl(key).await {
587                // Cache hit!
588                tier.record_hit();
589
590                // Promote to all upper tiers (if promotion enabled)
591                if tier.promotion_enabled && tier_index > 0 {
592                    let promotion_ttl = ttl.unwrap_or_else(|| CacheStrategy::Default.to_duration());
593
594                    // Promote to all tiers above this one
595                    for upper_tier in tiers[..tier_index].iter().rev() {
596                        if let Err(e) = upper_tier.set_with_ttl(key, value.clone(), promotion_ttl).await {
597                            eprintln!(
598                                "⚠️  Failed to promote '{}' from L{} to L{}: {}",
599                                key, tier.tier_level, upper_tier.tier_level, e
600                            );
601                        } else {
602                            self.promotions.fetch_add(1, Ordering::Relaxed);
603                            println!(
604                                "⬆️  Promoted '{}' from L{} to L{} (TTL: {:?})",
605                                key, tier.tier_level, upper_tier.tier_level, promotion_ttl
606                            );
607                        }
608                    }
609                }
610
611                return Ok(Some(value));
612            }
613        }
614
615        // Cache miss across all tiers
616        Ok(None)
617    }
618
619    /// Get value from cache (L1 first, then L2 fallback with promotion)
620    ///
621    /// This method now includes built-in Cache Stampede protection when cache misses occur.
622    /// Multiple concurrent requests for the same missing key will be coalesced to prevent
623    /// unnecessary duplicate work on external data sources.
624    ///
625    /// Supports both legacy 2-tier mode and new multi-tier mode (v0.5.0+).
626    ///
627    /// # Arguments
628    /// * `key` - Cache key to retrieve
629    ///
630    /// # Returns
631    /// * `Ok(Some(value))` - Cache hit, value found in any tier
632    /// * `Ok(None)` - Cache miss, value not found in any cache
633    /// * `Err(error)` - Cache operation failed
634    pub async fn get(&self, key: &str) -> Result<Option<serde_json::Value>> {
635        self.total_requests.fetch_add(1, Ordering::Relaxed);
636
637        // NEW: Multi-tier mode (v0.5.0+)
638        if self.tiers.is_some() {
639            // Fast path for L1 (first tier) - no locking needed
640            if let Some(tier1) = self.tiers.as_ref().unwrap().first() {
641                if let Some((value, _ttl)) = tier1.get_with_ttl(key).await {
642                    tier1.record_hit();
643                    // Update legacy stats for backward compatibility
644                    self.l1_hits.fetch_add(1, Ordering::Relaxed);
645                    return Ok(Some(value));
646                }
647            }
648
649            // L1 miss - use stampede protection for lower tiers
650            let key_owned = key.to_string();
651            let lock_guard = self.in_flight_requests
652                .entry(key_owned.clone())
653                .or_insert_with(|| Arc::new(Mutex::new(())))
654                .clone();
655
656            let _guard = lock_guard.lock().await;
657            let cleanup_guard = CleanupGuard {
658                map: &self.in_flight_requests,
659                key: key_owned.clone(),
660            };
661
662            // Double-check L1 after acquiring lock
663            if let Some(tier1) = self.tiers.as_ref().unwrap().first() {
664                if let Some((value, _ttl)) = tier1.get_with_ttl(key).await {
665                    tier1.record_hit();
666                    self.l1_hits.fetch_add(1, Ordering::Relaxed);
667                    return Ok(Some(value));
668                }
669            }
670
671            // Check remaining tiers with promotion
672            let result = self.get_multi_tier(key).await?;
673
674            if result.is_some() {
675                // Hit in L2+ tier - update legacy stats
676                if self.tiers.as_ref().unwrap().len() >= 2 {
677                    self.l2_hits.fetch_add(1, Ordering::Relaxed);
678                }
679            } else {
680                self.misses.fetch_add(1, Ordering::Relaxed);
681            }
682
683            drop(cleanup_guard);
684            return Ok(result);
685        }
686
687        // LEGACY: 2-tier mode (L1 + L2)
688        // Fast path: Try L1 first (no locking needed)
689        if let Some(value) = self.l1_cache.get(key).await {
690            self.l1_hits.fetch_add(1, Ordering::Relaxed);
691            return Ok(Some(value));
692        }
693        
694        // L1 miss - implement Cache Stampede protection for L2 lookup
695        let key_owned = key.to_string();
696        let lock_guard = self.in_flight_requests
697            .entry(key_owned.clone())
698            .or_insert_with(|| Arc::new(Mutex::new(())))
699            .clone();
700        
701        let _guard = lock_guard.lock().await;
702        
703        // RAII cleanup guard - ensures entry is removed even on early return or panic
704        let cleanup_guard = CleanupGuard {
705            map: &self.in_flight_requests,
706            key: key_owned.clone(),
707        };
708        
709        // Double-check L1 cache after acquiring lock
710        // (Another concurrent request might have populated it while we were waiting)
711        if let Some(value) = self.l1_cache.get(key).await {
712            self.l1_hits.fetch_add(1, Ordering::Relaxed);
713            // cleanup_guard will auto-remove entry on drop
714            return Ok(Some(value));
715        }
716        
717        // Check L2 cache with TTL information
718        if let Some((value, ttl)) = self.l2_cache.get_with_ttl(key).await {
719            self.l2_hits.fetch_add(1, Ordering::Relaxed);
720
721            // Promote to L1 with same TTL as Redis (or default if no TTL)
722            let promotion_ttl = ttl.unwrap_or_else(|| CacheStrategy::Default.to_duration());
723
724            if let Err(_) = self.l1_cache.set_with_ttl(key, value.clone(), promotion_ttl).await {
725                // L1 promotion failed, but we still have the data
726                eprintln!("⚠️ Failed to promote key '{}' to L1 cache", key);
727            } else {
728                self.promotions.fetch_add(1, Ordering::Relaxed);
729                println!("⬆️ Promoted '{}' from L2 to L1 with TTL {:?} (via get)", key, promotion_ttl);
730            }
731
732            // cleanup_guard will auto-remove entry on drop
733            return Ok(Some(value));
734        }
735        
736        // Both L1 and L2 miss
737        self.misses.fetch_add(1, Ordering::Relaxed);
738        
739        // cleanup_guard will auto-remove entry on drop
740        drop(cleanup_guard);
741        
742        Ok(None)
743    }
744    
745    /// Get value from cache with fallback computation (enhanced backward compatibility)
746    /// 
747    /// This is a convenience method that combines `get()` with optional computation.
748    /// If the value is not found in cache, it will execute the compute function
749    /// and cache the result automatically.
750    /// 
751    /// # Arguments
752    /// * `key` - Cache key
753    /// * `compute_fn` - Optional function to compute value if not in cache
754    /// * `strategy` - Cache strategy for storing computed value (default: ShortTerm)
755    /// 
756    /// # Returns
757    /// * `Ok(Some(value))` - Value found in cache or computed successfully
758    /// * `Ok(None)` - Value not in cache and no compute function provided
759    /// * `Err(error)` - Cache operation or computation failed
760    /// 
761    /// # Example
762    /// ```ignore
763    /// // Simple cache get (existing behavior)
764    /// let cached_data = cache_manager.get_with_fallback("my_key", None, None).await?;
765    ///
766    /// // Get with computation fallback (new enhanced behavior)
767    /// let api_data = cache_manager.get_with_fallback(
768    ///     "api_response",
769    ///     Some(|| async { fetch_data_from_api().await }),
770    ///     Some(CacheStrategy::RealTime)
771    /// ).await?;
772    /// ```
773    
774    /// Set value with specific cache strategy (all tiers)
775    ///
776    /// Supports both legacy 2-tier mode and new multi-tier mode (v0.5.0+).
777    /// In multi-tier mode, stores to ALL tiers with their respective TTL scaling.
778    pub async fn set_with_strategy(&self, key: &str, value: serde_json::Value, strategy: CacheStrategy) -> Result<()> {
779        let ttl = strategy.to_duration();
780
781        // NEW: Multi-tier mode (v0.5.0+)
782        if let Some(tiers) = &self.tiers {
783            // Store in ALL tiers with their respective TTL scaling
784            let mut success_count = 0;
785            let mut last_error = None;
786
787            for tier in tiers {
788                match tier.set_with_ttl(key, value.clone(), ttl).await {
789                    Ok(_) => {
790                        success_count += 1;
791                    }
792                    Err(e) => {
793                        eprintln!("⚠️ L{} cache set failed for key '{}': {}", tier.tier_level, key, e);
794                        last_error = Some(e);
795                    }
796                }
797            }
798
799            if success_count > 0 {
800                println!("💾 [Multi-Tier] Cached '{}' in {}/{} tiers (base TTL: {:?})",
801                         key, success_count, tiers.len(), ttl);
802                return Ok(());
803            } else {
804                return Err(last_error.unwrap_or_else(|| anyhow::anyhow!("All tiers failed for key '{}'", key)));
805            }
806        }
807
808        // LEGACY: 2-tier mode (L1 + L2)
809        // Store in both L1 and L2
810        let l1_result = self.l1_cache.set_with_ttl(key, value.clone(), ttl).await;
811        let l2_result = self.l2_cache.set_with_ttl(key, value, ttl).await;
812
813        // Return success if at least one cache succeeded
814        match (l1_result, l2_result) {
815            (Ok(_), Ok(_)) => {
816                // Both succeeded
817                println!("💾 [L1+L2] Cached '{}' with TTL {:?}", key, ttl);
818                Ok(())
819            }
820            (Ok(_), Err(_)) => {
821                // L1 succeeded, L2 failed
822                eprintln!("⚠️ L2 cache set failed for key '{}', continuing with L1", key);
823                println!("💾 [L1] Cached '{}' with TTL {:?}", key, ttl);
824                Ok(())
825            }
826            (Err(_), Ok(_)) => {
827                // L1 failed, L2 succeeded
828                eprintln!("⚠️ L1 cache set failed for key '{}', continuing with L2", key);
829                println!("💾 [L2] Cached '{}' with TTL {:?}", key, ttl);
830                Ok(())
831            }
832            (Err(e1), Err(_e2)) => {
833                // Both failed
834                Err(anyhow::anyhow!("Both L1 and L2 cache set failed for key '{}': {}", key, e1))
835            }
836        }
837    }
838    
839    /// Get or compute value with Cache Stampede protection across L1+L2+Compute
840    /// 
841    /// This method provides comprehensive Cache Stampede protection:
842    /// 1. Check L1 cache first (uses Moka's built-in coalescing)
843    /// 2. Check L2 cache with mutex-based coalescing
844    /// 3. Compute fresh data with protection against concurrent computations
845    /// 
846    /// # Arguments
847    /// * `key` - Cache key
848    /// * `strategy` - Cache strategy for TTL and storage behavior
849    /// * `compute_fn` - Async function to compute the value if not in any cache
850    /// 
851    /// # Example
852    /// ```ignore
853    /// let api_data = cache_manager.get_or_compute_with(
854    ///     "api_response",
855    ///     CacheStrategy::RealTime,
856    ///     || async {
857    ///         fetch_data_from_api().await
858    ///     }
859    /// ).await?;
860    /// ```
861    #[allow(dead_code)]
862    pub async fn get_or_compute_with<F, Fut>(
863        &self,
864        key: &str,
865        strategy: CacheStrategy,
866        compute_fn: F,
867    ) -> Result<serde_json::Value>
868    where
869        F: FnOnce() -> Fut + Send,
870        Fut: Future<Output = Result<serde_json::Value>> + Send,
871    {
872        self.total_requests.fetch_add(1, Ordering::Relaxed);
873        
874        // 1. Try L1 cache first (with built-in Moka coalescing for hot data)
875        if let Some(value) = self.l1_cache.get(key).await {
876            self.l1_hits.fetch_add(1, Ordering::Relaxed);
877            return Ok(value);
878        }
879        
880        // 2. L1 miss - try L2 with Cache Stampede protection
881        let key_owned = key.to_string();
882        let lock_guard = self.in_flight_requests
883            .entry(key_owned.clone())
884            .or_insert_with(|| Arc::new(Mutex::new(())))
885            .clone();
886        
887        let _guard = lock_guard.lock().await;
888        
889        // RAII cleanup guard - ensures entry is removed even on early return or panic
890        let _cleanup_guard = CleanupGuard {
891            map: &self.in_flight_requests,
892            key: key_owned,
893        };
894        
895        // 3. Double-check L1 cache after acquiring lock
896        // (Another request might have populated it while we were waiting)
897        if let Some(value) = self.l1_cache.get(key).await {
898            self.l1_hits.fetch_add(1, Ordering::Relaxed);
899            // _cleanup_guard will auto-remove entry on drop
900            return Ok(value);
901        }
902        
903        // 4. Check L2 cache with TTL
904        if let Some((value, redis_ttl)) = self.l2_cache.get_with_ttl(key).await {
905            self.l2_hits.fetch_add(1, Ordering::Relaxed);
906
907            // Promote to L1 using Redis TTL (or strategy TTL as fallback)
908            let promotion_ttl = redis_ttl.unwrap_or_else(|| strategy.to_duration());
909
910            if let Err(e) = self.l1_cache.set_with_ttl(key, value.clone(), promotion_ttl).await {
911                eprintln!("⚠️ Failed to promote key '{}' to L1: {}", key, e);
912            } else {
913                self.promotions.fetch_add(1, Ordering::Relaxed);
914                println!("⬆️ Promoted '{}' from L2 to L1 with TTL {:?}", key, promotion_ttl);
915            }
916
917            // _cleanup_guard will auto-remove entry on drop
918            return Ok(value);
919        }
920        
921        // 5. Both L1 and L2 miss - compute fresh data
922        println!("💻 Computing fresh data for key: '{}' (Cache Stampede protected)", key);
923        let fresh_data = compute_fn().await?;
924        
925        // 6. Store in both caches
926        if let Err(e) = self.set_with_strategy(key, fresh_data.clone(), strategy).await {
927            eprintln!("⚠️ Failed to cache computed data for key '{}': {}", key, e);
928        }
929        
930        // 7. _cleanup_guard will auto-remove entry on drop
931
932        Ok(fresh_data)
933    }
934
935    /// Get or compute typed value with Cache Stampede protection (Type-Safe Version)
936    ///
937    /// This method provides the same functionality as `get_or_compute_with()` but with
938    /// **type-safe** automatic serialization/deserialization. Perfect for database queries,
939    /// API calls, or any computation that returns structured data.
940    ///
941    /// # Type Safety
942    ///
943    /// - Returns your actual type `T` instead of `serde_json::Value`
944    /// - Compiler enforces Serialize + DeserializeOwned bounds
945    /// - No manual JSON conversion needed
946    ///
947    /// # Cache Flow
948    ///
949    /// 1. Check L1 cache → deserialize if found
950    /// 2. Check L2 cache → deserialize + promote to L1 if found
951    /// 3. Execute compute_fn → serialize → store in L1+L2
952    /// 4. Full stampede protection (only ONE request computes)
953    ///
954    /// # Arguments
955    ///
956    /// * `key` - Cache key
957    /// * `strategy` - Cache strategy for TTL
958    /// * `compute_fn` - Async function returning `Result<T>`
959    ///
960    /// # Example - Database Query
961    ///
962    /// ```no_run
963    /// # use multi_tier_cache::{CacheManager, CacheStrategy, L1Cache, L2Cache};
964    /// # use std::sync::Arc;
965    /// # use serde::{Serialize, Deserialize};
966    /// # async fn example() -> anyhow::Result<()> {
967    /// # let l1 = Arc::new(L1Cache::new().await?);
968    /// # let l2 = Arc::new(L2Cache::new().await?);
969    /// # let cache_manager = CacheManager::new(l1, l2);
970    ///
971    /// #[derive(Serialize, Deserialize)]
972    /// struct User {
973    ///     id: i64,
974    ///     name: String,
975    /// }
976    ///
977    /// // Type-safe database caching (example - requires sqlx)
978    /// // let user: User = cache_manager.get_or_compute_typed(
979    /// //     "user:123",
980    /// //     CacheStrategy::MediumTerm,
981    /// //     || async {
982    /// //         sqlx::query_as::<_, User>("SELECT * FROM users WHERE id = $1")
983    /// //             .bind(123)
984    /// //             .fetch_one(&pool)
985    /// //             .await
986    /// //     }
987    /// // ).await?;
988    /// # Ok(())
989    /// # }
990    /// ```
991    ///
992    /// # Example - API Call
993    ///
994    /// ```no_run
995    /// # use multi_tier_cache::{CacheManager, CacheStrategy, L1Cache, L2Cache};
996    /// # use std::sync::Arc;
997    /// # use serde::{Serialize, Deserialize};
998    /// # async fn example() -> anyhow::Result<()> {
999    /// # let l1 = Arc::new(L1Cache::new().await?);
1000    /// # let l2 = Arc::new(L2Cache::new().await?);
1001    /// # let cache_manager = CacheManager::new(l1, l2);
1002    /// #[derive(Serialize, Deserialize)]
1003    /// struct ApiResponse {
1004    ///     data: String,
1005    ///     timestamp: i64,
1006    /// }
1007    ///
1008    /// // API call caching (example - requires reqwest)
1009    /// // let response: ApiResponse = cache_manager.get_or_compute_typed(
1010    /// //     "api:endpoint",
1011    /// //     CacheStrategy::RealTime,
1012    /// //     || async {
1013    /// //         reqwest::get("https://api.example.com/data")
1014    /// //             .await?
1015    /// //             .json::<ApiResponse>()
1016    /// //             .await
1017    /// //     }
1018    /// // ).await?;
1019    /// # Ok(())
1020    /// # }
1021    /// ```
1022    ///
1023    /// # Performance
1024    ///
1025    /// - L1 hit: <1ms + deserialization (~10-50μs for small structs)
1026    /// - L2 hit: 2-5ms + deserialization + L1 promotion
1027    /// - Compute: Your function time + serialization + L1+L2 storage
1028    /// - Stampede protection: 99.6% latency reduction under high concurrency
1029    ///
1030    /// # Errors
1031    ///
1032    /// Returns error if:
1033    /// - Compute function fails
1034    /// - Serialization fails (invalid type for JSON)
1035    /// - Deserialization fails (cache data doesn't match type T)
1036    /// - Cache operations fail (Redis connection issues)
1037    pub async fn get_or_compute_typed<T, F, Fut>(
1038        &self,
1039        key: &str,
1040        strategy: CacheStrategy,
1041        compute_fn: F,
1042    ) -> Result<T>
1043    where
1044        T: serde::Serialize + serde::de::DeserializeOwned + Send + 'static,
1045        F: FnOnce() -> Fut + Send,
1046        Fut: Future<Output = Result<T>> + Send,
1047    {
1048        self.total_requests.fetch_add(1, Ordering::Relaxed);
1049
1050        // 1. Try L1 cache first (with built-in Moka coalescing for hot data)
1051        if let Some(cached_json) = self.l1_cache.get(key).await {
1052            self.l1_hits.fetch_add(1, Ordering::Relaxed);
1053
1054            // Attempt to deserialize from JSON to type T
1055            match serde_json::from_value::<T>(cached_json) {
1056                Ok(typed_value) => {
1057                    println!("✅ [L1 HIT] Deserialized '{}' to type {}", key, std::any::type_name::<T>());
1058                    return Ok(typed_value);
1059                }
1060                Err(e) => {
1061                    // Deserialization failed - cache data may be stale or corrupt
1062                    eprintln!("⚠️ L1 cache deserialization failed for key '{}': {}. Will recompute.", key, e);
1063                    // Fall through to recompute
1064                }
1065            }
1066        }
1067
1068        // 2. L1 miss - try L2 with Cache Stampede protection
1069        let key_owned = key.to_string();
1070        let lock_guard = self.in_flight_requests
1071            .entry(key_owned.clone())
1072            .or_insert_with(|| Arc::new(Mutex::new(())))
1073            .clone();
1074
1075        let _guard = lock_guard.lock().await;
1076
1077        // RAII cleanup guard - ensures entry is removed even on early return or panic
1078        let _cleanup_guard = CleanupGuard {
1079            map: &self.in_flight_requests,
1080            key: key_owned,
1081        };
1082
1083        // 3. Double-check L1 cache after acquiring lock
1084        // (Another request might have populated it while we were waiting)
1085        if let Some(cached_json) = self.l1_cache.get(key).await {
1086            self.l1_hits.fetch_add(1, Ordering::Relaxed);
1087            if let Ok(typed_value) = serde_json::from_value::<T>(cached_json) {
1088                println!("✅ [L1 HIT] Deserialized '{}' after lock acquisition", key);
1089                return Ok(typed_value);
1090            }
1091        }
1092
1093        // 4. Check L2 cache with TTL
1094        if let Some((cached_json, redis_ttl)) = self.l2_cache.get_with_ttl(key).await {
1095            self.l2_hits.fetch_add(1, Ordering::Relaxed);
1096
1097            // Attempt to deserialize
1098            match serde_json::from_value::<T>(cached_json.clone()) {
1099                Ok(typed_value) => {
1100                    println!("✅ [L2 HIT] Deserialized '{}' from Redis", key);
1101
1102                    // Promote to L1 using Redis TTL (or strategy TTL as fallback)
1103                    let promotion_ttl = redis_ttl.unwrap_or_else(|| strategy.to_duration());
1104
1105                    if let Err(e) = self.l1_cache.set_with_ttl(key, cached_json, promotion_ttl).await {
1106                        eprintln!("⚠️ Failed to promote key '{}' to L1: {}", key, e);
1107                    } else {
1108                        self.promotions.fetch_add(1, Ordering::Relaxed);
1109                        println!("⬆️ Promoted '{}' from L2 to L1 with TTL {:?}", key, promotion_ttl);
1110                    }
1111
1112                    return Ok(typed_value);
1113                }
1114                Err(e) => {
1115                    eprintln!("⚠️ L2 cache deserialization failed for key '{}': {}. Will recompute.", key, e);
1116                    // Fall through to recompute
1117                }
1118            }
1119        }
1120
1121        // 5. Both L1 and L2 miss (or deserialization failed) - compute fresh data
1122        println!("💻 Computing fresh typed data for key: '{}' (Cache Stampede protected)", key);
1123        let typed_value = compute_fn().await?;
1124
1125        // 6. Serialize to JSON for storage
1126        let json_value = serde_json::to_value(&typed_value)
1127            .map_err(|e| anyhow::anyhow!("Failed to serialize type {} for caching: {}", std::any::type_name::<T>(), e))?;
1128
1129        // 7. Store in both L1 and L2 caches
1130        if let Err(e) = self.set_with_strategy(key, json_value, strategy).await {
1131            eprintln!("⚠️ Failed to cache computed typed data for key '{}': {}", key, e);
1132        } else {
1133            println!("💾 Cached typed value for '{}' (type: {})", key, std::any::type_name::<T>());
1134        }
1135
1136        // 8. _cleanup_guard will auto-remove entry on drop
1137
1138        Ok(typed_value)
1139    }
1140
1141    /// Get comprehensive cache statistics
1142    ///
1143    /// In multi-tier mode, aggregates statistics from all tiers.
1144    /// In legacy mode, returns L1 and L2 stats.
1145    #[allow(dead_code)]
1146    pub fn get_stats(&self) -> CacheManagerStats {
1147        let total_reqs = self.total_requests.load(Ordering::Relaxed);
1148        let l1_hits = self.l1_hits.load(Ordering::Relaxed);
1149        let l2_hits = self.l2_hits.load(Ordering::Relaxed);
1150        let misses = self.misses.load(Ordering::Relaxed);
1151
1152        CacheManagerStats {
1153            total_requests: total_reqs,
1154            l1_hits,
1155            l2_hits,
1156            total_hits: l1_hits + l2_hits,
1157            misses,
1158            hit_rate: if total_reqs > 0 {
1159                ((l1_hits + l2_hits) as f64 / total_reqs as f64) * 100.0
1160            } else { 0.0 },
1161            l1_hit_rate: if total_reqs > 0 {
1162                (l1_hits as f64 / total_reqs as f64) * 100.0
1163            } else { 0.0 },
1164            promotions: self.promotions.load(Ordering::Relaxed),
1165            in_flight_requests: self.in_flight_requests.len(),
1166        }
1167    }
1168
1169    /// Get per-tier statistics (v0.5.0+)
1170    ///
1171    /// Returns statistics for each tier if multi-tier mode is enabled.
1172    /// Returns None if using legacy 2-tier mode.
1173    ///
1174    /// # Example
1175    /// ```rust,ignore
1176    /// if let Some(tier_stats) = cache_manager.get_tier_stats() {
1177    ///     for stats in tier_stats {
1178    ///         println!("L{}: {} hits ({})",
1179    ///                  stats.tier_level,
1180    ///                  stats.hit_count(),
1181    ///                  stats.backend_name);
1182    ///     }
1183    /// }
1184    /// ```
1185    pub fn get_tier_stats(&self) -> Option<Vec<TierStats>> {
1186        self.tiers.as_ref().map(|tiers| {
1187            tiers.iter().map(|tier| tier.stats.clone()).collect()
1188        })
1189    }
1190    
1191    // ===== Redis Streams Methods =====
1192    
1193    /// Publish data to Redis Stream
1194    ///
1195    /// # Arguments
1196    /// * `stream_key` - Name of the stream (e.g., "events_stream")
1197    /// * `fields` - Field-value pairs to publish
1198    /// * `maxlen` - Optional max length for stream trimming
1199    ///
1200    /// # Returns
1201    /// The entry ID generated by Redis
1202    ///
1203    /// # Errors
1204    /// Returns error if streaming backend is not configured
1205    pub async fn publish_to_stream(
1206        &self,
1207        stream_key: &str,
1208        fields: Vec<(String, String)>,
1209        maxlen: Option<usize>
1210    ) -> Result<String> {
1211        match &self.streaming_backend {
1212            Some(backend) => backend.stream_add(stream_key, fields, maxlen).await,
1213            None => Err(anyhow::anyhow!("Streaming backend not configured"))
1214        }
1215    }
1216    
1217    /// Read latest entries from Redis Stream
1218    ///
1219    /// # Arguments
1220    /// * `stream_key` - Name of the stream
1221    /// * `count` - Number of latest entries to retrieve
1222    ///
1223    /// # Returns
1224    /// Vector of (entry_id, fields) tuples (newest first)
1225    ///
1226    /// # Errors
1227    /// Returns error if streaming backend is not configured
1228    pub async fn read_stream_latest(
1229        &self,
1230        stream_key: &str,
1231        count: usize
1232    ) -> Result<Vec<(String, Vec<(String, String)>)>> {
1233        match &self.streaming_backend {
1234            Some(backend) => backend.stream_read_latest(stream_key, count).await,
1235            None => Err(anyhow::anyhow!("Streaming backend not configured"))
1236        }
1237    }
1238    
1239    /// Read from Redis Stream with optional blocking
1240    ///
1241    /// # Arguments
1242    /// * `stream_key` - Name of the stream
1243    /// * `last_id` - Last ID seen ("0" for start, "$" for new only)
1244    /// * `count` - Max entries to retrieve
1245    /// * `block_ms` - Optional blocking timeout in ms
1246    ///
1247    /// # Returns
1248    /// Vector of (entry_id, fields) tuples
1249    ///
1250    /// # Errors
1251    /// Returns error if streaming backend is not configured
1252    pub async fn read_stream(
1253        &self,
1254        stream_key: &str,
1255        last_id: &str,
1256        count: usize,
1257        block_ms: Option<usize>
1258    ) -> Result<Vec<(String, Vec<(String, String)>)>> {
1259        match &self.streaming_backend {
1260            Some(backend) => backend.stream_read(stream_key, last_id, count, block_ms).await,
1261            None => Err(anyhow::anyhow!("Streaming backend not configured"))
1262        }
1263    }
1264
1265    // ===== Cache Invalidation Methods =====
1266
1267    /// Invalidate a cache key across all instances
1268    ///
1269    /// This removes the key from all cache tiers and broadcasts
1270    /// the invalidation to all other cache instances via Redis Pub/Sub.
1271    ///
1272    /// Supports both legacy 2-tier mode and new multi-tier mode (v0.5.0+).
1273    ///
1274    /// # Arguments
1275    /// * `key` - Cache key to invalidate
1276    ///
1277    /// # Example
1278    /// ```rust,ignore
1279    /// // Invalidate user cache after profile update
1280    /// cache_manager.invalidate("user:123").await?;
1281    /// ```
1282    pub async fn invalidate(&self, key: &str) -> Result<()> {
1283        // NEW: Multi-tier mode (v0.5.0+)
1284        if let Some(tiers) = &self.tiers {
1285            // Remove from ALL tiers
1286            for tier in tiers {
1287                if let Err(e) = tier.remove(key).await {
1288                    eprintln!("⚠️ Failed to remove '{}' from L{}: {}", key, tier.tier_level, e);
1289                }
1290            }
1291        } else {
1292            // LEGACY: 2-tier mode
1293            self.l1_cache.remove(key).await?;
1294            self.l2_cache.remove(key).await?;
1295        }
1296
1297        // Broadcast to other instances
1298        if let Some(publisher) = &self.invalidation_publisher {
1299            let mut pub_lock = publisher.lock().await;
1300            let msg = InvalidationMessage::remove(key);
1301            pub_lock.publish(&msg).await?;
1302            self.invalidation_stats.messages_sent.fetch_add(1, Ordering::Relaxed);
1303        }
1304
1305        println!("🗑️  Invalidated '{}' across all instances", key);
1306        Ok(())
1307    }
1308
1309    /// Update cache value across all instances
1310    ///
1311    /// This updates the key in all cache tiers and broadcasts
1312    /// the update to all other cache instances, avoiding cache misses.
1313    ///
1314    /// Supports both legacy 2-tier mode and new multi-tier mode (v0.5.0+).
1315    ///
1316    /// # Arguments
1317    /// * `key` - Cache key to update
1318    /// * `value` - New value
1319    /// * `ttl` - Optional TTL (uses default if None)
1320    ///
1321    /// # Example
1322    /// ```rust,ignore
1323    /// // Update user cache with new data
1324    /// let user_data = serde_json::json!({"id": 123, "name": "Alice"});
1325    /// cache_manager.update_cache("user:123", user_data, Some(Duration::from_secs(3600))).await?;
1326    /// ```
1327    pub async fn update_cache(
1328        &self,
1329        key: &str,
1330        value: serde_json::Value,
1331        ttl: Option<Duration>,
1332    ) -> Result<()> {
1333        let ttl = ttl.unwrap_or_else(|| CacheStrategy::Default.to_duration());
1334
1335        // NEW: Multi-tier mode (v0.5.0+)
1336        if let Some(tiers) = &self.tiers {
1337            // Update ALL tiers with their respective TTL scaling
1338            for tier in tiers {
1339                if let Err(e) = tier.set_with_ttl(key, value.clone(), ttl).await {
1340                    eprintln!("⚠️ Failed to update '{}' in L{}: {}", key, tier.tier_level, e);
1341                }
1342            }
1343        } else {
1344            // LEGACY: 2-tier mode
1345            self.l1_cache.set_with_ttl(key, value.clone(), ttl).await?;
1346            self.l2_cache.set_with_ttl(key, value.clone(), ttl).await?;
1347        }
1348
1349        // Broadcast update to other instances
1350        if let Some(publisher) = &self.invalidation_publisher {
1351            let mut pub_lock = publisher.lock().await;
1352            let msg = InvalidationMessage::update(key, value, Some(ttl));
1353            pub_lock.publish(&msg).await?;
1354            self.invalidation_stats.messages_sent.fetch_add(1, Ordering::Relaxed);
1355        }
1356
1357        println!("🔄 Updated '{}' across all instances", key);
1358        Ok(())
1359    }
1360
1361    /// Invalidate all keys matching a pattern
1362    ///
1363    /// This scans L2 cache for keys matching the pattern, removes them from all tiers,
1364    /// and broadcasts the invalidation. L1 caches will be cleared via broadcast.
1365    ///
1366    /// Supports both legacy 2-tier mode and new multi-tier mode (v0.5.0+).
1367    ///
1368    /// **Note**: Pattern scanning requires a concrete L2Cache instance with `scan_keys()`.
1369    /// In multi-tier mode, this scans from L2 but removes from all tiers.
1370    ///
1371    /// # Arguments
1372    /// * `pattern` - Glob-style pattern (e.g., "user:*", "product:123:*")
1373    ///
1374    /// # Example
1375    /// ```rust,ignore
1376    /// // Invalidate all user caches
1377    /// cache_manager.invalidate_pattern("user:*").await?;
1378    ///
1379    /// // Invalidate specific user's related caches
1380    /// cache_manager.invalidate_pattern("user:123:*").await?;
1381    /// ```
1382    pub async fn invalidate_pattern(&self, pattern: &str) -> Result<()> {
1383        // Scan L2 for matching keys
1384        // (Note: Pattern scanning requires concrete L2Cache with scan_keys support)
1385        let keys = if let Some(l2) = &self.l2_cache_concrete {
1386            l2.scan_keys(pattern).await?
1387        } else {
1388            return Err(anyhow::anyhow!("Pattern invalidation requires concrete L2Cache instance"));
1389        };
1390
1391        if keys.is_empty() {
1392            println!("🔍 No keys found matching pattern '{}'", pattern);
1393            return Ok(());
1394        }
1395
1396        // NEW: Multi-tier mode (v0.5.0+)
1397        if let Some(tiers) = &self.tiers {
1398            // Remove from ALL tiers
1399            for key in &keys {
1400                for tier in tiers {
1401                    if let Err(e) = tier.remove(key).await {
1402                        eprintln!("⚠️ Failed to remove '{}' from L{}: {}", key, tier.tier_level, e);
1403                    }
1404                }
1405            }
1406        } else {
1407            // LEGACY: 2-tier mode - Remove from L2 in bulk
1408            if let Some(l2) = &self.l2_cache_concrete {
1409                l2.remove_bulk(&keys).await?;
1410            }
1411        }
1412
1413        // Broadcast pattern invalidation
1414        if let Some(publisher) = &self.invalidation_publisher {
1415            let mut pub_lock = publisher.lock().await;
1416            let msg = InvalidationMessage::remove_bulk(keys.clone());
1417            pub_lock.publish(&msg).await?;
1418            self.invalidation_stats.messages_sent.fetch_add(1, Ordering::Relaxed);
1419        }
1420
1421        println!("🔍 Invalidated {} keys matching pattern '{}'", keys.len(), pattern);
1422        Ok(())
1423    }
1424
1425    /// Set value with automatic broadcast to all instances
1426    ///
1427    /// This is a write-through operation that updates the cache and
1428    /// broadcasts the update to all other instances automatically.
1429    ///
1430    /// # Arguments
1431    /// * `key` - Cache key
1432    /// * `value` - Value to cache
1433    /// * `strategy` - Cache strategy (determines TTL)
1434    ///
1435    /// # Example
1436    /// ```rust,ignore
1437    /// // Update and broadcast in one call
1438    /// let data = serde_json::json!({"status": "active"});
1439    /// cache_manager.set_with_broadcast("user:123", data, CacheStrategy::MediumTerm).await?;
1440    /// ```
1441    pub async fn set_with_broadcast(
1442        &self,
1443        key: &str,
1444        value: serde_json::Value,
1445        strategy: CacheStrategy,
1446    ) -> Result<()> {
1447        let ttl = strategy.to_duration();
1448
1449        // Set in local caches
1450        self.set_with_strategy(key, value.clone(), strategy).await?;
1451
1452        // Broadcast update if invalidation is enabled
1453        if let Some(publisher) = &self.invalidation_publisher {
1454            let mut pub_lock = publisher.lock().await;
1455            let msg = InvalidationMessage::update(key, value, Some(ttl));
1456            pub_lock.publish(&msg).await?;
1457            self.invalidation_stats.messages_sent.fetch_add(1, Ordering::Relaxed);
1458        }
1459
1460        Ok(())
1461    }
1462
1463    /// Get invalidation statistics
1464    ///
1465    /// Returns statistics about invalidation operations if invalidation is enabled.
1466    pub fn get_invalidation_stats(&self) -> Option<InvalidationStats> {
1467        if self.invalidation_subscriber.is_some() {
1468            Some(self.invalidation_stats.snapshot())
1469        } else {
1470            None
1471        }
1472    }
1473}
1474
1475/// Cache Manager statistics
1476#[allow(dead_code)]
1477#[derive(Debug, Clone)]
1478pub struct CacheManagerStats {
1479    pub total_requests: u64,
1480    pub l1_hits: u64,
1481    pub l2_hits: u64,
1482    pub total_hits: u64,
1483    pub misses: u64,
1484    pub hit_rate: f64,
1485    pub l1_hit_rate: f64,
1486    pub promotions: usize,
1487    pub in_flight_requests: usize,
1488}