simple-cacher 0.1.1

A high-performance, flexible caching library with custom matching capabilities and automatic expiration
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
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
//! # Simple Cacher
//!
//! A high-performance, flexible caching library for Rust with custom matching capabilities
//! and automatic expiration.
//!
//! ## Key Features
//!
//! - **Fast O(1) exact key lookups** using IndexMap
//! - **Custom pattern matching** via the `Matcher<T>` trait
//! - **Automatic expiration** with configurable TTL per entry
//! - **Size-limited caches** with FIFO eviction (oldest entries removed first)
//! - **Lazy cleanup** - expired entries removed on access
//! - **Zero-copy value access** through references
//!
//! ## Quick Start
//!
//! ```rust
//! use simple_cacher::*;
//! use std::time::Duration;
//!
//! // Create a cache with 5-minute TTL
//! let mut cache = SimpleCacher::new(Duration::from_secs(300));
//!
//! // Insert data
//! cache.insert("user:123".to_string(), "Alice".to_string());
//!
//! // Retrieve data
//! match cache.get(&"user:123".to_string()) {
//!     Ok(entry) => println!("Found: {}", entry.value()),
//!     Err(SimpleCacheError::NotFound) => println!("Not found"),
//!     Err(SimpleCacheError::Expired) => println!("Expired"),
//! }
//! ```
//!
//! ## Custom Matching
//!
//! ```rust
//! use simple_cacher::*;
//! use std::time::Duration;
//!
//! let mut cache = SimpleCacher::new(Duration::from_secs(300));
//! cache.insert("user:alice".to_string(), "Alice Johnson".to_string());
//! cache.insert("admin:bob".to_string(), "Bob Admin".to_string());
//!
//! // Find by prefix
//! let user_matcher = PrefixMatcher::new("user:");
//! if let Ok(user) = cache.get_by_matcher(&user_matcher) {
//!     println!("Found user: {}", user.value());
//! }
//! ```

use indexmap::IndexMap;
use std::time::{Duration, Instant};

/// Error types returned by cache operations.
#[derive(Debug, Clone, PartialEq)]
pub enum SimpleCacheError {
    /// The requested key was not found in the cache.
    NotFound,
    /// The entry was found but has expired and was automatically removed.
    Expired,
}

impl std::fmt::Display for SimpleCacheError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SimpleCacheError::NotFound => write!(f, "Cache entry not found"),
            SimpleCacheError::Expired => write!(f, "Cache entry has expired"),
        }
    }
}

impl std::error::Error for SimpleCacheError {}

/// A cached value with metadata about its creation time and expiration.
///
/// This struct wraps the actual cached value along with timing information
/// to support automatic expiration and cache management.
///
/// # Examples
///
/// ```rust
/// use simple_cacher::*;
/// use std::time::Duration;
///
/// let mut cache = SimpleCacher::new(Duration::from_secs(60));
/// cache.insert("key".to_string(), "value".to_string());
///
/// if let Ok(entry) = cache.get(&"key".to_string()) {
///     println!("Value: {}", entry.value());
///     println!("Age: {:?}", entry.age());
///     println!("Expired: {}", entry.is_expired());
/// }
/// ```
#[derive(Debug, Clone)]
pub struct SimpleCacheObject<U> {
    created_at: Instant,
    value: U,
    max_age: Duration,
}

impl<U> SimpleCacheObject<U> {
    /// Creates a new cache object with the given value and maximum age.
    fn new(value: U, max_age: Duration) -> Self {
        Self {
            created_at: Instant::now(),
            value,
            max_age,
        }
    }

    /// Returns `true` if this cache entry has expired based on its max age.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use simple_cacher::*;
    /// use std::time::Duration;
    ///
    /// let mut cache = SimpleCacher::new(Duration::from_millis(100));
    /// cache.insert("key".to_string(), "value".to_string());
    ///
    /// if let Ok(entry) = cache.get(&"key".to_string()) {
    ///     // Should not be expired immediately
    ///     assert!(!entry.is_expired());
    /// }
    /// ```
    pub fn is_expired(&self) -> bool {
        self.created_at.elapsed() > self.max_age
    }

    /// Returns a reference to the cached value.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use simple_cacher::*;
    /// use std::time::Duration;
    ///
    /// let mut cache = SimpleCacher::new(Duration::from_secs(60));
    /// cache.insert("greeting".to_string(), "Hello, World!".to_string());
    ///
    /// if let Ok(entry) = cache.get(&"greeting".to_string()) {
    ///     assert_eq!(entry.value(), "Hello, World!");
    /// }
    /// ```
    pub fn value(&self) -> &U {
        &self.value
    }

    /// Returns a mutable reference to the cached value.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use simple_cacher::*;
    /// use std::time::Duration;
    ///
    /// let mut cache = SimpleCacher::new(Duration::from_secs(60));
    /// cache.insert("counter".to_string(), 0u32);
    ///
    /// if let Ok(entry) = cache.get_mut(&"counter".to_string()) {
    ///     *entry.value_mut() += 1;
    ///     assert_eq!(*entry.value(), 1);
    /// }
    /// ```
    pub fn value_mut(&mut self) -> &mut U {
        &mut self.value
    }

