pub struct StreamingPolicy {
pub enabled: bool,
pub max_cacheable_size: Option<usize>,
pub excluded_content_types: HashSet<String>,
pub force_cache_content_types: HashSet<String>,
pub stream_threshold: usize,
pub range_handling: RangeHandling,
pub enable_chunk_cache: bool,
pub chunk_size: usize,
pub min_chunk_file_size: u64,
}Expand description
Configuration for smart streaming and large file handling.
The streaming policy allows you to configure how the cache handles large response bodies and specific content types. This prevents memory exhaustion and cache pollution from large files.
§Examples
use tower_http_cache::streaming::StreamingPolicy;
use tower_http_cache::range::RangeHandling;
use std::collections::HashSet;
let policy = StreamingPolicy {
enabled: true,
max_cacheable_size: Some(1024 * 1024), // 1MB
excluded_content_types: HashSet::from([
"application/pdf".to_string(),
"video/*".to_string(),
]),
range_handling: RangeHandling::PassThrough,
..Default::default()
};Fields§
§enabled: boolEnable smart streaming (default: true)
max_cacheable_size: Option<usize>Skip caching bodies larger than this (default: 1MB) Set to None to disable size-based filtering
excluded_content_types: HashSet<String>Content types to never cache (PDFs, videos, archives, etc.) Supports wildcards like “video/*”
force_cache_content_types: HashSet<String>Always cache these content types regardless of size Useful for API responses that should always be cached
stream_threshold: usizeUse streaming for bodies above this size (default: 512KB) Currently used for decision making; actual streaming not yet implemented
range_handling: RangeHandlingHow to handle HTTP Range requests (default: PassThrough)
enable_chunk_cache: boolEnable chunk caching for large files (default: false, opt-in)
When enabled, large files are split into chunks and cached separately, allowing efficient range request handling without caching the entire file.
chunk_size: usizeChunk size for large files (default: 1MB)
Files are split into chunks of this size when chunk caching is enabled. Larger chunks use less memory overhead but may waste bandwidth for small range requests. Smaller chunks are more granular but have more overhead.
min_chunk_file_size: u64Minimum file size for chunking (default: 10MB)
Files smaller than this will not be chunked even if chunk caching is enabled. This prevents overhead for files that don’t benefit from chunking.
Implementations§
Source§impl StreamingPolicy
impl StreamingPolicy
Sourcepub fn disabled() -> Self
pub fn disabled() -> Self
Creates a new streaming policy with all features disabled. Useful for gradually migrating existing code.
Sourcepub fn size_only(max_size: usize) -> Self
pub fn size_only(max_size: usize) -> Self
Creates a streaming policy that only filters by size.
Sourcepub fn content_type_only(excluded: HashSet<String>) -> Self
pub fn content_type_only(excluded: HashSet<String>) -> Self
Creates a streaming policy that only filters by content type.
Trait Implementations§
Source§impl Clone for StreamingPolicy
impl Clone for StreamingPolicy
Source§fn clone(&self) -> StreamingPolicy
fn clone(&self) -> StreamingPolicy
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more