rrag 0.1.0-alpha.2

High-performance Rust framework for Retrieval-Augmented Generation with pluggable components, async-first design, and comprehensive observability
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
//! # Intelligent Caching Layer
//!
//! Enterprise-grade multi-level caching system designed specifically for RAG applications
//! with semantic awareness, intelligent eviction policies, and advanced performance
//! optimization features.
//!
//! This module provides a comprehensive caching solution that understands the unique
//! characteristics of RAG workloads, including query similarity, embedding reuse,
//! semantic relationships, and result patterns. It offers multiple cache layers
//! working together to minimize latency and computational overhead.
//!
//! ## Key Features
//!
//! - **Multi-Layer Architecture**: Query, embedding, semantic, and result caches
//! - **Semantic Awareness**: Understanding query similarity and content relationships
//! - **Intelligent Eviction**: Multiple policies (LRU, LFU, TTL, ARC, Semantic-aware)
//! - **Persistence Support**: Durable caching across system restarts
//! - **Performance Monitoring**: Comprehensive metrics and analytics
//! - **Memory Management**: Automatic cleanup and pressure-based eviction
//! - **Async Operations**: Non-blocking cache operations for high throughput
//!
//! ## Architecture
//!
//! The caching system consists of four specialized cache layers:
//!
//! 1. **Query Cache**: Caches complete query-response pairs with similarity matching
//! 2. **Embedding Cache**: Reuses expensive embedding computations
//! 3. **Semantic Cache**: Groups semantically similar queries for broader cache hits
//! 4. **Result Cache**: Caches search results for parameter combinations
//!
//! ## Examples
//!
//! ### Basic Cache Setup
//! ```rust
//! use rrag::caching::{CacheService, CacheConfig};
//!
//! # async fn example() -> rrag::RragResult<()> {
//! let cache_config = CacheConfig::default()
//!     .with_query_cache(true)
//!     .with_embedding_cache(true)
//!     .with_semantic_cache(true);
//!
//! let mut cache = CacheService::new(cache_config)?;
//!
//! // Cache will automatically optimize based on your RAG workload patterns
//! println!("Cache initialized with {} layers", 4);
//! # Ok(())
//! # }
//! ```
//!
//! ### Query Result Caching
//! ```rust
//! use rrag::caching::{QueryCacheEntry, CachedSearchResult};
//!
//! # async fn example() -> rrag::RragResult<()> {
//! # let mut cache = rrag::caching::CacheService::new(rrag::caching::CacheConfig::default())?;
//! let query = "What is machine learning?";
//!
//! // Check cache first
//! if let Some(cached_entry) = cache.get_query_results(query).await {
//!     println!("Cache hit! Retrieved {} results", cached_entry.results.len());
//!     return Ok(());
//! }
//!
//! // If not cached, perform search and cache results
//! let search_results = vec![
//!     CachedSearchResult {
//!         document_id: "doc_1".to_string(),
//!         content: "Machine learning is a subset of AI...".to_string(),
//!         score: 0.95,
//!         rank: 1,
//!         metadata: std::collections::HashMap::new(),
//!     }
//! ];
//!
//! let cache_entry = QueryCacheEntry {
//!     query: query.to_string(),
//!     embedding_hash: "hash123".to_string(),
//!     results: search_results,
//!     generated_answer: Some("ML is a field of AI...".to_string()),
//!     metadata: rrag::caching::CacheEntryMetadata::new(),
//! };
//!
//! cache.cache_query_results(query.to_string(), cache_entry).await?;
//! # Ok(())
//! # }
//! ```
//!
//! ### Embedding Caching for Performance
//! ```rust
//! # async fn example() -> rrag::RragResult<()> {
//! # let mut cache = rrag::caching::CacheService::new(rrag::caching::CacheConfig::default())?;
//! let text = "Advanced machine learning techniques";
//! let model = "sentence-transformers/all-MiniLM-L6-v2";
//!
//! // Check if embedding is already computed
//! if let Some(cached_embedding) = cache.get_embedding(text, model).await {
//!     println!("Using cached embedding of dimension {}", cached_embedding.len());
//! } else {
//!     // Compute embedding (expensive operation)
//!     let embedding = compute_embedding(text, model).await?;
//!     
//!     // Cache for future use
//!     cache.cache_embedding(
//!         text.to_string(),
//!         model.to_string(),
//!         embedding
//!     ).await?;
//!     
//!     println!("Computed and cached new embedding");
//! }
//!
//! # async fn compute_embedding(text: &str, model: &str) -> rrag::RragResult<Vec<f32>> {
//! #     Ok(vec![0.1, 0.2, 0.3]) // Mock embedding
//! # }
//! # Ok(())
//! # }
//! ```
//!
//! ### Semantic Similarity Caching
//! ```rust
//! use rrag::caching::{SemanticCacheEntry, SimilarEntry};
//!
//! # async fn example() -> rrag::RragResult<()> {
//! # let mut cache = rrag::caching::CacheService::new(rrag::caching::CacheConfig::default())?;
//! let similar_queries = [
//!     "What is artificial intelligence?",
//!     "Explain AI concepts",
//!     "Define artificial intelligence",
//! ];
//!
//! // Check for semantically similar cached results
//! for query in &similar_queries {
//!     if let Some(semantic_entry) = cache.get_semantic_results(query).await {
//!         println!("Found semantic match for: {}", query);
//!         println!("Representative: {}", semantic_entry.representative);
//!         println!("Similar entries: {}", semantic_entry.similar_entries.len());
//!         return Ok(());
//!     }
//! }
//!
//! // Create semantic cache entry for related queries
//! let semantic_entry = SemanticCacheEntry {
//!     representative: "What is artificial intelligence?".to_string(),
//!     cluster_id: Some(1),
//!     similar_entries: vec![
//!         SimilarEntry {
//!             text: "Explain AI concepts".to_string(),
//!             similarity: 0.92,
//!             added_at: std::time::SystemTime::now(),
//!         },
//!         SimilarEntry {
//!             text: "Define artificial intelligence".to_string(),
//!             similarity: 0.89,
//!             added_at: std::time::SystemTime::now(),
//!         },
//!     ],
//!     results: vec![], // Shared results for all similar queries
//!     metadata: rrag::caching::CacheEntryMetadata::new(),
//! };
//!
//! cache.cache_semantic_results(
//!     "ai_concepts_cluster".to_string(),
//!     semantic_entry
//! ).await?;
//! # Ok(())
//! # }
//! ```
//!
//! ### Advanced Cache Configuration
//! ```rust
//! use rrag::caching::{
//!     CacheConfig, QueryCacheConfig, EmbeddingCacheConfig,
//!     EvictionPolicy, PersistenceConfig, PersistenceFormat
//! };
//! use std::time::Duration;
//!
//! # async fn example() -> rrag::RragResult<()> {
//! let advanced_config = CacheConfig {
//!     enabled: true,
//!     query_cache: QueryCacheConfig {
//!         enabled: true,
//!         max_size: 5000,
//!         ttl: Duration::from_secs(7200), // 2 hours
//!         eviction_policy: EvictionPolicy::SemanticAware,
//!         similarity_threshold: 0.92,
//!     },
//!     embedding_cache: EmbeddingCacheConfig {
//!         enabled: true,
//!         max_size: 50000,
//!         ttl: Duration::from_secs(86400), // 24 hours
//!         eviction_policy: EvictionPolicy::LFU,
//!         compression_enabled: true,
//!     },
//!     persistence: PersistenceConfig {
//!         enabled: true,
//!         storage_path: "/data/rag_cache".to_string(),
//!         auto_save_interval: Duration::from_secs(300),
//!         format: PersistenceFormat::MessagePack,
//!     },
//!     ..Default::default()
//! };
//!
//! let cache = CacheService::new(advanced_config)?;
//! println!("Advanced cache configured with persistence and compression");
//! # Ok(())
//! # }
//! ```
//!
//! ### Cache Performance Monitoring
//! ```rust
//! # async fn example() -> rrag::RragResult<()> {
//! # let mut cache = rrag::caching::CacheService::new(rrag::caching::CacheConfig::default())?;
//! // Get comprehensive cache metrics
//! let metrics = cache.get_metrics();
//!
//! println!("📊 Cache Performance Report");
//! println!("Query Cache: {:.1}% hit rate, {} entries",
//!          metrics.query_cache.hit_rate * 100.0,
//!          metrics.query_cache.total_entries);
//!
//! println!("Embedding Cache: {:.1}% hit rate, {:.1}MB memory",
//!          metrics.embedding_cache.hit_rate * 100.0,
//!          metrics.embedding_cache.memory_usage as f32 / 1024.0 / 1024.0);
//!
//! println!("Semantic Cache: {:.1}% hit rate, {} evictions",
//!          metrics.semantic_cache.hit_rate * 100.0,
//!          metrics.semantic_cache.evictions);
//!
//! println!("Overall Efficiency: {:.1}%, Time Saved: {:.1}ms",
//!          metrics.overall.efficiency_score * 100.0,
//!          metrics.overall.time_saved_ms);
//!
//! // Performance optimization based on metrics
//! if metrics.overall.memory_pressure > 0.8 {
//!     println!("⚠️  High memory pressure detected, triggering cleanup");
//!     cache.maintenance().await?;
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ## Cache Eviction Policies
//!
//! ### LRU (Least Recently Used)
//! Best for: General-purpose caching with temporal locality
//! - Evicts items that haven't been accessed recently
//! - Good memory efficiency
//! - Simple and fast
//!
//! ### LFU (Least Frequently Used)
//! Best for: Embedding caches where popular items should stay
//! - Evicts items with lowest access frequency
//! - Excellent for reusable computations
//! - Handles long-term access patterns
//!
//! ### TTL (Time-To-Live)
//! Best for: Fresh data requirements
//! - Evicts items after fixed time period
//! - Ensures data freshness
//! - Predictable memory usage
//!
//! ### Semantic-Aware
//! Best for: RAG-specific query patterns
//! - Considers semantic similarity in eviction decisions
//! - Maintains representative queries from clusters
//! - Optimizes for query pattern diversity
//!
//! ## Performance Optimization Tips
//!
//! 1. **Tune Cache Sizes**: Monitor hit rates and adjust max_size accordingly
//! 2. **Enable Compression**: For embedding caches with large vectors
//! 3. **Use Persistence**: For frequently reused embeddings across sessions
//! 4. **Semantic Clustering**: Enable for diverse query workloads
//! 5. **Async Operations**: Enable for high-throughput applications
//! 6. **Memory Monitoring**: Set appropriate pressure thresholds
//!
//! ## Integration with RAG Systems
//!
//! ```rust
//! use rrag::{RragSystemBuilder, caching::CacheConfig};
//!
//! # async fn example() -> rrag::RragResult<()> {
//! let rag_system = RragSystemBuilder::new()
//!     .with_caching(
//!         CacheConfig::production()
//!             .with_semantic_awareness(true)
//!             .with_persistence(true)
//!             .with_intelligent_eviction(true)
//!     )
//!     .build()
//!     .await?;
//!
//! // Cache automatically optimizes based on your query patterns
//! let results = rag_system.search("machine learning applications", Some(10)).await?;
//! // Subsequent similar queries will benefit from semantic caching
//! # Ok(())
//! # }
//! ```