    /// Consumes the cache object and returns the cached value.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use simple_cacher::*;
    /// use std::time::Duration;
    ///
    /// let mut cache = SimpleCacher::new(Duration::from_secs(60));
    /// cache.insert("data".to_string(), vec![1, 2, 3]);
    ///
    /// if let Some(entry) = cache.remove(&"data".to_string()) {
    ///     let data = entry.into_value();
    ///     assert_eq!(data, vec![1, 2, 3]);
    /// }
    /// ```
    pub fn into_value(self) -> U {
        self.value
    }

    /// Returns the age of this cache entry (time since creation).
    ///
    /// # Examples
    ///
    /// ```rust
    /// use simple_cacher::*;
    /// use std::time::Duration;
    ///
    /// let mut cache = SimpleCacher::new(Duration::from_secs(60));
    /// cache.insert("key".to_string(), "value".to_string());
    ///
    /// if let Ok(entry) = cache.get(&"key".to_string()) {
    ///     let age = entry.age();
    ///     assert!(age.as_millis() >= 0);
    /// }
    /// ```
    pub fn age(&self) -> Duration {
        self.created_at.elapsed()
    }

    /// Returns the instant when this entry was created.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use simple_cacher::*;
    /// use std::time::{Duration, Instant};
    ///
    /// let mut cache = SimpleCacher::new(Duration::from_secs(60));
    /// let before = Instant::now();
    /// cache.insert("key".to_string(), "value".to_string());
    /// let after = Instant::now();
    ///
    /// if let Ok(entry) = cache.get(&"key".to_string()) {
    ///     let created = entry.created_at();
    ///     assert!(created >= before && created <= after);
    /// }
    /// ```
    pub fn created_at(&self) -> Instant {
        self.created_at
    }
}

/// Trait for implementing custom matching logic against cache keys.
///
/// This trait allows you to define complex search patterns for finding cached entries
/// beyond simple exact key matching. Implementations can match based on patterns,
/// ranges, regular expressions, or any custom logic.
///
/// # Examples
///
/// ```rust
/// use simple_cacher::*;
/// use std::time::Duration;
///
/// // Custom matcher for email domains
/// struct DomainMatcher {
///     domain: String,
/// }
///
/// impl Matcher<String> for DomainMatcher {
///     fn matches(&self, email: &String) -> bool {
///         email.ends_with(&format!("@{}", self.domain))
///     }
/// }
///
/// let mut cache = SimpleCacher::new(Duration::from_secs(300));
/// cache.insert("alice@company.com".to_string(), "Alice".to_string());
/// cache.insert("bob@company.com".to_string(), "Bob".to_string());
/// cache.insert("charlie@gmail.com".to_string(), "Charlie".to_string());
///
/// let company_matcher = DomainMatcher { domain: "company.com".to_string() };
/// let company_users = cache.get_all_by_matcher(&company_matcher);
/// assert_eq!(company_users.len(), 2);
/// ```
pub trait Matcher<T> {
    /// Returns `true` if the given key matches this matcher's criteria.
    ///
    /// # Arguments
    ///
    /// * `key` - The cache key to test against this matcher
    ///
    /// # Returns
    ///
    /// `true` if the key matches, `false` otherwise
    fn matches(&self, key: &T) -> bool;
}

/// A high-performance cache with automatic expiration and custom matching capabilities.
///
/// `SimpleCacher` provides fast O(1) exact key lookups using an IndexMap, along with
/// flexible O(n) pattern matching via the `Matcher` trait. Entries automatically expire
/// based on configurable TTL values, and the cache can be size-limited with FIFO eviction.
///
/// # Type Parameters
///
/// * `T` - The type of keys (must implement `Clone + Eq + Hash`)
/// * `U` - The type of cached values
///
/// # Examples
///
/// ## Basic Usage
///
/// ```rust
/// use simple_cacher::*;
/// use std::time::Duration;
///
/// let mut cache = SimpleCacher::new(Duration::from_secs(300));
///
/// // Insert a value
/// cache.insert("user:123".to_string(), "Alice Johnson".to_string());
///
/// // Retrieve it
/// match cache.get(&"user:123".to_string()) {
///     Ok(entry) => println!("Found: {}", entry.value()),
///     Err(SimpleCacheError::NotFound) => println!("Not found"),
///     Err(SimpleCacheError::Expired) => println!("Expired and removed"),
/// }
/// ```
///
/// ## Size-Limited Cache
///
/// ```rust
/// use simple_cacher::*;
/// use std::time::Duration;
///
/// // Cache with max 1000 entries
/// let mut cache = SimpleCacher::with_max_size(Duration::from_secs(300), 1000);
///
/// // When full, oldest entries are automatically removed
/// for i in 0..1500 {
///     cache.insert(format!("key_{}", i), format!("value_{}", i));
/// }
///
/// assert_eq!(cache.len(), 1000); // Only newest 1000 entries remain
/// ```
#[derive(Debug, Clone)]
pub struct SimpleCacher<T, U> {
    cache: IndexMap<T, SimpleCacheObject<U>>,
    max_age: Duration,
    max_size: Option<usize>,
}

