oxirs-samm 0.2.2

Semantic Aspect Meta Model (SAMM) implementation for OxiRS
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
//! LRU Cache for SAMM Model Elements
//!
//! This module provides an efficient Least Recently Used (LRU) cache for SAMM model elements.
//! The cache automatically evicts the least recently used items when it reaches capacity.
//!
//! # Use Cases
//!
//! - **Large model repositories**: Cache frequently accessed aspects
//! - **Web applications**: Reduce parsing overhead for repeated requests
//! - **Batch processing**: Reuse parsed models across multiple operations
//! - **Memory constraints**: Limit cache size while maintaining performance
//!
//! # Example
//!
//! ```rust
//! use oxirs_samm::cache::LruModelCache;
//! use oxirs_samm::metamodel::Aspect;
//! use std::sync::Arc;
//!
//! let mut cache = LruModelCache::new(100); // Cache up to 100 aspects
//!
//! let aspect = Aspect::new("urn:samm:org.example:1.0.0#MyAspect".to_string());
//! cache.put("my-aspect".to_string(), Arc::new(aspect));
//!
//! // Later retrieval
//! if let Some(cached_aspect) = cache.get("my-aspect") {
//!     println!("Cache hit!");
//! }
//!
//! // Check cache statistics
//! println!("Cache size: {}/{}", cache.len(), cache.capacity());
//! println!("Hit rate: {:.2}%", cache.hit_rate() * 100.0);
//! ```

use crate::metamodel::{Aspect, Characteristic, Entity, Operation, Property};
use std::collections::HashMap;
use std::sync::{Arc, RwLock};

/// Entry in the LRU cache with access tracking
#[derive(Clone)]
struct CacheEntry<T> {
    value: Arc<T>,
    access_count: usize,
    last_accessed: std::time::Instant,
}

/// LRU (Least Recently Used) cache for model elements
///
/// Thread-safe cache with automatic eviction of least recently used items.
pub struct LruModelCache<T> {
    capacity: usize,
    entries: Arc<RwLock<HashMap<String, CacheEntry<T>>>>,
    access_order: Arc<RwLock<Vec<String>>>,
    hits: Arc<RwLock<usize>>,
    misses: Arc<RwLock<usize>>,
}