pub mod cache_core;
pub mod embedding_cache;
pub mod metrics;
pub mod persistence;
pub mod policies;
pub mod query_cache;
pub mod result_cache;
pub mod semantic_cache;

use crate::RragResult;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::hash::{Hash, Hasher};
use std::time::{Duration, SystemTime};

/// Main caching service orchestrating multiple cache layers
pub struct CacheService {
    /// Query result cache
    query_cache: Box<dyn Cache<String, QueryCacheEntry>>,

    /// Embedding cache for reusing computations
    embedding_cache: Box<dyn Cache<String, EmbeddingCacheEntry>>,

    /// Semantic similarity cache
    semantic_cache: Box<dyn Cache<String, SemanticCacheEntry>>,

    /// Document retrieval cache
    result_cache: Box<dyn Cache<String, ResultCacheEntry>>,

    /// Cache configuration
    config: CacheConfig,

    /// Performance metrics
    metrics: CacheMetrics,
}

/// Global cache configuration
#[derive(Debug, Clone)]
pub struct CacheConfig {
    /// Enable/disable caching globally
    pub enabled: bool,

    /// Query cache configuration
    pub query_cache: QueryCacheConfig,

    /// Embedding cache configuration
    pub embedding_cache: EmbeddingCacheConfig,

    /// Semantic cache configuration
    pub semantic_cache: SemanticCacheConfig,