impl<T, U> SimpleCacher<T, U>
where
    T: Clone + Eq + std::hash::Hash,
{
    /// Creates a new cache with the specified maximum age for entries.
    ///
    /// All entries inserted into this cache will expire after the given duration,
    /// unless overridden with `insert_with_ttl`.
    ///
    /// # Arguments
    ///
    /// * `max_age` - Default time-to-live for cache entries
    ///
    /// # Examples
    ///
    /// ```rust
    /// use simple_cacher::*;
    /// use std::time::Duration;
    ///
    /// let mut cache = SimpleCacher::new(Duration::from_secs(300)); // 5 minutes
    /// cache.insert("key".to_string(), "value".to_string());
    /// ```
    pub fn new(max_age: Duration) -> Self {
        Self {
            cache: IndexMap::new(),
            max_age,
            max_size: None,
        }
    }

    /// Creates a new cache with both maximum age and maximum size constraints.
    ///
    /// When the cache exceeds `max_size` entries, the oldest entries are automatically
    /// removed to make room for new ones (FIFO eviction policy).
    ///
    /// # Arguments
    ///
    /// * `max_age` - Default time-to-live for cache entries
    /// * `max_size` - Maximum number of entries to keep in the cache
    ///
    /// # Examples
    ///
    /// ```rust
    /// use simple_cacher::*;
    /// use std::time::Duration;
    ///
    /// let mut cache: SimpleCacher<String,String> = SimpleCacher::with_max_size(
    ///     Duration::from_secs(300), // 5 minutes TTL
    ///     1000 // max 1000 entries
    /// );
    /// ```
    pub fn with_max_size(max_age: Duration, max_size: usize) -> Self {
        Self {
            cache: IndexMap::new(),
            max_age,
            max_size: Some(max_size),
        }
    }

    /// Retrieves an entry by exact key match in O(1) time.
    ///
    /// If the entry exists but has expired, it will be automatically removed
    /// from the cache and `SimpleCacheError::Expired` will be returned.
    ///
    /// # Arguments
    ///
    /// * `key` - The key to look up
    ///
    /// # Returns
    ///
    /// * `Ok(&SimpleCacheObject<U>)` - The cached entry if found and not expired
    /// * `Err(SimpleCacheError::NotFound)` - The key doesn't exist
    /// * `Err(SimpleCacheError::Expired)` - The entry existed but expired (now removed)
    ///
    /// # Examples
    ///
    /// ```rust
    /// use simple_cacher::*;
    /// use std::time::Duration;
    ///
    /// let mut cache = SimpleCacher::new(Duration::from_secs(60));
    /// cache.insert("user:123".to_string(), "Alice".to_string());
    ///
    /// match cache.get(&"user:123".to_string()) {
    ///     Ok(entry) => {
    ///         println!("Found: {}", entry.value());
    ///         println!("Age: {:?}", entry.age());
    ///     }
    ///     Err(SimpleCacheError::NotFound) => println!("User not found"),
    ///     Err(SimpleCacheError::Expired) => println!("User data expired"),
    /// }
    /// ```
    pub fn get(&mut self, key: &T) -> Result<&SimpleCacheObject<U>, SimpleCacheError> {
        // Check if entry exists and if it's expired
        let should_remove = match self.cache.get(key) {
            Some(obj) => obj.is_expired(),
            None => return Err(SimpleCacheError::NotFound),
        };

        if should_remove {
            self.cache.shift_remove(key);
            return Err(SimpleCacheError::Expired);
        }

        // Safe to get immutable reference now
        Ok(self.cache.get(key).unwrap())
    }

    /// Retrieves a mutable reference to an entry by exact key match.
    ///
    /// Similar to `get()`, but returns a mutable reference that allows you to modify
    /// the cached value in place. Expired entries are automatically removed.
    ///
    /// # Arguments
    ///
    /// * `key` - The key to look up
    ///
    /// # Returns
    ///
    /// * `Ok(&mut SimpleCacheObject<U>)` - Mutable reference to the cached entry
    /// * `Err(SimpleCacheError::NotFound)` - The key doesn't exist
    /// * `Err(SimpleCacheError::Expired)` - The entry existed but expired (now removed)
    ///
    /// # Examples
    ///
    /// ```rust
    /// use simple_cacher::*;
    /// use std::time::Duration;
    ///
    /// let mut cache = SimpleCacher::new(Duration::from_secs(60));
    /// cache.insert("counter".to_string(), 0u32);
    ///
    /// if let Ok(entry) = cache.get_mut(&"counter".to_string()) {
    ///     *entry.value_mut() += 1;
    ///     assert_eq!(*entry.value(), 1);
    /// }
    /// ```
    pub fn get_mut(&mut self, key: &T) -> Result<&mut SimpleCacheObject<U>, SimpleCacheError> {
        // Check if exists and if it's expired first
        let should_remove = match self.cache.get(key) {
            Some(obj) => obj.is_expired(),
            None => return Err(SimpleCacheError::NotFound),
        };

        if should_remove {
            self.cache.shift_remove(key);
            return Err(SimpleCacheError::Expired);
        }

        // Safe to get mutable reference now
        Ok(self.cache.get_mut(key).unwrap())
    }

    /// Finds the first entry matching the given matcher in O(n) time.
    ///
    /// This method iterates through all cache entries and returns the first one
    /// that matches the provided matcher's criteria. Expired entries encountered
    /// during the search are automatically cleaned up.
    ///
    /// # Arguments
    ///
    /// * `matcher` - An implementation of `Matcher<T>` that defines the search criteria
    ///
    /// # Returns
    ///
    /// * `Ok(&SimpleCacheObject<U>)` - The first matching entry found
    /// * `Err(SimpleCacheError::NotFound)` - No matching entries found
    ///
    /// # Examples
    ///
    /// ```rust
    /// use simple_cacher::*;
    /// use std::time::Duration;
    ///
    /// let mut cache = SimpleCacher::new(Duration::from_secs(300));
    /// cache.insert("user:alice".to_string(), "Alice".to_string());
    /// cache.insert("user:bob".to_string(), "Bob".to_string());
    /// cache.insert("admin:charlie".to_string(), "Charlie".to_string());
    ///
    /// let user_matcher = PrefixMatcher::new("user:");
    /// if let Ok(user) = cache.get_by_matcher(&user_matcher) {
    ///     println!("Found user: {}", user.value());
    /// }
    /// ```
    pub fn get_by_matcher<M>(
        &mut self,
        matcher: &M,
    ) -> Result<&SimpleCacheObject<U>, SimpleCacheError>
    where
        M: Matcher<T>,
    {
        // First pass: collect expired keys and find match
        let mut expired_keys = Vec::new();
        let mut found_key = None;

        for (key, obj) in &self.cache {
            if obj.is_expired() {
                expired_keys.push(key.clone());
            } else if found_key.is_none() && matcher.matches(key) {
                found_key = Some(key.clone());
            }
        }

        // Clean up expired entries
        for key in expired_keys {
            self.cache.shift_remove(&key);
        }

        // Return the found entry (get fresh reference after cleanup)
        if let Some(key) = found_key {
            self.cache.get(&key).ok_or(SimpleCacheError::NotFound)
        } else {
            Err(SimpleCacheError::NotFound)
        }
    }

    /// Finds all entries matching the given matcher.
    ///
    /// This method returns all cache entries that match the provided matcher's criteria.
    /// The cache is automatically cleaned of expired entries before searching.
    ///
    /// # Arguments
    ///
    /// * `matcher` - An implementation of `Matcher<T>` that defines the search criteria
    ///
    /// # Returns
    ///
    /// A vector of tuples containing references to matching keys and their cached values.
    /// The vector may be empty if no matches are found.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use simple_cacher::*;
    /// use std::time::Duration;
    ///
    /// let mut cache = SimpleCacher::new(Duration::from_secs(300));
    /// cache.insert("user:alice".to_string(), "Alice".to_string());
    /// cache.insert("user:bob".to_string(), "Bob".to_string());
    /// cache.insert("admin:charlie".to_string(), "Charlie".to_string());
    ///
    /// let user_matcher = PrefixMatcher::new("user:");
    /// let users = cache.get_all_by_matcher(&user_matcher);
    /// println!("Found {} users", users.len());
    /// ```
    pub fn get_all_by_matcher<M>(&mut self, matcher: &M) -> Vec<(&T, &SimpleCacheObject<U>)>
    where
        M: Matcher<T>,
    {
        // Clean up expired entries first
        self.cleanup_expired();

        self.cache
            .iter()
            .filter(|(key, obj)| !obj.is_expired() && matcher.matches(key))
            .collect()
    }

    /// Inserts a new entry into the cache with the default TTL.
    ///
    /// If the cache has a size limit and is at capacity, the oldest entry
    /// will be automatically removed to make room for the new entry (FIFO eviction).
    /// If an entry with the same key already exists, it will be replaced.
    ///
    /// # Arguments
    ///
    /// * `key` - The key to associate with the value
    /// * `value` - The value to cache
    ///
    /// # Examples
    ///
    /// ```rust
    /// use simple_cacher::*;
    /// use std::time::Duration;
    ///
    /// let mut cache = SimpleCacher::new(Duration::from_secs(300));
    /// cache.insert("user:123".to_string(), "Alice Johnson".to_string());
    /// ```
    pub fn insert(&mut self, key: T, value: U) {
        // Enforce max size by removing oldest entries (FIFO)
        if let Some(max_size) = self.max_size {
            while self.cache.len() >= max_size {
                self.cache.shift_remove_index(0);
            }
        }

        let cache_obj = SimpleCacheObject::new(value, self.max_age);
        self.cache.insert(key, cache_obj);
    }

    /// Inserts a new entry into the cache with a custom TTL.
    ///
    /// This allows you to override the default TTL for specific entries,
    /// useful for caching data with different freshness requirements.
    ///
    /// # Arguments
    ///
    /// * `key` - The key to associate with the value
    /// * `value` - The value to cache
    /// * `ttl` - Custom time-to-live for this specific entry
    ///
    /// # Examples
    ///
    /// ```rust
    /// use simple_cacher::*;
    /// use std::time::Duration;
    ///
    /// let mut cache = SimpleCacher::new(Duration::from_secs(300)); // Default 5 min
    ///
    /// // Cache with custom 1-hour TTL
    /// cache.insert_with_ttl(
    ///     "important_data".to_string(),
    ///     "critical information".to_string(),
    ///     Duration::from_secs(3600)
    /// );
    /// ```
    pub fn insert_with_ttl(&mut self, key: T, value: U, ttl: Duration) {
        if let Some(max_size) = self.max_size {
            while self.cache.len() >= max_size {
                self.cache.shift_remove_index(0);
            }
        }

        let cache_obj = SimpleCacheObject::new(value, ttl);
        self.cache.insert(key, cache_obj);
    }

    /// Removes an entry by key and returns it if it existed.
    ///
    /// This method removes the entry regardless of whether it has expired.
    /// Returns `None` if the key doesn't exist.
    ///
    /// # Arguments
    ///
    /// * `key` - The key of the entry to remove
    ///
    /// # Returns
    ///
    /// `Some(SimpleCacheObject<U>)` if the key existed, `None` otherwise
    ///
    /// # Examples
    ///
    /// ```rust
    /// use simple_cacher::*;
    /// use std::time::Duration;
    ///
    /// let mut cache = SimpleCacher::new(Duration::from_secs(300));
    /// cache.insert("temp_data".to_string(), "temporary".to_string());
    ///
    /// if let Some(removed) = cache.remove(&"temp_data".to_string()) {
    ///     println!("Removed: {}", removed.into_value());
    /// }
    /// ```
    pub fn remove(&mut self, key: &T) -> Option<SimpleCacheObject<U>> {
        self.cache.shift_remove(key)
    }

    /// Checks if a key exists in the cache and is not expired.
    ///
    /// This is a lightweight check that doesn't trigger cleanup of expired entries.
    ///
    /// # Arguments
    ///
    /// * `key` - The key to check for existence
    ///
    /// # Returns
    ///
    /// `true` if the key exists and is not expired, `false` otherwise
    ///
    /// # Examples
    ///
    /// ```rust
    /// use simple_cacher::*;
    /// use std::time::Duration;
    ///
    /// let mut cache = SimpleCacher::new(Duration::from_secs(300));
    /// cache.insert("key".to_string(), "value".to_string());
    ///
    /// assert!(cache.contains_key(&"key".to_string()));
    /// assert!(!cache.contains_key(&"nonexistent".to_string()));
    /// ```
    pub fn contains_key(&self, key: &T) -> bool {
        self.cache
            .get(key)
            .map(|obj| !obj.is_expired())
            .unwrap_or(false)
    }

    /// Manually removes all expired entries from the cache.
    ///
    /// This method performs a full scan of the cache and removes all entries
    /// that have exceeded their TTL. This can be useful for periodic cleanup
    /// to free memory and maintain cache performance.
    ///
    /// # Returns
    ///
    /// The number of expired entries that were removed
    ///
    /// # Examples
    ///
    /// ```rust
    /// use simple_cacher::*;
    /// use std::time::Duration;
    ///
    /// let mut cache = SimpleCacher::new(Duration::from_millis(100));
    /// cache.insert("key1".to_string(), "value1".to_string());
    /// cache.insert("key2".to_string(), "value2".to_string());
    ///
    /// // Wait for expiration
    /// std::thread::sleep(Duration::from_millis(150));
    ///
    /// let removed = cache.cleanup_expired();
    /// println!("Cleaned up {} expired entries", removed);
    /// ```
    pub fn cleanup_expired(&mut self) -> usize {
        let expired_keys: Vec<T> = self
            .cache
            .iter()
            .filter_map(|(k, v)| {
                if v.is_expired() {
                    Some(k.clone())
                } else {
                    None
                }
            })
            .collect();

        let count = expired_keys.len();
        for key in expired_keys {
            self.cache.shift_remove(&key);
        }
        count
    }

    /// Returns the total number of entries in the cache (including expired ones).
    ///
    /// Note that this includes expired entries that haven't been cleaned up yet.
    /// Use `active_len()` to get only non-expired entries.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use simple_cacher::*;
    /// use std::time::Duration;
    ///
    /// let mut cache = SimpleCacher::new(Duration::from_secs(300));
    /// cache.insert("key1".to_string(), "value1".to_string());
    /// cache.insert("key2".to_string(), "value2".to_string());
    ///
    /// assert_eq!(cache.len(), 2);
    /// ```
    pub fn len(&self) -> usize {
        self.cache.len()
    }

    /// Returns the number of non-expired entries in the cache.
    ///
    /// This method counts only entries that are still valid (not expired).
    /// It does not modify the cache or remove expired entries.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use simple_cacher::*;
    /// use std::time::Duration;
    ///
    /// let mut cache = SimpleCacher::new(Duration::from_secs(300));
    /// cache.insert("key1".to_string(), "value1".to_string());
    /// cache.insert("key2".to_string(), "value2".to_string());
    ///
    /// assert_eq!(cache.active_len(), 2);
    /// ```
    pub fn active_len(&self) -> usize {
        self.cache
            .iter()
            .filter(|(_, obj)| !obj.is_expired())
            .count()
    }

    /// Returns `true` if the cache contains no entries.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use simple_cacher::*;
    /// use std::time::Duration;
    ///
    /// let cache = SimpleCacher::<String, String>::new(Duration::from_secs(300));
    /// assert!(cache.is_empty());
    /// ```
    pub fn is_empty(&self) -> bool {
        self.cache.is_empty()
    }

    /// Removes all entries from the cache.
    ///
    /// After calling this method, the cache will be empty and `len()` will return 0.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use simple_cacher::*;
    /// use std::time::Duration;
    ///
    /// let mut cache = SimpleCacher::new(Duration::from_secs(300));
    /// cache.insert("key".to_string(), "value".to_string());
    /// assert_eq!(cache.len(), 1);
    ///
    /// cache.clear();
    /// assert_eq!(cache.len(), 0);
    /// ```
    pub fn clear(&mut self) {
        self.cache.clear();
    }

    /// Returns comprehensive statistics about the cache state.
    ///
    /// This provides detailed information about cache usage, including total entries,
    /// active (non-expired) entries, expired entries, and configuration settings.
    ///
    /// # Returns
    ///
    /// A `CacheStats` struct containing cache metrics
    ///
    /// # Examples
    ///
    /// ```rust
    /// use simple_cacher::*;
    /// use std::time::Duration;
    ///
    /// let mut cache = SimpleCacher::with_max_size(Duration::from_secs(300), 1000);
    /// cache.insert("key1".to_string(), "value1".to_string());
    /// cache.insert("key2".to_string(), "value2".to_string());
    ///
    /// let stats = cache.stats();
    /// println!("Active entries: {}", stats.active_entries);
    /// println!("Max size: {:?}", stats.max_size);
    /// ```
    pub fn stats(&self) -> CacheStats {
        let total = self.cache.len();
        let expired = self
            .cache
            .iter()
            .filter(|(_, obj)| obj.is_expired())
            .count();

        CacheStats {
            total_entries: total,
            active_entries: total - expired,
            expired_entries: expired,
            max_size: self.max_size,
            max_age: self.max_age,
        }
    }

    /// Returns an iterator over all non-expired entries in the cache.
    ///
    /// This iterator yields tuples of `(&T, &SimpleCacheObject<U>)` for each
    /// active (non-expired) entry. Expired entries are skipped.
    ///
    /// # Returns
    ///
    /// An iterator over active cache entries
    ///
    /// # Examples
    ///
    /// ```rust
    /// use simple_cacher::*;
    /// use std::time::Duration;
    ///
    /// let mut cache = SimpleCacher::new(Duration::from_secs(300));
    /// cache.insert("user:1".to_string(), "Alice".to_string());
    /// cache.insert("user:2".to_string(), "Bob".to_string());
    ///
    /// for (key, entry) in cache.iter_active() {
    ///     println!("{}: {} (age: {:?})", key, entry.value(), entry.age());
    /// }
    /// ```
    pub fn iter_active(&self) -> impl Iterator<Item = (&T, &SimpleCacheObject<U>)> {
        self.cache.iter().filter(|(_, obj)| !obj.is_expired())
    }
}