impl<T> LruModelCache<T>
where
    T: Clone,
{
    /// Create a new LRU cache with specified capacity
    ///
    /// # Arguments
    ///
    /// * `capacity` - Maximum number of items to cache
    ///
    /// # Example
    ///
    /// ```rust
    /// use oxirs_samm::cache::LruModelCache;
    /// use oxirs_samm::metamodel::Aspect;
    ///
    /// let cache: LruModelCache<Aspect> = LruModelCache::new(50);
    /// assert_eq!(cache.capacity(), 50);
    /// ```
    pub fn new(capacity: usize) -> Self {
        Self {
            capacity: capacity.max(1), // Minimum capacity of 1
            entries: Arc::new(RwLock::new(HashMap::new())),
            access_order: Arc::new(RwLock::new(Vec::new())),
            hits: Arc::new(RwLock::new(0)),
            misses: Arc::new(RwLock::new(0)),
        }
    }

    /// Get an item from the cache
    ///
    /// Updates access statistics and LRU ordering.
    pub fn get(&self, key: &str) -> Option<Arc<T>> {
        let mut entries = self.entries.write().expect("lock poisoned");

        if let Some(entry) = entries.get_mut(key) {
            // Update access statistics
            entry.access_count += 1;
            entry.last_accessed = std::time::Instant::now();

            // Update access order
            let mut access_order = self.access_order.write().expect("lock poisoned");
            if let Some(pos) = access_order.iter().position(|k| k == key) {
                access_order.remove(pos);
            }
            access_order.push(key.to_string());

            // Record hit
            *self.hits.write().expect("lock poisoned") += 1;

            Some(Arc::clone(&entry.value))
        } else {
            // Record miss
            *self.misses.write().expect("lock poisoned") += 1;
            None
        }
    }

    /// Put an item into the cache
    ///
    /// If the cache is at capacity, evicts the least recently used item.
    pub fn put(&mut self, key: String, value: Arc<T>) {
        let mut entries = self.entries.write().expect("lock poisoned");
        let mut access_order = self.access_order.write().expect("lock poisoned");

        // Remove existing entry if present
        if entries.contains_key(&key) {
            if let Some(pos) = access_order.iter().position(|k| k == &key) {
                access_order.remove(pos);
            }
        }

        // Evict LRU entry if at capacity
        if entries.len() >= self.capacity && !entries.contains_key(&key) {
            if let Some(lru_key) = access_order.first() {
                let lru_key = lru_key.clone();
                entries.remove(&lru_key);
                access_order.remove(0);
            }
        }

        // Insert new entry
        let entry = CacheEntry {
            value,
            access_count: 0,
            last_accessed: std::time::Instant::now(),
        };

        entries.insert(key.clone(), entry);
        access_order.push(key);
    }

    /// Check if the cache contains a key
    pub fn contains(&self, key: &str) -> bool {
        let entries = self.entries.read().expect("lock poisoned");
        entries.contains_key(key)
    }

    /// Remove an item from the cache
    pub fn remove(&mut self, key: &str) -> Option<Arc<T>> {
        let mut entries = self.entries.write().expect("lock poisoned");
        let mut access_order = self.access_order.write().expect("lock poisoned");

        if let Some(pos) = access_order.iter().position(|k| k == key) {
            access_order.remove(pos);
        }

        entries.remove(key).map(|entry| entry.value)
    }

    /// Clear all items from the cache
    pub fn clear(&mut self) {
        let mut entries = self.entries.write().expect("lock poisoned");
        let mut access_order = self.access_order.write().expect("lock poisoned");

        entries.clear();
        access_order.clear();
    }

    /// Get the number of items in the cache
    pub fn len(&self) -> usize {
        let entries = self.entries.read().expect("lock poisoned");
        entries.len()
    }

    /// Check if the cache is empty
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Get the cache capacity
    pub fn capacity(&self) -> usize {
        self.capacity
    }

    /// Resize the cache capacity
    ///
    /// If the new capacity is smaller than current size, evicts LRU items.
    pub fn resize(&mut self, new_capacity: usize) {
        self.capacity = new_capacity.max(1);

        let mut entries = self.entries.write().expect("lock poisoned");
        let mut access_order = self.access_order.write().expect("lock poisoned");

        // Evict items if over capacity
        while entries.len() > self.capacity {
            if let Some(lru_key) = access_order.first() {
                let lru_key = lru_key.clone();
                entries.remove(&lru_key);
                access_order.remove(0);
            } else {
                break;
            }
        }
    }

    /// Get cache hit rate (0.0 to 1.0)
    pub fn hit_rate(&self) -> f64 {
        let hits = *self.hits.read().expect("lock poisoned");
        let misses = *self.misses.read().expect("lock poisoned");
        let total = hits + misses;

        if total == 0 {
            0.0
        } else {
            hits as f64 / total as f64
        }
    }

    /// Get total number of cache hits
    pub fn hits(&self) -> usize {
        *self.hits.read().expect("lock poisoned")
    }

    /// Get total number of cache misses
    pub fn misses(&self) -> usize {
        *self.misses.read().expect("lock poisoned")
    }

    /// Reset cache statistics
    pub fn reset_statistics(&mut self) {
        *self.hits.write().expect("lock poisoned") = 0;
        *self.misses.write().expect("lock poisoned") = 0;
    }

    /// Get all keys in the cache (in LRU order)
    pub fn keys(&self) -> Vec<String> {
        let access_order = self.access_order.read().expect("lock poisoned");
        access_order.clone()
    }

    /// Get cache statistics
    pub fn statistics(&self) -> CacheStatistics {
        CacheStatistics {
            size: self.len(),
            capacity: self.capacity,
            hits: self.hits(),
            misses: self.misses(),
            hit_rate: self.hit_rate(),
        }
    }
}