    /// Result cache configuration
    pub result_cache: ResultCacheConfig,

    /// Persistence configuration
    pub persistence: PersistenceConfig,

    /// Performance tuning
    pub performance: PerformanceConfig,
}

/// Query cache configuration
#[derive(Debug, Clone)]
pub struct QueryCacheConfig {
    pub enabled: bool,
    pub max_size: usize,
    pub ttl: Duration,
    pub eviction_policy: EvictionPolicy,
    pub similarity_threshold: f32,
}

/// Embedding cache configuration
#[derive(Debug, Clone)]
pub struct EmbeddingCacheConfig {
    pub enabled: bool,
    pub max_size: usize,
    pub ttl: Duration,
    pub eviction_policy: EvictionPolicy,
    pub compression_enabled: bool,
}

/// Semantic cache configuration
#[derive(Debug, Clone)]
pub struct SemanticCacheConfig {
    pub enabled: bool,
    pub max_size: usize,
    pub ttl: Duration,
    pub similarity_threshold: f32,
    pub clustering_enabled: bool,
    pub max_clusters: usize,
}

/// Result cache configuration
#[derive(Debug, Clone)]
pub struct ResultCacheConfig {
    pub enabled: bool,
    pub max_size: usize,
    pub ttl: Duration,
    pub eviction_policy: EvictionPolicy,
    pub compress_large_results: bool,
}