/// Statistics about cache state and performance.
///
/// This struct provides detailed metrics about cache usage, including
/// the number of active and expired entries, size limits, and TTL settings.
#[derive(Debug, Clone)]
pub struct CacheStats {
    /// Total number of entries in the cache (including expired)
    pub total_entries: usize,
    /// Number of non-expired entries
    pub active_entries: usize,
    /// Number of expired entries (not yet cleaned up)
    pub expired_entries: usize,
    /// Maximum number of entries allowed (None if unlimited)
    pub max_size: Option<usize>,
    /// Default time-to-live for new entries
    pub max_age: Duration,
}

// ========== Built-in Matchers ==========

/// Exact equality matcher for cache keys.
///
/// This matcher performs exact equality comparison, similar to using `get()` directly,
/// but is useful in generic code where you need a `Matcher` implementation.
///
/// # Examples
///
/// ```rust
/// use simple_cacher::*;
/// use std::time::Duration;
///
/// let mut cache = SimpleCacher::new(Duration::from_secs(300));
/// cache.insert("exact_key".to_string(), "value".to_string());
///
/// let matcher = ExactMatcher::new("exact_key".to_string());
/// if let Ok(entry) = cache.get_by_matcher(&matcher) {
///     println!("Found: {}", entry.value());
/// }
/// ```
pub struct ExactMatcher<T> {
    target: T,
}