impl<T> Clone for LruModelCache<T> {
    fn clone(&self) -> Self {
        Self {
            capacity: self.capacity,
            entries: Arc::clone(&self.entries),
            access_order: Arc::clone(&self.access_order),
            hits: Arc::clone(&self.hits),
            misses: Arc::clone(&self.misses),
        }
    }
}

/// Cache statistics
#[derive(Debug, Clone)]
pub struct CacheStatistics {
    /// Current number of items
    pub size: usize,
    /// Maximum capacity
    pub capacity: usize,
    /// Total hits
    pub hits: usize,
    /// Total misses
    pub misses: usize,
    /// Hit rate (0.0 to 1.0)
    pub hit_rate: f64,
}

impl CacheStatistics {
    /// Get the fill percentage (0.0 to 100.0)
    pub fn fill_percentage(&self) -> f64 {
        if self.capacity == 0 {
            0.0
        } else {
            (self.size as f64 / self.capacity as f64) * 100.0
        }
    }

    /// Get total accesses
    pub fn total_accesses(&self) -> usize {
        self.hits + self.misses
    }
}

// Type aliases for common cache types
/// LRU cache for Aspect models
pub type AspectCache = LruModelCache<Aspect>;

/// LRU cache for Property elements
pub type PropertyCache = LruModelCache<Property>;

/// LRU cache for Characteristic elements
pub type CharacteristicCache = LruModelCache<Characteristic>;

/// LRU cache for Entity elements
pub type EntityCache = LruModelCache<Entity>;

/// LRU cache for Operation elements
pub type OperationCache = LruModelCache<Operation>;

/// TTL (Time-To-Live) cache entry with expiration time
#[derive(Clone)]
struct TtlCacheEntry<T> {
    value: Arc<T>,
    access_count: usize,
    created_at: std::time::Instant,
    expires_at: std::time::Instant,
}

/// Cache with Time-To-Live (TTL) support
///
/// Combines LRU eviction with time-based expiration. Entries automatically
/// expire after a configurable TTL period.
///
/// # Use Cases
///
/// - **Remote model caching**: Auto-expire cached HTTP-fetched models
/// - **Session-based caching**: Expire models after session timeout
/// - **Memory safety**: Ensure stale data is automatically removed
/// - **Time-sensitive data**: Cache models with validity periods
///
/// # Example
///
/// ```rust
/// use oxirs_samm::cache::TtlCache;
/// use oxirs_samm::metamodel::Aspect;
/// use std::sync::Arc;
/// use std::time::Duration;
///
/// let mut cache = TtlCache::new(100, Duration::from_secs(300)); // 5 min TTL
///
/// let aspect = Aspect::new("urn:samm:org.example:1.0.0#MyAspect".to_string());
/// cache.put("my-aspect".to_string(), Arc::new(aspect));
///
/// // Entry expires after 5 minutes
/// // Manual expiration check
/// cache.evict_expired();
/// ```
pub struct TtlCache<T> {
    capacity: usize,
    ttl: std::time::Duration,
    entries: Arc<RwLock<HashMap<String, TtlCacheEntry<T>>>>,
    access_order: Arc<RwLock<Vec<String>>>,
    hits: Arc<RwLock<usize>>,
    misses: Arc<RwLock<usize>>,
    expirations: Arc<RwLock<usize>>,
}