/// Persistence configuration
#[derive(Debug, Clone)]
pub struct PersistenceConfig {
    pub enabled: bool,
    pub storage_path: String,
    pub auto_save_interval: Duration,
    pub format: PersistenceFormat,
}

/// Performance configuration
#[derive(Debug, Clone)]
pub struct PerformanceConfig {
    pub async_writes: bool,
    pub batch_operations: bool,
    pub background_cleanup: bool,
    pub memory_pressure_threshold: f32,
}

/// Cache eviction policies
#[derive(Debug, Clone, Copy)]
pub enum EvictionPolicy {
    /// Least Recently Used
    LRU,
    /// Least Frequently Used
    LFU,
    /// Time-To-Live based
    TTL,
    /// Adaptive Replacement Cache
    ARC,
    /// Custom semantic-aware policy
    SemanticAware,
}

/// Persistence formats
#[derive(Debug, Clone)]
pub enum PersistenceFormat {
    Binary,
    Json,
    MessagePack,
}

/// Generic cache trait
pub trait Cache<K, V>: Send + Sync
where
    K: Hash + Eq + Clone + Send + Sync + 'static,
    V: Clone + Send + Sync + 'static,
{
    /// Get value from cache
    fn get(&self, key: &K) -> Option<V>;

    /// Put value into cache
    fn put(&mut self, key: K, value: V) -> RragResult<()>;

    /// Remove value from cache
    fn remove(&mut self, key: &K) -> Option<V>;

    /// Check if key exists
    fn contains(&self, key: &K) -> bool;

    /// Clear all entries
    fn clear(&mut self);

    /// Get cache size
    fn size(&self) -> usize;

    /// Get cache statistics
    fn stats(&self) -> CacheStats;
}