impl<T> ExactMatcher<T> {
    /// Creates a new exact matcher for the given target value.
    ///
    /// # Arguments
    ///
    /// * `target` - The exact value to match against
    pub fn new(target: T) -> Self {
        Self { target }
    }
}

impl<T> Matcher<T> for ExactMatcher<T>
where
    T: PartialEq,
{
    fn matches(&self, key: &T) -> bool {
        key == &self.target
    }
}

/// String prefix matcher for finding keys that start with a specific string.
///
/// This matcher is useful for finding groups of related cache entries that
/// follow a naming convention with common prefixes.
///
/// # Examples
///
/// ```rust
/// use simple_cacher::*;
/// use std::time::Duration;
///
/// let mut cache = SimpleCacher::new(Duration::from_secs(300));
/// cache.insert("user:alice".to_string(), "Alice Johnson".to_string());
/// cache.insert("user:bob".to_string(), "Bob Smith".to_string());
/// cache.insert("admin:charlie".to_string(), "Charlie Admin".to_string());
///
/// let user_matcher = PrefixMatcher::new("user:");
/// let users = cache.get_all_by_matcher(&user_matcher);
/// assert_eq!(users.len(), 2); // Found alice and bob
/// ```
pub struct PrefixMatcher {
    prefix: String,
}