impl<T> TtlCache<T>
where
    T: Clone,
{
    /// Create a new TTL cache with specified capacity and TTL
    ///
    /// # Arguments
    ///
    /// * `capacity` - Maximum number of items to cache
    /// * `ttl` - Time-to-live duration for each entry
    ///
    /// # Example
    ///
    /// ```rust
    /// use oxirs_samm::cache::TtlCache;
    /// use oxirs_samm::metamodel::Aspect;
    /// use std::time::Duration;
    ///
    /// let cache: TtlCache<Aspect> = TtlCache::new(50, Duration::from_secs(600));
    /// assert_eq!(cache.capacity(), 50);
    /// ```
    pub fn new(capacity: usize, ttl: std::time::Duration) -> Self {
        Self {
            capacity: capacity.max(1),
            ttl,
            entries: Arc::new(RwLock::new(HashMap::new())),
            access_order: Arc::new(RwLock::new(Vec::new())),
            hits: Arc::new(RwLock::new(0)),
            misses: Arc::new(RwLock::new(0)),
            expirations: Arc::new(RwLock::new(0)),
        }
    }

    /// Get an item from the cache
    ///
    /// Returns None if item is expired or not found.
    pub fn get(&self, key: &str) -> Option<Arc<T>> {
        let mut entries = self.entries.write().expect("lock poisoned");

        if let Some(entry) = entries.get_mut(key) {
            let now = std::time::Instant::now();

            // Check if expired
            if now >= entry.expires_at {
                // Remove expired entry
                entries.remove(key);
                let mut access_order = self.access_order.write().expect("lock poisoned");
                if let Some(pos) = access_order.iter().position(|k| k == key) {
                    access_order.remove(pos);
                }
                *self.expirations.write().expect("lock poisoned") += 1;
                *self.misses.write().expect("lock poisoned") += 1;
                return None;
            }

            // Update access statistics
            entry.access_count += 1;

            // Update access order
            let mut access_order = self.access_order.write().expect("lock poisoned");
            if let Some(pos) = access_order.iter().position(|k| k == key) {
                access_order.remove(pos);
            }
            access_order.push(key.to_string());

            // Record hit
            *self.hits.write().expect("lock poisoned") += 1;

            Some(Arc::clone(&entry.value))
        } else {
            // Record miss
            *self.misses.write().expect("lock poisoned") += 1;
            None
        }
    }

    /// Put an item into the cache with TTL
    ///
    /// If the cache is at capacity, evicts the least recently used item.
    pub fn put(&mut self, key: String, value: Arc<T>) {
        let mut entries = self.entries.write().expect("lock poisoned");
        let mut access_order = self.access_order.write().expect("lock poisoned");

        // Remove existing entry if present
        if entries.contains_key(&key) {
            if let Some(pos) = access_order.iter().position(|k| k == &key) {
                access_order.remove(pos);
            }
        }

        // Evict LRU entry if at capacity
        if entries.len() >= self.capacity && !entries.contains_key(&key) {
            if let Some(lru_key) = access_order.first() {
                let lru_key = lru_key.clone();
                entries.remove(&lru_key);
                access_order.remove(0);
            }
        }

        // Insert new entry with expiration
        let now = std::time::Instant::now();
        let entry = TtlCacheEntry {
            value,
            access_count: 0,
            created_at: now,
            expires_at: now + self.ttl,
        };

        entries.insert(key.clone(), entry);
        access_order.push(key);
    }

    /// Evict all expired entries
    ///
    /// Returns the number of entries evicted.
    pub fn evict_expired(&mut self) -> usize {
        let mut entries = self.entries.write().expect("lock poisoned");
        let mut access_order = self.access_order.write().expect("lock poisoned");
        let now = std::time::Instant::now();

        let expired_keys: Vec<String> = entries
            .iter()
            .filter(|(_, entry)| now >= entry.expires_at)
            .map(|(key, _)| key.clone())
            .collect();

        let count = expired_keys.len();

        for key in expired_keys {
            entries.remove(&key);
            if let Some(pos) = access_order.iter().position(|k| k == &key) {
                access_order.remove(pos);
            }
        }

        *self.expirations.write().expect("lock poisoned") += count;
        count
    }

    /// Get the number of items in the cache
    pub fn len(&self) -> usize {
        let entries = self.entries.read().expect("lock poisoned");
        entries.len()
    }

    /// Check if the cache is empty
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Get the cache capacity
    pub fn capacity(&self) -> usize {
        self.capacity
    }

    /// Get the TTL duration
    pub fn ttl(&self) -> std::time::Duration {
        self.ttl
    }

    /// Clear all items from the cache
    pub fn clear(&mut self) {
        let mut entries = self.entries.write().expect("lock poisoned");
        let mut access_order = self.access_order.write().expect("lock poisoned");

        entries.clear();
        access_order.clear();
    }

    /// Get cache hit rate (0.0 to 1.0)
    pub fn hit_rate(&self) -> f64 {
        let hits = *self.hits.read().expect("lock poisoned");
        let misses = *self.misses.read().expect("lock poisoned");
        let total = hits + misses;

        if total == 0 {
            0.0
        } else {
            hits as f64 / total as f64
        }
    }

    /// Get total number of expirations
    pub fn expirations(&self) -> usize {
        *self.expirations.read().expect("lock poisoned")
    }

    /// Get cache statistics including TTL information
    pub fn statistics(&self) -> TtlCacheStatistics {
        TtlCacheStatistics {
            size: self.len(),
            capacity: self.capacity,
            hits: *self.hits.read().expect("lock poisoned"),
            misses: *self.misses.read().expect("lock poisoned"),
            expirations: self.expirations(),
            hit_rate: self.hit_rate(),
            ttl_seconds: self.ttl.as_secs(),
        }
    }
}