/// Cache entry metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CacheEntryMetadata {
    /// Creation timestamp
    pub created_at: SystemTime,

    /// Last access timestamp
    pub last_accessed: SystemTime,

    /// Access count
    pub access_count: u64,

    /// Entry size in bytes
    pub size_bytes: usize,

    /// Time-to-live
    pub ttl: Option<Duration>,

    /// Custom metadata
    pub custom: HashMap<String, String>,
}

/// Query cache entry
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QueryCacheEntry {
    /// Original query
    pub query: String,

    /// Query embedding hash
    pub embedding_hash: String,

    /// Cached results
    pub results: Vec<CachedSearchResult>,

    /// Generation result if any
    pub generated_answer: Option<String>,

    /// Metadata
    pub metadata: CacheEntryMetadata,
}

/// Embedding cache entry
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EmbeddingCacheEntry {
    /// Input text
    pub text: String,

    /// Text hash for verification
    pub text_hash: String,

    /// Computed embedding
    pub embedding: Vec<f32>,

    /// Model used for embedding
    pub model: String,

    /// Metadata
    pub metadata: CacheEntryMetadata,
}

/// Semantic cache entry
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SemanticCacheEntry {
    /// Representative query/text
    pub representative: String,

    /// Cluster ID if clustering enabled
    pub cluster_id: Option<usize>,

    /// Similar queries/texts
    pub similar_entries: Vec<SimilarEntry>,

    /// Cached semantic results
    pub results: Vec<CachedSearchResult>,

    /// Metadata
    pub metadata: CacheEntryMetadata,
}

/// Result cache entry
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResultCacheEntry {
    /// Search parameters hash
    pub params_hash: String,

    /// Cached search results
    pub results: Vec<CachedSearchResult>,

    /// Result metadata
    pub result_metadata: HashMap<String, String>,

    /// Metadata
    pub metadata: CacheEntryMetadata,
}

/// Similar entry for semantic cache
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SimilarEntry {
    /// Similar text
    pub text: String,

    /// Similarity score
    pub similarity: f32,

    /// When added
    pub added_at: SystemTime,
}

/// Cached search result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CachedSearchResult {
    /// Document ID
    pub document_id: String,

    /// Document content
    pub content: String,

    /// Relevance score
    pub score: f32,

    /// Result rank
    pub rank: usize,

    /// Additional metadata
    pub metadata: HashMap<String, String>,
}

/// Cache statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CacheStats {
    /// Total number of entries
    pub total_entries: usize,

    /// Total cache hits
    pub hits: u64,

    /// Total cache misses
    pub misses: u64,

    /// Hit rate percentage
    pub hit_rate: f32,

    /// Total memory usage in bytes
    pub memory_usage: usize,

    /// Average access time in microseconds
    pub avg_access_time_us: f32,

    /// Eviction count
    pub evictions: u64,

    /// Last cleanup time
    pub last_cleanup: SystemTime,
}

/// Cache performance metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CacheMetrics {
    /// Query cache metrics
    pub query_cache: CacheStats,

    /// Embedding cache metrics
    pub embedding_cache: CacheStats,

    /// Semantic cache metrics
    pub semantic_cache: CacheStats,

    /// Result cache metrics
    pub result_cache: CacheStats,

    /// Overall performance metrics
    pub overall: OverallCacheMetrics,
}

/// Overall cache performance metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OverallCacheMetrics {
    /// Total memory saved (bytes)
    pub memory_saved: usize,

    /// Total time saved (milliseconds)
    pub time_saved_ms: f32,

    /// Cache efficiency score
    pub efficiency_score: f32,

    /// Memory pressure level (0.0 to 1.0)
    pub memory_pressure: f32,

    /// Total operations per second
    pub ops_per_second: f32,
}