impl PrefixMatcher {
    /// Creates a new prefix matcher.
    ///
    /// # Arguments
    ///
    /// * `prefix` - The prefix string to match against
    pub fn new(prefix: impl Into<String>) -> Self {
        Self {
            prefix: prefix.into(),
        }
    }
}

impl Matcher<String> for PrefixMatcher {
    fn matches(&self, key: &String) -> bool {
        key.starts_with(&self.prefix)
    }
}

impl Matcher<&str> for PrefixMatcher {
    fn matches(&self, key: &&str) -> bool {
        key.starts_with(&self.prefix)
    }
}

/// String suffix matcher for finding keys that end with a specific string.
///
/// This matcher is useful for finding cache entries based on file extensions,
/// domain names, or other suffix-based patterns.
///
/// # Examples
///
/// ```rust
/// use simple_cacher::*;
/// use std::time::Duration;
///
/// let mut cache = SimpleCacher::new(Duration::from_secs(300));
/// cache.insert("document.pdf".to_string(), "PDF content".to_string());
/// cache.insert("image.jpg".to_string(), "JPEG data".to_string());
/// cache.insert("script.js".to_string(), "JavaScript code".to_string());
///
/// let pdf_matcher = SuffixMatcher::new(".pdf");
/// let pdfs = cache.get_all_by_matcher(&pdf_matcher);
/// assert_eq!(pdfs.len(), 1);
/// ```
pub struct SuffixMatcher {
    suffix: String,
}