/// TTL cache statistics
#[derive(Debug, Clone)]
pub struct TtlCacheStatistics {
    /// Current number of items
    pub size: usize,
    /// Maximum capacity
    pub capacity: usize,
    /// Total hits
    pub hits: usize,
    /// Total misses
    pub misses: usize,
    /// Total expirations
    pub expirations: usize,
    /// Hit rate (0.0 to 1.0)
    pub hit_rate: f64,
    /// TTL in seconds
    pub ttl_seconds: u64,
}

impl TtlCacheStatistics {
    /// Get the fill percentage (0.0 to 100.0)
    pub fn fill_percentage(&self) -> f64 {
        if self.capacity == 0 {
            0.0
        } else {
            (self.size as f64 / self.capacity as f64) * 100.0
        }
    }

    /// Get total accesses
    pub fn total_accesses(&self) -> usize {
        self.hits + self.misses
    }
}

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

    #[test]
    fn test_cache_creation() {
        let cache: LruModelCache<Aspect> = LruModelCache::new(10);
        assert_eq!(cache.capacity(), 10);
        assert_eq!(cache.len(), 0);
        assert!(cache.is_empty());
    }

    #[test]
    fn test_put_and_get() {
        let mut cache = LruModelCache::new(5);
        let aspect = Arc::new(Aspect::new("urn:test:1.0.0#Test".to_string()));

        cache.put("test".to_string(), Arc::clone(&aspect));
        assert_eq!(cache.len(), 1);

        let retrieved = cache.get("test");
        assert!(retrieved.is_some());
    }

    #[test]
    fn test_lru_eviction() {
        let mut cache = LruModelCache::new(3);

        // Fill cache to capacity
        cache.put(
            "a".to_string(),
            Arc::new(Aspect::new("urn:test:1.0.0#A".to_string())),
        );
        cache.put(
            "b".to_string(),
            Arc::new(Aspect::new("urn:test:1.0.0#B".to_string())),
        );
        cache.put(
            "c".to_string(),
            Arc::new(Aspect::new("urn:test:1.0.0#C".to_string())),
        );

        assert_eq!(cache.len(), 3);

        // Add one more - should evict "a" (least recently used)
        cache.put(
            "d".to_string(),
            Arc::new(Aspect::new("urn:test:1.0.0#D".to_string())),
        );

        assert_eq!(cache.len(), 3);
        assert!(!cache.contains("a")); // "a" was evicted
        assert!(cache.contains("b"));
        assert!(cache.contains("c"));
        assert!(cache.contains("d"));
    }

    #[test]
    fn test_lru_access_updates() {
        let mut cache = LruModelCache::new(3);

        cache.put(
            "a".to_string(),
            Arc::new(Aspect::new("urn:test:1.0.0#A".to_string())),
        );
        cache.put(
            "b".to_string(),
            Arc::new(Aspect::new("urn:test:1.0.0#B".to_string())),
        );
        cache.put(
            "c".to_string(),
            Arc::new(Aspect::new("urn:test:1.0.0#C".to_string())),
        );

        // Access "a" to make it recently used
        cache.get("a");

        // Add "d" - should evict "b" (now LRU)
        cache.put(
            "d".to_string(),
            Arc::new(Aspect::new("urn:test:1.0.0#D".to_string())),
        );

        assert!(cache.contains("a")); // "a" was accessed, not evicted
        assert!(!cache.contains("b")); // "b" was evicted
        assert!(cache.contains("c"));
        assert!(cache.contains("d"));
    }

    #[test]
    fn test_remove() {
        let mut cache = LruModelCache::new(5);
        cache.put(
            "test".to_string(),
            Arc::new(Aspect::new("urn:test:1.0.0#Test".to_string())),
        );

        assert_eq!(cache.len(), 1);
        let removed = cache.remove("test");
        assert!(removed.is_some());
        assert_eq!(cache.len(), 0);
    }

    #[test]
    fn test_clear() {
        let mut cache = LruModelCache::new(5);
        cache.put(
            "a".to_string(),
            Arc::new(Aspect::new("urn:test:1.0.0#A".to_string())),
        );
        cache.put(
            "b".to_string(),
            Arc::new(Aspect::new("urn:test:1.0.0#B".to_string())),
        );

        assert_eq!(cache.len(), 2);
        cache.clear();
        assert_eq!(cache.len(), 0);
        assert!(cache.is_empty());
    }

    #[test]
    fn test_hit_rate() {
        let mut cache = LruModelCache::new(5);
        cache.put(
            "test".to_string(),
            Arc::new(Aspect::new("urn:test:1.0.0#Test".to_string())),
        );

        // 2 hits
        cache.get("test");
        cache.get("test");

        // 1 miss
        cache.get("nonexistent");

        assert_eq!(cache.hits(), 2);
        assert_eq!(cache.misses(), 1);
        assert!((cache.hit_rate() - 0.666).abs() < 0.01);
    }

    #[test]
    fn test_resize() {
        let mut cache = LruModelCache::new(5);

        // Fill with 5 items
        for i in 0..5 {
            cache.put(
                format!("item{}", i),
                Arc::new(Aspect::new(format!("urn:test:1.0.0#Item{}", i))),
            );
        }

        assert_eq!(cache.len(), 5);

        // Resize to 3 - should evict 2 LRU items
        cache.resize(3);
        assert_eq!(cache.capacity(), 3);
        assert_eq!(cache.len(), 3);

        // Oldest items should be evicted
        assert!(!cache.contains("item0"));
        assert!(!cache.contains("item1"));
        assert!(cache.contains("item2"));
        assert!(cache.contains("item3"));
        assert!(cache.contains("item4"));
    }

    #[test]
    fn test_statistics() {
        let mut cache = LruModelCache::new(10);
        cache.put(
            "test".to_string(),
            Arc::new(Aspect::new("urn:test:1.0.0#Test".to_string())),
        );

        cache.get("test");
        cache.get("test");
        cache.get("nonexistent");

        let stats = cache.statistics();
        assert_eq!(stats.size, 1);
        assert_eq!(stats.capacity, 10);
        assert_eq!(stats.hits, 2);
        assert_eq!(stats.misses, 1);
        assert_eq!(stats.total_accesses(), 3);
        assert_eq!(stats.fill_percentage(), 10.0);
    }

    #[test]
    fn test_keys() {
        let mut cache = LruModelCache::new(5);
        cache.put(
            "a".to_string(),
            Arc::new(Aspect::new("urn:test:1.0.0#A".to_string())),
        );
        cache.put(
            "b".to_string(),
            Arc::new(Aspect::new("urn:test:1.0.0#B".to_string())),
        );
        cache.put(
            "c".to_string(),
            Arc::new(Aspect::new("urn:test:1.0.0#C".to_string())),
        );

        let keys = cache.keys();
        assert_eq!(keys.len(), 3);
        assert!(keys.contains(&"a".to_string()));
        assert!(keys.contains(&"b".to_string()));
        assert!(keys.contains(&"c".to_string()));
    }

    // TTL Cache Tests

    #[test]
    fn test_ttl_cache_creation() {
        let cache: TtlCache<Aspect> = TtlCache::new(10, std::time::Duration::from_secs(60));
        assert_eq!(cache.capacity(), 10);
        assert_eq!(cache.len(), 0);
        assert!(cache.is_empty());
        assert_eq!(cache.ttl().as_secs(), 60);
    }

    #[test]
    fn test_ttl_cache_put_and_get() {
        let mut cache = TtlCache::new(5, std::time::Duration::from_secs(60));
        let aspect = Arc::new(Aspect::new("urn:test:1.0.0#Test".to_string()));

        cache.put("test".to_string(), Arc::clone(&aspect));
        assert_eq!(cache.len(), 1);

        let retrieved = cache.get("test");
        assert!(retrieved.is_some());
    }

    #[test]
    fn test_ttl_cache_expiration() {
        let mut cache = TtlCache::new(5, std::time::Duration::from_millis(50));
        let aspect = Arc::new(Aspect::new("urn:test:1.0.0#Test".to_string()));

        cache.put("test".to_string(), Arc::clone(&aspect));
        assert_eq!(cache.len(), 1);

        // Item should be accessible immediately
        assert!(cache.get("test").is_some());

        // Wait for expiration
        std::thread::sleep(std::time::Duration::from_millis(100));

        // Item should be expired
        assert!(cache.get("test").is_none());
        assert_eq!(cache.expirations(), 1);
    }

    #[test]
    fn test_ttl_cache_evict_expired() {
        let mut cache = TtlCache::new(5, std::time::Duration::from_millis(50));

        // Add multiple items
        for i in 0..3 {
            cache.put(
                format!("item{}", i),
                Arc::new(Aspect::new(format!("urn:test:1.0.0#Item{}", i))),
            );
        }

        assert_eq!(cache.len(), 3);

        // Wait for expiration
        std::thread::sleep(std::time::Duration::from_millis(100));

        // Manually evict expired entries
        let evicted = cache.evict_expired();
        assert_eq!(evicted, 3);
        assert_eq!(cache.len(), 0);
    }

    #[test]
    fn test_ttl_cache_statistics() {
        let mut cache = TtlCache::new(10, std::time::Duration::from_secs(60));
        cache.put(
            "test".to_string(),
            Arc::new(Aspect::new("urn:test:1.0.0#Test".to_string())),
        );

        cache.get("test");
        cache.get("test");
        cache.get("nonexistent");

        let stats = cache.statistics();
        assert_eq!(stats.size, 1);
        assert_eq!(stats.capacity, 10);
        assert_eq!(stats.hits, 2);
        assert_eq!(stats.misses, 1);
        assert_eq!(stats.total_accesses(), 3);
        assert_eq!(stats.ttl_seconds, 60);
    }

    #[test]
    fn test_ttl_cache_clear() {
        let mut cache = TtlCache::new(5, std::time::Duration::from_secs(60));
        cache.put(
            "a".to_string(),
            Arc::new(Aspect::new("urn:test:1.0.0#A".to_string())),
        );
        cache.put(
            "b".to_string(),
            Arc::new(Aspect::new("urn:test:1.0.0#B".to_string())),
        );

        assert_eq!(cache.len(), 2);
        cache.clear();
        assert_eq!(cache.len(), 0);
        assert!(cache.is_empty());
    }

    #[test]
    fn test_ttl_cache_lru_eviction() {
        let mut cache = TtlCache::new(3, std::time::Duration::from_secs(60));

        // Fill cache to capacity
        cache.put(
            "a".to_string(),
            Arc::new(Aspect::new("urn:test:1.0.0#A".to_string())),
        );
        cache.put(
            "b".to_string(),
            Arc::new(Aspect::new("urn:test:1.0.0#B".to_string())),
        );
        cache.put(
            "c".to_string(),
            Arc::new(Aspect::new("urn:test:1.0.0#C".to_string())),
        );

        assert_eq!(cache.len(), 3);

        // Add one more - should evict "a" (LRU)
        cache.put(
            "d".to_string(),
            Arc::new(Aspect::new("urn:test:1.0.0#D".to_string())),
        );

        assert_eq!(cache.len(), 3);
        // Note: Cannot easily check which was evicted without accessing cache
        // Just verify size is maintained
    }
}