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
//! Memory caching for pieces and blocks.
//!
//! This module provides in-memory caching to reduce disk I/O and improve
//! download performance.
//!
//! # Overview
//!
//! The cache system has several components:
//!
//! - [`PieceCache`] - Caches complete pieces using the ARC algorithm
//! - [`BlockCache`] - Caches individual blocks being downloaded
//! - [`BufferPool`] - Reusable buffer allocation
//! - [`MemoryBudget`] - Memory limit enforcement
//!
//! # ARC Caching Algorithm
//!
//! The [`PieceCache`] uses the Adaptive Replacement Cache (ARC) algorithm,
//! which adapts to access patterns by tracking both recent and frequent
//! access. This provides better hit rates than simple LRU for mixed workloads.
//!
//! # Examples
//!
//! ## Using the piece cache
//!
//! ```
//! use rbit::cache::PieceCache;
//! use bytes::Bytes;
//!
//! let cache = PieceCache::new(100); // capacity of 100 pieces
//!
//! // Cache a piece
//! let data = Bytes::from(vec![0u8; 16384]);
//! cache.insert("info_hash", 0, data.clone(), true);
//!
//! // Retrieve from cache
//! if let Some(cached) = cache.get("info_hash", 0) {
//! assert_eq!(cached.len(), 16384);
//! }
//! ```
//!
//! ## Memory budgeting
//!
//! ```
//! use rbit::cache::MemoryBudget;
//!
//! // Create a 256MB budget
//! let budget = MemoryBudget::new(256 * 1024 * 1024);
//!
//! // Try to allocate memory
//! if let Some(permit) = budget.try_allocate(16384) {
//! // Use the allocated memory
//! println!("Allocated {} bytes", permit.bytes());
//! // Memory is released when permit is dropped
//! }
//! ```
pub use ;
pub use BufferPool;
pub use ;
pub use PieceCache;