oxcache 0.1.4

A high-performance multi-level cache library for Rust with L1 (memory) and L2 (Redis) caching.
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
//! Copyright (c) 2025-2026, Kirky.X
//!
//! MIT License
//!
//! 布隆过滤器实现 - 用于缓存穿透防护
//! 通过 `bloom-filter` feature 控制启用/禁用

#[cfg(feature = "bloom-filter")]
use crate::error::CacheError;
#[cfg(feature = "bloom-filter")]
use murmur3::murmur3_32;
#[cfg(feature = "bloom-filter")]
use std::collections::HashMap;
#[cfg(feature = "bloom-filter")]
use std::sync::Arc;
#[cfg(feature = "bloom-filter")]
use std::sync::{
    atomic::{AtomicU64, Ordering},
    RwLock, RwLockReadGuard, RwLockWriteGuard,
};

/// 布隆过滤器配置
#[cfg(feature = "bloom-filter")]
#[derive(Clone, Debug)]
pub struct BloomFilterOptions {
    pub expected_elements: usize,
    pub false_positive_rate: f64,
    pub name: String,
}

#[cfg(feature = "bloom-filter")]
impl BloomFilterOptions {
    pub fn new(name: String, expected_elements: usize, false_positive_rate: f64) -> Self {
        Self {
            name,
            expected_elements,
            false_positive_rate,
        }
    }

    pub fn default_with_name(name: String) -> Self {
        Self {
            name,
            expected_elements: 100000,
            false_positive_rate: 0.01,
        }
    }

    pub fn optimal_size(&self) -> usize {
        let num_items = self.expected_elements as f64;
        let false_positive_prob = self.false_positive_rate;
        let size = -num_items * false_positive_prob.ln() / (std::f64::consts::LN_2).powi(2);
        (size as usize / 8) * 8
    }

    pub fn optimal_num_hashes(&self) -> usize {
        let size = self.optimal_size() as f64 * 8.0;
        let num_items = self.expected_elements as f64;
        (size / num_items * std::f64::consts::LN_2).round() as usize
    }
}

/// 布隆过滤器
///
/// 使用位数组和多个哈希函数实现的空间效率型概率数据结构
/// 用于快速判断元素是否可能存在于集合中
#[cfg(feature = "bloom-filter")]
#[allow(clippy::type_complexity)]
pub struct BloomFilter {
    options: BloomFilterOptions,
    bit_array: Vec<u8>,
    seeds: Vec<u32>,
    added_count: Arc<AtomicU64>,
    checked_count: Arc<AtomicU64>,
    false_positive_count: Arc<AtomicU64>,
    /// 哈希缓存 - 使用 Arc<Vec<u8>> 作为键,避免重复内存分配
    hash_cache: Arc<RwLock<HashMap<Arc<Vec<u8>>, Vec<usize>>>>,
}

#[cfg(feature = "bloom-filter")]
impl BloomFilter {
    /// 创建新的布隆过滤器
    pub fn new(options: BloomFilterOptions) -> Self {
        let size = options.optimal_size();
        let num_hashes = options.optimal_num_hashes();

        let mut seeds = Vec::with_capacity(num_hashes);
        let mut seed = 0xc3f3e5f3u32;
        for _ in 0..num_hashes {
            seeds.push(seed);
            seed = seed.wrapping_mul(0xc13fa9a9u32);
        }

        // 创建哈希缓存
        let hash_cache = Arc::new(RwLock::new(HashMap::new()));

        Self {
            options,
            bit_array: vec![0; size],
            seeds,
            added_count: Arc::new(AtomicU64::new(0)),
            checked_count: Arc::new(AtomicU64::new(0)),
            false_positive_count: Arc::new(AtomicU64::new(0)),
            hash_cache,
        }
    }

    fn calculate_positions(&self, mut item: &[u8]) -> Vec<usize> {
        let bit_array_len = self.bit_array.len();
        let mut positions = Vec::with_capacity(self.seeds.len());
        for &seed in &self.seeds {
            let hash = murmur3_32(&mut item, seed).unwrap_or(0);
            let pos = (hash as usize) % (bit_array_len * 8);
            positions.push(pos);
        }
        positions
    }