impl CacheService {
    /// Create new cache service
    pub fn new(config: CacheConfig) -> RragResult<Self> {
        let query_cache = Box::new(query_cache::QueryCache::new(config.query_cache.clone())?);

        let embedding_cache = Box::new(embedding_cache::EmbeddingCache::new(
            config.embedding_cache.clone(),
        )?);

        let semantic_cache = Box::new(semantic_cache::SemanticCache::new(
            config.semantic_cache.clone(),
        )?);

        let result_cache = Box::new(result_cache::ResultCache::new(config.result_cache.clone())?);

        Ok(Self {
            query_cache,
            embedding_cache,
            semantic_cache,
            result_cache,
            config,
            metrics: CacheMetrics::default(),
        })
    }

    /// Get cached query results
    pub async fn get_query_results(&self, query: &str) -> Option<QueryCacheEntry> {
        if !self.config.enabled || !self.config.query_cache.enabled {
            return None;
        }

        self.query_cache.get(&query.to_string())
    }

    /// Cache query results
    pub async fn cache_query_results(
        &mut self,
        query: String,
        entry: QueryCacheEntry,
    ) -> RragResult<()> {
        if !self.config.enabled || !self.config.query_cache.enabled {
            return Ok(());
        }

        self.query_cache.put(query, entry)
    }

    /// Get cached embedding
    pub async fn get_embedding(&self, text: &str, model: &str) -> Option<Vec<f32>> {
        if !self.config.enabled || !self.config.embedding_cache.enabled {
            return None;
        }

        let key = format!("{}:{}", model, text);
        self.embedding_cache.get(&key).map(|entry| entry.embedding)
    }

    /// Cache embedding
    pub async fn cache_embedding(
        &mut self,
        text: String,
        model: String,
        embedding: Vec<f32>,
    ) -> RragResult<()> {
        if !self.config.enabled || !self.config.embedding_cache.enabled {
            return Ok(());
        }

        let key = format!("{}:{}", model, text);
        let entry = EmbeddingCacheEntry {
            text: text.clone(),
            text_hash: Self::hash_string(&text),
            embedding,
            model,
            metadata: CacheEntryMetadata::new(),
        };

        self.embedding_cache.put(key, entry)
    }

    /// Get semantically similar cached results
    pub async fn get_semantic_results(&self, query: &str) -> Option<SemanticCacheEntry> {
        if !self.config.enabled || !self.config.semantic_cache.enabled {
            return None;
        }

        self.semantic_cache.get(&query.to_string())
    }

    /// Cache semantic results
    pub async fn cache_semantic_results(
        &mut self,
        query: String,
        entry: SemanticCacheEntry,
    ) -> RragResult<()> {
        if !self.config.enabled || !self.config.semantic_cache.enabled {
            return Ok(());
        }

        self.semantic_cache.put(query, entry)
    }

    /// Get cache metrics
    pub fn get_metrics(&self) -> &CacheMetrics {
        &self.metrics
    }

    /// Clear all caches
    pub fn clear_all(&mut self) {
        self.query_cache.clear();
        self.embedding_cache.clear();
        self.semantic_cache.clear();
        self.result_cache.clear();
    }

    /// Perform cache maintenance
    pub async fn maintenance(&mut self) -> RragResult<()> {
        // Background cleanup, eviction, persistence, etc.
        Ok(())
    }

    /// Hash string for cache keys
    fn hash_string(s: &str) -> String {
        use std::collections::hash_map::DefaultHasher;
        let mut hasher = DefaultHasher::new();
        s.hash(&mut hasher);
        format!("{:x}", hasher.finish())
    }
}