impl SuffixMatcher {
    /// Creates a new suffix matcher.
    ///
    /// # Arguments
    ///
    /// * `suffix` - The suffix string to match against
    pub fn new(suffix: impl Into<String>) -> Self {
        Self {
            suffix: suffix.into(),
        }
    }
}

impl Matcher<String> for SuffixMatcher {
    fn matches(&self, key: &String) -> bool {
        key.ends_with(&self.suffix)
    }
}

impl Matcher<&str> for SuffixMatcher {
    fn matches(&self, key: &&str) -> bool {
        key.ends_with(&self.suffix)
    }
}

/// String substring matcher for finding keys that contain a specific string.
///
/// This matcher searches for cache entries where the key contains the specified
/// substring anywhere within it.
///
/// # Examples
///
/// ```rust
/// use simple_cacher::*;
/// use std::time::Duration;
///
/// let mut cache = SimpleCacher::new(Duration::from_secs(300));
/// cache.insert("user_profile_123".to_string(), "Profile data".to_string());
/// cache.insert("user_settings_456".to_string(), "Settings data".to_string());
/// cache.insert("admin_config".to_string(), "Config data".to_string());
///
/// let profile_matcher = ContainsMatcher::new("profile");
/// let profiles = cache.get_all_by_matcher(&profile_matcher);
/// assert_eq!(profiles.len(), 1);
/// ```
pub struct ContainsMatcher {
    substring: String,
}

impl ContainsMatcher {
    /// Creates a new substring matcher.
    ///
    /// # Arguments
    ///
    /// * `substring` - The substring to search for within keys
    pub fn new(substring: impl Into<String>) -> Self {
        Self {
            substring: substring.into(),
        }
    }
}

impl Matcher<String> for ContainsMatcher {
    fn matches(&self, key: &String) -> bool {
        key.contains(&self.substring)
    }
}

impl Matcher<&str> for ContainsMatcher {
    fn matches(&self, key: &&str) -> bool {
        key.contains(&self.substring)
    }
}

/// Numeric range matcher for finding keys within a specified range.
///
/// This matcher is useful for numeric keys like IDs, scores, timestamps,
/// or any other ordered numeric data.
///
/// # Examples
///
/// ```rust
/// use simple_cacher::*;
/// use std::time::Duration;
///
/// let mut cache = SimpleCacher::new(Duration::from_secs(300));
/// cache.insert(85, "Good score".to_string());
/// cache.insert(92, "Excellent score".to_string());
/// cache.insert(67, "Average score".to_string());
/// cache.insert(45, "Poor score".to_string());
///
/// let high_score_matcher = RangeMatcher::new(80, 100);
/// let high_scores = cache.get_all_by_matcher(&high_score_matcher);
/// assert_eq!(high_scores.len(), 2); // 85 and 92
/// ```
pub struct RangeMatcher<T> {
    min: T,
    max: T,
    inclusive: bool,
}

impl<T> RangeMatcher<T> {
    /// Creates a new inclusive range matcher.
    ///
    /// # Arguments
    ///
    /// * `min` - Minimum value (inclusive)
    /// * `max` - Maximum value (inclusive)
    pub fn new(min: T, max: T) -> Self {
        Self {
            min,
            max,
            inclusive: true,
        }
    }

    /// Creates a new exclusive range matcher.
    ///
    /// # Arguments
    ///
    /// * `min` - Minimum value (exclusive)
    /// * `max` - Maximum value (exclusive)
    pub fn exclusive(min: T, max: T) -> Self {
        Self {
            min,
            max,
            inclusive: false,
        }
    }
}