    pub fn contains(&self, item: &[u8]) -> Result<bool, CacheError> {
        self.checked_count.fetch_add(1, Ordering::SeqCst);

        // 尝试从缓存获取哈希位置
        let item_key = Arc::new(item.to_vec());
        if let Some(cached_positions) = {
            let cache = self
                .hash_cache
                .read()
                .map_err(|_| CacheError::L1Error("Hash cache lock poisoned".to_string()))?;
            cache.get(&item_key).cloned()
        } {
            // 使用缓存的位置进行检查
            return Ok(self.check_positions(&cached_positions));
        }

        // 缓存未命中,计算新的位置
        let positions = self.calculate_positions(item);

        // 将结果存入缓存(限制缓存大小以避免内存无限增长)
        {
            let mut cache = self
                .hash_cache
                .write()
                .map_err(|_| CacheError::L1Error("Hash cache lock poisoned".to_string()))?;

            // 如果缓存过大,使用 LRU 策略移除部分条目
            if cache.len() > 10000 {
                let mut to_remove = Vec::new();
                for entry in cache.iter() {
                    to_remove.push(entry.0.clone());
                    if to_remove.len() >= 1000 {
                        break;
                    }
                }
                for key in to_remove {
                    cache.remove(&key);
                }
            }

            cache.insert(item_key, positions.clone());
        }

        Ok(self.check_positions(&positions))
    }

    /// 检查位置是否都设置为1
    fn check_positions(&self, positions: &[usize]) -> bool {
        let bit_array = &self.bit_array;

        for pos in positions {
            let byte_idx = pos / 8;
            let bit_idx = pos % 8;

            if byte_idx >= bit_array.len() {
                continue;
            }

            if (bit_array[byte_idx] & (1 << bit_idx)) == 0 {
                return false;
            }
        }

        self.false_positive_count.fetch_add(1, Ordering::SeqCst);
        true
    }

    pub fn add(&mut self, item: &[u8]) -> Result<(), CacheError> {
        // 尝试从缓存获取哈希位置
        let item_key = Arc::new(item.to_vec());
        let positions = if let Some(cached_positions) = {
            let cache = self
                .hash_cache
                .read()
                .map_err(|_| CacheError::L1Error("Hash cache lock poisoned".to_string()))?;
            cache.get(&item_key).cloned()
        } {
            cached_positions
        } else {
            let positions = self.calculate_positions(item);

            // 将结果存入缓存
            {
                let mut cache = self
                    .hash_cache
                    .write()
                    .map_err(|_| CacheError::L1Error("Hash cache lock poisoned".to_string()))?;

                // 如果缓存过大,使用 LRU 策略移除部分条目
                if cache.len() > 10000 {
                    let mut to_remove = Vec::new();
                    for entry in cache.iter() {
                        to_remove.push(entry.0.clone());
                        if to_remove.len() >= 1000 {
                            break;
                        }
                    }
                    for key in to_remove {
                        cache.remove(&key);
                    }
                }

                cache.insert(item_key, positions.clone());
            }

            positions
        };

        for pos in &positions {
            let byte_idx = pos / 8;
            let bit_idx = pos % 8;

            if byte_idx < self.bit_array.len() {
                self.bit_array[byte_idx] |= 1 << bit_idx;
            }
        }

        self.added_count.fetch_add(1, Ordering::SeqCst);
        Ok(())
    }

    pub fn add_checked(&mut self, item: &[u8]) -> Result<bool, CacheError> {
        let existed = self.contains(item)?;
        if !existed {
            self.add(item)?;
        }
        Ok(!existed)
    }

    pub fn contains_and_add(&mut self, item: &[u8]) -> Result<bool, CacheError> {
        let result = self.contains(item)?;
        if !result {
            self.add(item)?;
        }
        Ok(result)
    }

    pub fn remove(&self, _item: &[u8]) -> bool {
        false
    }

