pub struct ChunkedEntry {
pub metadata: ChunkMetadata,
pub chunks: Arc<DashMap<u64, Bytes>>,
pub chunk_size: usize,
}Expand description
Chunk-based cache entry for large files.
A ChunkedEntry splits a large file into fixed-size chunks, allowing
efficient storage and retrieval of partial content. This is particularly
useful for range requests where only a portion of the file is needed.
Fields§
§metadata: ChunkMetadataMetadata about the full file
chunks: Arc<DashMap<u64, Bytes>>Map of chunk index to chunk data
chunk_size: usizeChunk size in bytes (default: 1MB)
Implementations§
Source§impl ChunkedEntry
impl ChunkedEntry
Sourcepub fn new(metadata: ChunkMetadata, chunk_size: usize) -> Self
pub fn new(metadata: ChunkMetadata, chunk_size: usize) -> Self
Creates a new chunked entry with the given metadata and chunk size.
§Arguments
metadata- File metadata including size, content type, and headerschunk_size- Size of each chunk in bytes
§Examples
use tower_http_cache::chunks::{ChunkedEntry, ChunkMetadata};
use http::StatusCode;
let metadata = ChunkMetadata {
total_size: 10_000_000,
content_type: "video/mp4".to_string(),
etag: None,
last_modified: None,
status: StatusCode::OK,
version: http::Version::HTTP_11,
headers: vec![],
};
let entry = ChunkedEntry::new(metadata, 1024 * 1024);
assert_eq!(entry.chunk_size, 1024 * 1024);Sourcepub fn get_range(&self, start: u64, end: u64) -> Option<Bytes>
pub fn get_range(&self, start: u64, end: u64) -> Option<Bytes>
Gets a byte range from the cached chunks.
Returns None if any required chunks are missing from the cache.
The range is inclusive: [start, end].
§Arguments
start- Starting byte offset (inclusive)end- Ending byte offset (inclusive)
§Examples
let entry = ChunkedEntry::new(metadata, 1024);
entry.add_chunk(0, Bytes::from(vec![0u8; 1024]));
entry.add_chunk(1, Bytes::from(vec![1u8; 1024]));
// Get first 512 bytes
let range = entry.get_range(0, 511);
assert!(range.is_some());
assert_eq!(range.unwrap().len(), 512);Sourcepub fn is_complete(&self) -> bool
pub fn is_complete(&self) -> bool
Checks if all chunks are cached.
Returns true if the entry contains all chunks needed to represent
the full file.
§Examples
let entry = ChunkedEntry::new(metadata, 1024);
assert!(!entry.is_complete());
entry.add_chunk(0, Bytes::from(vec![0u8; 1024]));
entry.add_chunk(1, Bytes::from(vec![1u8; 1024]));
assert!(entry.is_complete());Sourcepub fn coverage(&self) -> f64
pub fn coverage(&self) -> f64
Gets the cache coverage percentage.
Returns a percentage (0-100) indicating how much of the file is cached.
§Examples
let entry = ChunkedEntry::new(metadata, 1024);
assert_eq!(entry.coverage(), 0.0);
entry.add_chunk(0, Bytes::from(vec![0u8; 1024]));
assert_eq!(entry.coverage(), 25.0);
entry.add_chunk(1, Bytes::from(vec![1u8; 1024]));
assert_eq!(entry.coverage(), 50.0);Trait Implementations§
Source§impl Clone for ChunkedEntry
impl Clone for ChunkedEntry
Source§fn clone(&self) -> ChunkedEntry
fn clone(&self) -> ChunkedEntry
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more