impl CacheEntryMetadata {
    /// Create new metadata
    pub fn new() -> Self {
        let now = SystemTime::now();
        Self {
            created_at: now,
            last_accessed: now,
            access_count: 0,
            size_bytes: 0,
            ttl: None,
            custom: HashMap::new(),
        }
    }

    /// Update access info
    pub fn accessed(&mut self) {
        self.last_accessed = SystemTime::now();
        self.access_count += 1;
    }

    /// Check if entry has expired
    pub fn is_expired(&self) -> bool {
        if let Some(ttl) = self.ttl {
            if let Ok(elapsed) = self.created_at.elapsed() {
                return elapsed > ttl;
            }
        }
        false
    }
}

impl Default for CacheConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            query_cache: QueryCacheConfig::default(),
            embedding_cache: EmbeddingCacheConfig::default(),
            semantic_cache: SemanticCacheConfig::default(),
            result_cache: ResultCacheConfig::default(),
            persistence: PersistenceConfig::default(),
            performance: PerformanceConfig::default(),
        }
    }
}

impl Default for QueryCacheConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            max_size: 1000,
            ttl: Duration::from_secs(3600), // 1 hour
            eviction_policy: EvictionPolicy::LRU,
            similarity_threshold: 0.95,
        }
    }
}

impl Default for EmbeddingCacheConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            max_size: 10000,
            ttl: Duration::from_secs(86400), // 24 hours
            eviction_policy: EvictionPolicy::LFU,
            compression_enabled: true,
        }
    }
}

impl Default for SemanticCacheConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            max_size: 5000,
            ttl: Duration::from_secs(7200), // 2 hours
            similarity_threshold: 0.85,
            clustering_enabled: true,
            max_clusters: 100,
        }
    }
}

impl Default for ResultCacheConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            max_size: 2000,
            ttl: Duration::from_secs(1800), // 30 minutes
            eviction_policy: EvictionPolicy::TTL,
            compress_large_results: true,
        }
    }
}

impl Default for PersistenceConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            storage_path: "./cache".to_string(),
            auto_save_interval: Duration::from_secs(300), // 5 minutes
            format: PersistenceFormat::Binary,
        }
    }
}

impl Default for PerformanceConfig {
    fn default() -> Self {
        Self {
            async_writes: true,
            batch_operations: true,
            background_cleanup: true,
            memory_pressure_threshold: 0.8,
        }
    }
}

impl Default for CacheStats {
    fn default() -> Self {
        Self {
            total_entries: 0,
            hits: 0,
            misses: 0,
            hit_rate: 0.0,
            memory_usage: 0,
            avg_access_time_us: 0.0,
            evictions: 0,
            last_cleanup: SystemTime::now(),
        }
    }
}

impl Default for CacheMetrics {
    fn default() -> Self {
        Self {
            query_cache: CacheStats::default(),
            embedding_cache: CacheStats::default(),
            semantic_cache: CacheStats::default(),
            result_cache: CacheStats::default(),
            overall: OverallCacheMetrics::default(),
        }
    }
}

impl Default for OverallCacheMetrics {
    fn default() -> Self {
        Self {
            memory_saved: 0,
            time_saved_ms: 0.0,
            efficiency_score: 0.0,
            memory_pressure: 0.0,
            ops_per_second: 0.0,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_cache_service_creation() {
        let config = CacheConfig::default();
        let cache_service = CacheService::new(config).unwrap();

        let metrics = cache_service.get_metrics();
        assert_eq!(metrics.overall.efficiency_score, 0.0);
    }

    #[test]
    fn test_cache_entry_metadata() {
        let mut metadata = CacheEntryMetadata::new();
        assert_eq!(metadata.access_count, 0);

        metadata.accessed();
        assert_eq!(metadata.access_count, 1);
    }

    #[test]
    fn test_cache_config_defaults() {
        let config = CacheConfig::default();
        assert!(config.enabled);
        assert!(config.query_cache.enabled);
        assert!(config.embedding_cache.enabled);
    }
}