    pub fn get_stats(&self) -> BloomFilterStats {
        let total_bits = self.bit_array.len() as u64 * 8;
        let used_bits: u64 = self
            .bit_array
            .iter()
            .map(|byte| byte.count_ones() as u64)
            .sum();
        let added = self.added_count.load(Ordering::SeqCst);
        let checked = self.checked_count.load(Ordering::SeqCst);
        let false_positives = self.false_positive_count.load(Ordering::SeqCst);

        let utilization = if total_bits > 0 {
            used_bits as f64 / total_bits as f64
        } else {
            0.0
        };

        let estimated_count = if self.options.false_positive_rate > 0.0 {
            let ln_2_sq = std::f64::consts::LN_2.powi(2);
            (total_bits as f64 * ln_2_sq / used_bits.max(1) as f64 * 2f64.ln()) as u64
        } else {
            added
        };

        BloomFilterStats {
            name: self.options.name.clone(),
            total_bits,
            used_bits,
            utilization,
            estimated_count,
            added_count: added,
            checked_count: checked,
            false_positive_count: false_positives,
            false_positive_rate: if checked > 0 {
                false_positives as f64 / checked as f64
            } else {
                0.0
            },
            configured_fp_rate: self.options.false_positive_rate,
        }
    }

    pub fn get_estimated_count(&self) -> usize {
        let total_bits = self.bit_array.len() as f64 * 8.0;
        let used_bits: f64 = self
            .bit_array
            .iter()
            .map(|byte| byte.count_ones() as f64)
            .sum();

        if used_bits == 0.0 {
            return 0;
        }

        let num_hashes = self.seeds.len() as f64;
        let ln_2_sq = std::f64::consts::LN_2.powi(2);

        ((-total_bits * ln_2_sq / used_bits).exp() * num_hashes) as usize
    }

    pub fn clear(&mut self) {
        for byte in &mut self.bit_array {
            *byte = 0;
        }
        self.added_count.store(0, Ordering::SeqCst);
    }
}

/// 布隆过滤器统计信息
#[cfg(feature = "bloom-filter")]
#[derive(Clone, Debug)]
pub struct BloomFilterStats {
    pub name: String,
    pub total_bits: u64,
    pub used_bits: u64,
    pub utilization: f64,
    pub estimated_count: u64,
    pub added_count: u64,
    pub checked_count: u64,
    pub false_positive_count: u64,
    pub false_positive_rate: f64,
    pub configured_fp_rate: f64,
}

#[cfg(feature = "bloom-filter")]
impl std::fmt::Display for BloomFilterStats {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "BloomFilter {}: {}/{} bits ({:.2}%), est_count={}, added={}, checked={}, fp_rate={:.4}% (config={:.2}%)",
            self.name,
            self.used_bits,
            self.total_bits,
            self.utilization * 100.0,
            self.estimated_count,
            self.added_count,
            self.checked_count,
            self.false_positive_rate * 100.0,
            self.configured_fp_rate * 100.0,
        )
    }
}

/// 布隆过滤器共享包装器
///
/// 使用Arc包装布隆过滤器,支持多线程共享
#[cfg(feature = "bloom-filter")]
#[derive(Clone)]
pub struct BloomFilterShared {
    filter: Arc<RwLock<BloomFilter>>,
    name: String,
}

#[cfg(feature = "bloom-filter")]
impl BloomFilterShared {
    pub fn new(filter: BloomFilter) -> Self {
        let name = filter.options.name.clone();
        Self {
            filter: Arc::new(RwLock::new(filter)),
            name,
        }
    }

    pub fn contains(&self, item: &[u8]) -> Result<bool, CacheError> {
        self.filter
            .read()
            .map_err(|_| CacheError::L1Error("Filter lock poisoned".to_string()))?
            .contains(item)
    }

    pub async fn add(&self, item: &[u8]) -> Result<(), CacheError> {
        self.filter
            .write()
            .map_err(|_| CacheError::L1Error("Filter lock poisoned".to_string()))?
            .add(item)
    }

    pub async fn contains_and_add(&self, item: &[u8]) -> Result<bool, CacheError> {
        self.filter
            .write()
            .map_err(|_| CacheError::L1Error("Filter lock poisoned".to_string()))?
            .contains_and_add(item)
    }

    pub fn get_stats(&self) -> Result<BloomFilterStats, CacheError> {
        Ok(self
            .filter
            .read()
            .map_err(|_| CacheError::L1Error("Filter lock poisoned".to_string()))?
            .get_stats())
    }

    pub fn name(&self) -> &str {
        &self.name
    }
}

/// 布隆过滤器管理器
///
/// 管理和复用多个布隆过滤器实例
#[cfg(feature = "bloom-filter")]
#[derive(Clone, Default)]
pub struct BloomFilterManager {
    filters: Arc<RwLock<HashMap<String, BloomFilterShared>>>,
}