impl<T> Matcher<T> for RangeMatcher<T>
where
    T: PartialOrd,
{
    fn matches(&self, key: &T) -> bool {
        if self.inclusive {
            key >= &self.min && key <= &self.max
        } else {
            key > &self.min && key < &self.max
        }
    }
}

/// Function-based matcher for maximum flexibility in matching logic.
///
/// This matcher allows you to provide a custom function that determines
/// whether a key matches. This is the most flexible matcher and can implement
/// any matching logic you need.
///
/// # Examples
///
/// ```rust
/// use simple_cacher::*;
/// use std::time::Duration;
///
/// let mut cache = SimpleCacher::new(Duration::from_secs(300));
/// cache.insert(2, "Even number".to_string());
/// cache.insert(3, "Odd number".to_string());
/// cache.insert(4, "Even number".to_string());
/// cache.insert(5, "Odd number".to_string());
///
/// // Find even numbers
/// let even_matcher = FnMatcher::new(|&key: &i32| key % 2 == 0);
/// let even_numbers = cache.get_all_by_matcher(&even_matcher);
/// assert_eq!(even_numbers.len(), 2); // 2 and 4
/// ```
pub struct FnMatcher<T, F>
where
    F: Fn(&T) -> bool,
{
    matcher_fn: F,
    _phantom: std::marker::PhantomData<T>,
}

impl<T, F> FnMatcher<T, F>
where
    F: Fn(&T) -> bool,
{
    /// Creates a new function-based matcher.
    ///
    /// # Arguments
    ///
    /// * `matcher_fn` - A function that takes a key reference and returns `true` if it matches
    ///
    /// # Examples
    ///
    /// ```rust
    /// use simple_cacher::*;
    ///
    /// // Match strings longer than 5 characters
    /// let long_string_matcher = FnMatcher::new(|s: &String| s.len() > 5);
    /// ```
    pub fn new(matcher_fn: F) -> Self {
        Self {
            matcher_fn,
            _phantom: std::marker::PhantomData,
        }
    }
}

impl<T, F> Matcher<T> for FnMatcher<T, F>
where
    F: Fn(&T) -> bool,
{
    fn matches(&self, key: &T) -> bool {
        (self.matcher_fn)(key)
    }
}

// ========== Tests ==========

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

    #[test]
    fn test_basic_cache_operations() {
        let mut cache = SimpleCacher::new(Duration::from_secs(1));

        // Test insert and get
        cache.insert("key1".to_string(), "value1".to_string());
        assert_eq!(cache.get(&"key1".to_string()).unwrap().value(), "value1");

        // Test not found
        assert!(matches!(
            cache.get(&"nonexistent".to_string()),
            Err(SimpleCacheError::NotFound)
        ));
    }

    #[test]
    fn test_expiration() {
        let mut cache = SimpleCacher::new(Duration::from_millis(100));

        cache.insert("key1".to_string(), "value1".to_string());
        assert!(cache.get(&"key1".to_string()).is_ok());

        thread::sleep(Duration::from_millis(150));
        assert!(matches!(
            cache.get(&"key1".to_string()),
            Err(SimpleCacheError::Expired)
        ));
    }

    #[test]
    fn test_max_size() {
        let mut cache = SimpleCacher::with_max_size(Duration::from_secs(10), 2);

        cache.insert(1, "value1");
        cache.insert(2, "value2");
        cache.insert(3, "value3"); // Should evict key 1

        assert!(matches!(cache.get(&1), Err(SimpleCacheError::NotFound)));
        assert!(cache.get(&2).is_ok());
        assert!(cache.get(&3).is_ok());
    }

    #[test]
    fn test_prefix_matcher() {
        let mut cache = SimpleCacher::new(Duration::from_secs(10));

        cache.insert("prefix_key1".to_string(), "value1");
        cache.insert("prefix_key2".to_string(), "value2");
        cache.insert("other_key".to_string(), "value3");

        let matcher = PrefixMatcher::new("prefix_");
        let result = cache.get_by_matcher(&matcher);
        assert!(result.is_ok());
        assert!(result.unwrap().value().starts_with("value"));
    }

    #[test]
    fn test_range_matcher() {
        let mut cache = SimpleCacher::new(Duration::from_secs(10));

        cache.insert(1, "value1");
        cache.insert(5, "value5");
        cache.insert(10, "value10");
        cache.insert(15, "value15");

        let matcher = RangeMatcher::new(3, 12);
        let result = cache.get_by_matcher(&matcher);
        assert!(result.is_ok());

        // Should find either key 5 or 10
        let found_value = result.unwrap().value();
        assert!(found_value.to_string() == "value5" || found_value.to_string() == "value10");
    }

    #[test]
    fn test_function_matcher() {
        let mut cache = SimpleCacher::new(Duration::from_secs(10));

        cache.insert(2, "even");
        cache.insert(3, "odd");
        cache.insert(4, "even");
        cache.insert(5, "odd");

        let even_matcher = FnMatcher::new(|&key: &i32| key % 2 == 0);
        let result = cache.get_by_matcher(&even_matcher);
        assert!(result.is_ok());
        assert_eq!(result.unwrap().value().to_string(), "even");
    }
}