Skip to main content

Module chunks

Module chunks 

Source
Expand description

Chunk-based caching for large files and efficient range request handling.

This module provides a chunk-based caching system that splits large files into fixed-size chunks, enabling efficient storage and retrieval of partial content. This is particularly useful for:

  • Video streaming with range requests
  • Large file downloads with resume support
  • Efficient memory usage for large responses

§Examples

use tower_http_cache::chunks::{ChunkCache, ChunkMetadata};
use bytes::Bytes;
use http::StatusCode;

let chunk_cache = ChunkCache::new(1024 * 1024); // 1MB chunks

let metadata = ChunkMetadata {
    total_size: 10 * 1024 * 1024, // 10MB file
    content_type: "video/mp4".to_string(),
    etag: Some("abc123".to_string()),
    last_modified: None,
    status: StatusCode::OK,
    version: http::Version::HTTP_11,
    headers: vec![],
};

let entry = chunk_cache.get_or_create("video.mp4".to_string(), metadata);

// Add chunks as they're received
entry.add_chunk(0, Bytes::from(vec![0u8; 1024 * 1024]));
entry.add_chunk(1, Bytes::from(vec![1u8; 1024 * 1024]));

// Retrieve a range
let range = entry.get_range(0, 1024 * 1024 - 1);
assert!(range.is_some());

Structs§

ChunkCache
Chunk cache manager.
ChunkCacheStats
Statistics for chunk cache.
ChunkMetadata
Metadata for a chunked cache entry.
ChunkedEntry
Chunk-based cache entry for large files.