#[cfg(feature = "bloom-filter")]
impl BloomFilterManager {
    pub fn new() -> Self {
        Self {
            filters: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    pub async fn get_or_create(
        &self,
        options: BloomFilterOptions,
    ) -> Result<BloomFilterShared, CacheError> {
        let mut guard: RwLockWriteGuard<'_, HashMap<String, BloomFilterShared>> = self
            .filters
            .write()
            .map_err(|_| CacheError::L1Error("Filters lock poisoned".to_string()))?;

        if let Some(existing) = guard.get(&options.name) {
            let existing: &BloomFilterShared = existing;
            return Ok(existing.clone());
        }

        let filter = BloomFilter::new(options.clone());
        let shared = BloomFilterShared::new(filter);
        guard.insert(options.name.clone(), shared.clone());
        Ok(shared)
    }

    pub fn get(&self, name: &str) -> Result<Option<BloomFilterShared>, CacheError> {
        Ok(self
            .filters
            .read()
            .map_err(|_| CacheError::L1Error("Filters lock poisoned".to_string()))?
            .get(name)
            .cloned())
    }

    pub fn remove(&self, name: &str) -> Result<bool, CacheError> {
        Ok(self
            .filters
            .write()
            .map_err(|_| CacheError::L1Error("Filters lock poisoned".to_string()))?
            .remove(name)
            .is_some())
    }

    pub fn list_names(&self) -> Result<Vec<String>, CacheError> {
        Ok(self
            .filters
            .read()
            .map_err(|_| CacheError::L1Error("Filters lock poisoned".to_string()))?
            .keys()
            .cloned()
            .collect())
    }

    pub async fn get_all_stats(&self) -> Result<Vec<BloomFilterStats>, CacheError> {
        let guard: RwLockReadGuard<'_, HashMap<String, BloomFilterShared>> = self
            .filters
            .read()
            .map_err(|_| CacheError::L1Error("Filters lock poisoned".to_string()))?;
        let mut stats = Vec::with_capacity(guard.len());

        for filter in guard.values() {
            let filter: &BloomFilterShared = filter;
            if let Ok(stat) = filter.get_stats() {
                stats.push(stat);
            }
        }

        Ok(stats)
    }
}

// ============================================================================
// 当 bloom-filter 功能禁用时的空实现
// ============================================================================

#[cfg(not(feature = "bloom-filter"))]
/// 布隆过滤器配置(空实现)
#[derive(Clone, Debug, Default)]
pub struct BloomFilterOptions;

#[cfg(not(feature = "bloom-filter"))]
impl BloomFilterOptions {
    pub fn new(_name: String, _expected_elements: usize, _false_positive_rate: f64) -> Self {
        Self
    }

    pub fn default_with_name(_name: String) -> Self {
        Self
    }

    pub fn optimal_size(&self) -> usize {
        0
    }

    pub fn optimal_num_hashes(&self) -> usize {
        0
    }
}

/// 布隆过滤器(空实现)
#[cfg(not(feature = "bloom-filter"))]
#[derive(Clone, Debug)]
pub struct BloomFilter;

#[cfg(not(feature = "bloom-filter"))]
use crate::error::CacheError;

#[cfg(not(feature = "bloom-filter"))]
impl BloomFilter {
    pub fn new(_options: BloomFilterOptions) -> Self {
        Self
    }

    pub fn contains(&self, _item: &[u8]) -> Result<bool, CacheError> {
        Ok(false)
    }

    pub fn add(&mut self, _item: &[u8]) -> Result<(), CacheError> {
        Ok(())
    }

    pub fn add_checked(&mut self, _item: &[u8]) -> Result<bool, CacheError> {
        Ok(false)
    }

    pub fn contains_and_add(&mut self, _item: &[u8]) -> Result<bool, CacheError> {
        Ok(false)
    }

    pub fn remove(&self, _item: &[u8]) -> bool {
        false
    }

    pub fn get_stats(&self) -> BloomFilterStats {
        BloomFilterStats::default()
    }

    pub fn get_estimated_count(&self) -> usize {
        0
    }

    pub fn clear(&mut self) {}
}

/// 布隆过滤器统计信息(空实现)
#[cfg(not(feature = "bloom-filter"))]
#[derive(Clone, Debug, Default)]
pub struct BloomFilterStats {
    pub name: String,
    pub total_bits: u64,
    pub used_bits: u64,
    pub utilization: f64,
    pub estimated_count: u64,
    pub added_count: u64,
    pub checked_count: u64,
    pub false_positive_count: u64,
    pub false_positive_rate: f64,
    pub configured_fp_rate: f64,
}

#[cfg(not(feature = "bloom-filter"))]
impl std::fmt::Display for BloomFilterStats {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "BloomFilter (Disabled)")
    }
}

/// 布隆过滤器共享包装器(空实现)
#[cfg(not(feature = "bloom-filter"))]
#[derive(Clone, Default)]
pub struct BloomFilterShared;

#[cfg(not(feature = "bloom-filter"))]
impl BloomFilterShared {
    pub fn new(_filter: BloomFilter) -> Self {
        Self
    }

    pub fn contains(&self, _item: &[u8]) -> Result<bool, CacheError> {
        Ok(false)
    }

    pub async fn add(&self, _item: &[u8]) -> Result<(), CacheError> {
        Ok(())
    }

    pub async fn contains_and_add(&self, _item: &[u8]) -> Result<bool, CacheError> {
        Ok(false)
    }

    pub fn get_stats(&self) -> Result<BloomFilterStats, CacheError> {
        Ok(BloomFilterStats::default())
    }

    pub fn name(&self) -> &str {
        ""
    }
}

/// 布隆过滤器管理器(空实现)
#[cfg(not(feature = "bloom-filter"))]
#[derive(Clone, Default)]
pub struct BloomFilterManager;

#[cfg(not(feature = "bloom-filter"))]
impl BloomFilterManager {
    pub fn new() -> Self {
        Self
    }

    pub async fn get_or_create(
        &self,
        _options: BloomFilterOptions,
    ) -> Result<BloomFilterShared, CacheError> {
        Ok(BloomFilterShared::new(BloomFilter::new(
            BloomFilterOptions::default(),
        )))
    }

    pub fn get(&self, _name: &str) -> Result<Option<BloomFilterShared>, CacheError> {
        Ok(None)
    }

    pub fn remove(&self, _name: &str) -> Result<bool, CacheError> {
        Ok(false)
    }

    pub fn list_names(&self) -> Result<Vec<String>, CacheError> {
        Ok(Vec::new())
    }

    pub async fn get_all_stats(&self) -> Result<Vec<BloomFilterStats>, CacheError> {
        Ok(Vec::new())
    }
}

#[cfg(test)]
#[cfg(feature = "bloom-filter")]
mod tests {
    use super::*;

    #[test]
    fn test_bloom_filter_basic() -> Result<(), CacheError> {
        let options = BloomFilterOptions::default_with_name("test".to_string());
        let mut filter = BloomFilter::new(options);

        assert!(!filter.contains(b"hello")?);
        assert!(!filter.contains(b"world")?);

        filter.add(b"hello")?;

        assert!(filter.contains(b"hello")?);
        assert!(!filter.contains(b"world")?);

        filter.add(b"world")?;

        assert!(filter.contains(b"hello")?);
        assert!(filter.contains(b"world")?);
        Ok(())
    }

    #[test]
    fn test_bloom_filter_false_positive_rate() -> Result<(), CacheError> {
        let options = BloomFilterOptions::new("test_fp".to_string(), 10000, 0.01);
        let mut filter = BloomFilter::new(options);

        for i in 0..1000 {
            filter.add(format!("item_{}", i).as_bytes())?;
        }

        let mut false_positives = 0;
        for i in 1000..2000 {
            if filter.contains(format!("fake_{}", i).as_bytes())? {
                false_positives += 1;
            }
        }

        let fp_rate = false_positives as f64 / 1000.0;
        assert!(fp_rate < 0.05, "False positive rate too high: {}", fp_rate);
        Ok(())
    }

    #[test]
    fn test_bloom_filter_contains_and_add() -> Result<(), CacheError> {
        let options = BloomFilterOptions::default_with_name("test_caa".to_string());
        let mut filter = BloomFilter::new(options);

        assert!(!filter.contains_and_add(b"new_item")?);
        assert!(filter.contains_and_add(b"new_item")?);
        Ok(())
    }

    #[test]
    fn test_optimal_size_calculation() {
        let options = BloomFilterOptions::new("test".to_string(), 100000, 0.01);
        assert!(options.optimal_size() > 0);
        assert!(options.optimal_num_hashes() > 0);
    }
}